org.springframework.amqp.support.converter.Jackson2JsonMessageConverter Java Examples

The following examples show how to use org.springframework.amqp.support.converter.Jackson2JsonMessageConverter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RabbitConfig.java    From SpringBootBucket with MIT License 6 votes vote down vote up
/**
 * 定制化amqp模版      可根据需要定制多个
 * <p>
 * <p>
 * 此处为模版类定义 Jackson消息转换器
 * ConfirmCallback接口用于实现消息发送到RabbitMQ交换器后接收ack回调   即消息发送到exchange  ack
 * ReturnCallback接口用于实现消息发送到RabbitMQ 交换器,但无相应队列与交换器绑定时的回调  即消息发送不到任何一个队列中  ack
 *
 * @return the amqp template
 */
// @Primary
@Bean
public AmqpTemplate amqpTemplate() {
    Logger log = LoggerFactory.getLogger(RabbitTemplate.class);
    // 使用jackson 消息转换器
    rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
    rabbitTemplate.setEncoding("UTF-8");
    // 消息发送失败返回到队列中,yml需要配置 publisher-returns: true
    rabbitTemplate.setMandatory(true);
    rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
        String correlationId = message.getMessageProperties().getCorrelationIdString();
        log.debug("消息:{} 发送失败, 应答码:{} 原因:{} 交换机: {}  路由键: {}", correlationId, replyCode, replyText, exchange, routingKey);
    });
    // 消息确认,yml需要配置 publisher-confirms: true
    rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
        if (ack) {
            log.debug("消息发送到exchange成功,id: {}", correlationData.getId());
        } else {
            log.debug("消息发送到exchange失败,原因: {}", cause);
        }
    });
    return rabbitTemplate;
}
 
Example #2
Source File: AmqpMessageHandlerServiceTest.java    From hawkbit with Eclipse Public License 1.0 6 votes vote down vote up
@Before
@SuppressWarnings({ "rawtypes", "unchecked" })
public void before() throws Exception {
    messageConverter = new Jackson2JsonMessageConverter();
    when(rabbitTemplate.getMessageConverter()).thenReturn(messageConverter);
    when(artifactManagementMock.findFirstBySHA1(SHA1)).thenReturn(Optional.empty());
    final TenantConfigurationValue multiAssignmentConfig = TenantConfigurationValue.builder().value(Boolean.FALSE)
            .global(Boolean.FALSE).build();
    when(tenantConfigurationManagement.getConfigurationValue(MULTI_ASSIGNMENTS_ENABLED, Boolean.class))
            .thenReturn(multiAssignmentConfig);

    final SecurityContextTenantAware tenantAware = new SecurityContextTenantAware();
    final SystemSecurityContext systemSecurityContext = new SystemSecurityContext(tenantAware);

    amqpMessageHandlerService = new AmqpMessageHandlerService(rabbitTemplate, amqpMessageDispatcherServiceMock,
            controllerManagementMock, entityFactoryMock, systemSecurityContext, tenantConfigurationManagement);
    amqpAuthenticationMessageHandlerService = new AmqpAuthenticationMessageHandler(rabbitTemplate,
            authenticationManagerMock, artifactManagementMock, downloadIdCache, hostnameResolverMock,
            controllerManagementMock, tenantAwareMock);
}
 
Example #3
Source File: RabbitMQConfiguration.java    From seed with Apache License 2.0 6 votes vote down vote up
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
    RabbitTemplate template = new RabbitTemplate(connectionFactory);
    template.setMessageConverter(new Jackson2JsonMessageConverter());
    template.setEncoding(SeedConstants.DEFAULT_CHARSET);
    //消息发送失败时,返回到队列中(需要spring.rabbitmq.publisherReturns=true)
    template.setMandatory(true);
    //消息成功到达exchange,但没有queue与之绑定时触发的回调(即消息发送不到任何一个队列中)
    //也可以在生产者发送消息的类上实现org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback和RabbitTemplate.ReturnCallback两个接口(本例中即为SendController.java)
    template.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> LogUtil.getLogger().error("消息发送失败,replyCode={},replyText={},exchange={},routingKey={},消息体=[{}]", replyCode, replyText, exchange, routingKey, JSON.toJSONString(message.getBody())));
    //消息成功到达exchange后触发的ack回调(需要spring.rabbitmq.publisherConfirms=true)
    template.setConfirmCallback((correlationData, ack, cause) -> {
        if(ack){
            LogUtil.getLogger().info("消息发送成功,消息ID={}", correlationData.getId());
        }else{
            LogUtil.getLogger().error("消息发送失败,消息ID={},cause={}", correlationData.getId(), cause);
        }
    });
    return template;
}
 
Example #4
Source File: RabbitMQConfiguration.java    From cukes with Apache License 2.0 5 votes vote down vote up
@Bean
SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory  cf,
                                                                    ObjectMapper mapper) {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(cf);
    factory.setMessageConverter(new Jackson2JsonMessageConverter(mapper));
    return factory;
}
 
Example #5
Source File: RabbitMQConfiguration.java    From cukes with Apache License 2.0 5 votes vote down vote up
@Bean
RabbitTemplate rabbitTemplate(org.springframework.amqp.rabbit.connection.ConnectionFactory  cf,
                              ObjectMapper mapper) {
    RabbitTemplate template = new RabbitTemplate(cf);
    template.setExchange(EXCHANGE_NAME);
    RetryTemplate retry = new RetryTemplate();
    ExponentialBackOffPolicy backOff = new ExponentialBackOffPolicy();
    backOff.setInitialInterval(1000);
    backOff.setMultiplier(1.5);
    backOff.setMaxInterval(60000);
    retry.setBackOffPolicy(backOff);
    template.setRetryTemplate(retry);
    template.setMessageConverter(new Jackson2JsonMessageConverter(mapper));
    return template;
}
 
Example #6
Source File: TopicRabbitConfig.java    From iot-dc3 with Apache License 2.0 5 votes vote down vote up
@Bean
public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setMessageConverter(new Jackson2JsonMessageConverter());
    return factory;
}
 
Example #7
Source File: AmqpTestConfiguration.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@Bean
@Primary
public RabbitTemplate rabbitTemplateForTest(final ConnectionFactory connectionFactory) {
    final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
    rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
    rabbitTemplate.setReplyTimeout(TimeUnit.SECONDS.toMillis(3));
    rabbitTemplate.setReceiveTimeout(TimeUnit.SECONDS.toMillis(3));
    return rabbitTemplate;
}
 
Example #8
Source File: AbstractAmqpIntegrationTest.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
private RabbitTemplate createDmfClient() {
    final RabbitTemplate template = new RabbitTemplate(connectionFactory);
    template.setMessageConverter(new Jackson2JsonMessageConverter());
    template.setReceiveTimeout(TimeUnit.SECONDS.toMillis(3));
    template.setReplyTimeout(TimeUnit.SECONDS.toMillis(3));
    template.setExchange(getExchange());
    return template;
}
 
Example #9
Source File: RabbitMQConfiguration.java    From seed with Apache License 2.0 5 votes vote down vote up
@Bean
public SimpleRabbitListenerContainerFactory jadyerRabbitListenerContainerFactory(ConnectionFactory connectionFactory){
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setMessageConverter(new Jackson2JsonMessageConverter());
    return factory;
}
 
Example #10
Source File: RabbitMqConfiguration.java    From scraping-microservice-java-python-rabbitmq with Apache License 2.0 5 votes vote down vote up
@Bean
public MessageConverter jsonMessageConverter()
{
    final Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
    converter.setClassMapper(classMapper());
    return converter;
}
 
Example #11
Source File: AmqpMessageDispatcherServiceTest.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void before() throws Exception {
    super.before();
    testTarget = targetManagement.create(entityFactory.target().create().controllerId(CONTROLLER_ID)
            .securityToken(TEST_TOKEN).address(AMQP_URI.toString()));

    this.rabbitTemplate = Mockito.mock(RabbitTemplate.class);
    when(rabbitTemplate.getMessageConverter()).thenReturn(new Jackson2JsonMessageConverter());

    senderService = Mockito.mock(DefaultAmqpMessageSenderService.class);

    final ArtifactUrlHandler artifactUrlHandlerMock = Mockito.mock(ArtifactUrlHandler.class);
    when(artifactUrlHandlerMock.getUrls(any(), any()))
            .thenReturn(Arrays.asList(new ArtifactUrl("http", "download", "http://mockurl")));

    systemManagement = Mockito.mock(SystemManagement.class);
    final TenantMetaData tenantMetaData = Mockito.mock(TenantMetaData.class);
    when(tenantMetaData.getId()).thenReturn(TENANT_ID);
    when(tenantMetaData.getTenant()).thenReturn(TENANT);

    when(systemManagement.getTenantMetadata()).thenReturn(tenantMetaData);

    amqpMessageDispatcherService = new AmqpMessageDispatcherService(rabbitTemplate, senderService,
            artifactUrlHandlerMock, systemSecurityContext, systemManagement, targetManagement, serviceMatcher,
            distributionSetManagement, softwareModuleManagement, deploymentManagement);

}
 
Example #12
Source File: RabbitConfig.java    From iot-dc3 with Apache License 2.0 5 votes vote down vote up
@Bean
public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setMessageConverter(new Jackson2JsonMessageConverter());
    return factory;
}
 
Example #13
Source File: TopicRabbitConfig.java    From iot-dc3 with Apache License 2.0 5 votes vote down vote up
@Bean
public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setMessageConverter(new Jackson2JsonMessageConverter());
    return factory;
}
 
Example #14
Source File: RabbitConfig.java    From cloud-espm-cloud-native with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the rabbit template.
 * 
 * @param rabbitConnectionFactory
 * @return RabbitTemplate
 */
@Bean
RabbitTemplate rabbitTemplateSettings(ConnectionFactory rabbitConnectionFactory){
	RabbitTemplate template = new RabbitTemplate(rabbitConnectionFactory);
	template.setMessageConverter(new Jackson2JsonMessageConverter());
	return template;

}
 
Example #15
Source File: RabbitTemplateConfig.java    From spring-boot-tutorials with Apache License 2.0 5 votes vote down vote up
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setMessageConverter(new Jackson2JsonMessageConverter());
    return factory;
}
 
Example #16
Source File: RabbitMQConfig.java    From rome with Apache License 2.0 5 votes vote down vote up
@Bean("rabbitTemplate")
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory,
                                     @Qualifier("jackson2JsonMessageConverter") Jackson2JsonMessageConverter jackson2JsonMessageConverter) {
    RabbitTemplate template = new RabbitTemplate(connectionFactory);
    template.setMessageConverter(new Jackson2JsonMessageConverter());
    return template;
}
 
Example #17
Source File: RabbitTemplateConfig.java    From spring-boot-tutorials with Apache License 2.0 5 votes vote down vote up
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setMessageConverter(new Jackson2JsonMessageConverter());
    return factory;
}
 
Example #18
Source File: BaseAmqpServiceTest.java    From hawkbit with Eclipse Public License 1.0 4 votes vote down vote up
@Before
public void setup() {
    when(rabbitTemplate.getMessageConverter()).thenReturn(new Jackson2JsonMessageConverter());
    baseAmqpService = new BaseAmqpService(rabbitTemplate);

}
 
Example #19
Source File: BusConfig.java    From SpringCloud with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    return new ContentTypeDelegatingMessageConverter(new Jackson2JsonMessageConverter(objectMapper));
}
 
Example #20
Source File: AmqpControllerAuthenticationTest.java    From hawkbit with Eclipse Public License 1.0 4 votes vote down vote up
@Before
public void before() throws Exception {
    messageConverter = new Jackson2JsonMessageConverter();
    final RabbitTemplate rabbitTemplate = mock(RabbitTemplate.class);
    when(rabbitTemplate.getMessageConverter()).thenReturn(messageConverter);

    final DdiSecurityProperties secruityProperties = mock(DdiSecurityProperties.class);
    final Rp rp = mock(Rp.class);
    final DdiSecurityProperties.Authentication ddiAuthentication = mock(DdiSecurityProperties.Authentication.class);
    final Anonymous anonymous = mock(Anonymous.class);
    when(secruityProperties.getRp()).thenReturn(rp);
    when(rp.getSslIssuerHashHeader()).thenReturn("X-Ssl-Issuer-Hash-%d");
    when(secruityProperties.getAuthentication()).thenReturn(ddiAuthentication);
    when(ddiAuthentication.getAnonymous()).thenReturn(anonymous);
    when(anonymous.isEnabled()).thenReturn(false);

    when(tenantConfigurationManagementMock.getConfigurationValue(any(), eq(Boolean.class)))
            .thenReturn(CONFIG_VALUE_FALSE);

    final ControllerManagement controllerManagement = mock(ControllerManagement.class);
    when(controllerManagement.getByControllerId(anyString())).thenReturn(Optional.of(targetMock));
    when(controllerManagement.get(any(Long.class))).thenReturn(Optional.of(targetMock));

    when(targetMock.getSecurityToken()).thenReturn(CONTROLLER_ID);
    when(targetMock.getControllerId()).thenReturn(CONTROLLER_ID);

    final SecurityContextTenantAware tenantAware = new SecurityContextTenantAware();
    final SystemSecurityContext systemSecurityContext = new SystemSecurityContext(tenantAware);

    final TenantMetaData tenantMetaData = mock(TenantMetaData.class);
    when(tenantMetaData.getTenant()).thenReturn(TENANT);
    when(systemManagement.getTenantMetadata(TENANT_ID)).thenReturn(tenantMetaData);

    authenticationManager = new AmqpControllerAuthentication(systemManagement, controllerManagement,
            tenantConfigurationManagementMock, tenantAware, secruityProperties, systemSecurityContext);

    authenticationManager.postConstruct();

    final JpaArtifact testArtifact = new JpaArtifact(SHA1, "afilename", new JpaSoftwareModule(
            new JpaSoftwareModuleType("a key", "a name", null, 1), "a name", null, null, null));
    testArtifact.setId(1L);

    when(artifactManagementMock.get(ARTIFACT_ID)).thenReturn(Optional.of(testArtifact));
    when(artifactManagementMock.findFirstBySHA1(SHA1)).thenReturn(Optional.of(testArtifact));

    amqpMessageHandlerService = new AmqpMessageHandlerService(rabbitTemplate,
            mock(AmqpMessageDispatcherService.class), controllerManagementMock, new JpaEntityFactory(),
            systemSecurityContext, tenantConfigurationManagementMock);

    amqpAuthenticationMessageHandlerService = new AmqpAuthenticationMessageHandler(rabbitTemplate,
            authenticationManager, artifactManagementMock, cacheMock, hostnameResolverMock,
            controllerManagementMock, tenantAware);

    when(hostnameResolverMock.resolveHostname()).thenReturn(new URL("http://localhost"));

    when(controllerManagementMock.hasTargetArtifactAssigned(TARGET_ID, SHA1)).thenReturn(true);
    when(controllerManagementMock.hasTargetArtifactAssigned(CONTROLLER_ID, SHA1)).thenReturn(true);
}
 
Example #21
Source File: AmqpConfig.java    From demo_springboot_rabbitmq with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter jsonMessageConverter() {
    return new Jackson2JsonMessageConverter();
}
 
Example #22
Source File: RabbitConfig.java    From POC with Apache License 2.0 4 votes vote down vote up
@Bean
public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
	return new Jackson2JsonMessageConverter();
}
 
Example #23
Source File: RabbitSinkTests.java    From spring-cloud-stream-app-starters with Apache License 2.0 4 votes vote down vote up
@Bean
public Jackson2JsonMessageConverter myConverter() {
	return new Jackson2JsonMessageConverter();
}
 
Example #24
Source File: RabbitConfigurer.java    From bird-java with MIT License 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    return new ContentTypeDelegatingMessageConverter(new Jackson2JsonMessageConverter());
}
 
Example #25
Source File: BusConfig.java    From SpringCloud with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    return new ContentTypeDelegatingMessageConverter(new Jackson2JsonMessageConverter(objectMapper));
}
 
Example #26
Source File: BusConfig.java    From SpringCloud with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    return new ContentTypeDelegatingMessageConverter(new Jackson2JsonMessageConverter(objectMapper));
}
 
Example #27
Source File: BusConfig.java    From SpringCloud with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    return new ContentTypeDelegatingMessageConverter(new Jackson2JsonMessageConverter(objectMapper));
}
 
Example #28
Source File: MQConsumerConfig.java    From lemon-rabbitmq with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    return new Jackson2JsonMessageConverter();
}
 
Example #29
Source File: MQProducerConfig.java    From lemon-rabbitmq with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    return new Jackson2JsonMessageConverter();
}
 
Example #30
Source File: RabbitMQConfig.java    From jakduk-api with MIT License 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    return new Jackson2JsonMessageConverter(objectMapper);
}