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

The following examples show how to use org.springframework.amqp.support.converter.Jackson2JsonMessageConverter. These examples are extracted from open source projects. 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 Project: SpringBootBucket   Source File: RabbitConfig.java    License: 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 Project: seed   Source File: RabbitMQConfiguration.java    License: 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 3
@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 4
Source Project: rome   Source File: RabbitMQConfig.java    License: 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 5
Source Project: cloud-espm-cloud-native   Source File: RabbitConfig.java    License: 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 6
Source Project: iot-dc3   Source File: TopicRabbitConfig.java    License: 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 Project: iot-dc3   Source File: RabbitConfig.java    License: 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 8
Source Project: iot-dc3   Source File: TopicRabbitConfig.java    License: 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 9
Source Project: seed   Source File: RabbitMQConfiguration.java    License: 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
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setMessageConverter(new Jackson2JsonMessageConverter());
    return factory;
}
 
Example 11
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setMessageConverter(new Jackson2JsonMessageConverter());
    return factory;
}
 
Example 12
@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 13
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 14
Source Project: hawkbit   Source File: AmqpTestConfiguration.java    License: 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 15
Source Project: cukes   Source File: RabbitMQConfiguration.java    License: 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 16
Source Project: cukes   Source File: RabbitMQConfiguration.java    License: 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 17
@Bean
public MessageConverter jsonMessageConverter()
{
    final Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
    converter.setClassMapper(classMapper());
    return converter;
}
 
Example 18
@Bean
public MessageConverter messageConverter() {
    return new Jackson2JsonMessageConverter();
}
 
Example 19
Source Project: mogu_blog_v2   Source File: RabbitMqConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    return new Jackson2JsonMessageConverter();
}
 
Example 20
Source Project: mogu_blog_v2   Source File: RabbitMqConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    return new Jackson2JsonMessageConverter();
}
 
Example 21
Source Project: mogu_blog_v2   Source File: RabbitMqConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter messageConverter() {
    return new Jackson2JsonMessageConverter();
}
 
Example 22
Source Project: gpmall   Source File: RabbitMqConfig.java    License: Apache License 2.0 4 votes vote down vote up
Jackson2JsonMessageConverter jsonMessageConverter(){
	return new Jackson2JsonMessageConverter();
}
 
Example 23
@Bean
public MessageConverter messageConverter() {
    return new Jackson2JsonMessageConverter();
}
 
Example 24
@Bean
public Jackson2JsonMessageConverter messageConverter() {
  return new Jackson2JsonMessageConverter();
}
 
Example 25
@Bean
public Jackson2JsonMessageConverter messageConverter() {
  return new Jackson2JsonMessageConverter();
}
 
Example 26
@Bean
public Jackson2JsonMessageConverter messageConverter() {
  return new Jackson2JsonMessageConverter();
}
 
Example 27
@Bean
public Jackson2JsonMessageConverter messageConverter() {
  return new Jackson2JsonMessageConverter();
}
 
Example 28
@Bean
public Jackson2JsonMessageConverter messageConverter() {
  return new Jackson2JsonMessageConverter();
}
 
Example 29
@Bean
public Jackson2JsonMessageConverter messageConverter() {
  return new Jackson2JsonMessageConverter();
}
 
Example 30
@Bean
public Jackson2JsonMessageConverter messageConverter() {
  return new Jackson2JsonMessageConverter();
}