Java Code Examples for org.springframework.amqp.rabbit.core.RabbitTemplate#setMessageConverter()

The following examples show how to use org.springframework.amqp.rabbit.core.RabbitTemplate#setMessageConverter() . 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: 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 2
Source File: AmqpContextConfig.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
/**
 * {@link RabbitTemplate}のインスタンスをDIコンテナに登録します。
 * @return {@link RabbitTemplate}のインスタンス
 */
@Bean
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate template = new RabbitTemplate();
    template.setConnectionFactory(factory());
    template.setMessageConverter(converter());
    template.setRetryTemplate(retryTemplate());
    configure(template);
    return template;
}
 
Example 3
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 4
Source File: RabbitMqConfiguration.java    From Hands-On-High-Performance-with-Spring-5 with MIT License 5 votes vote down vote up
@Bean
public RabbitTemplate rabbitTemplate() {
	RabbitTemplate template = new RabbitTemplate(connectionFactory());
	template.setRoutingKey(ROUTING_KEY);
	template.setExchange(RABBIT_MESSAGE_EXCHANGE);
	template.setMessageConverter(messageConverter());
	return template;
}
 
Example 5
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 6
Source File: RabbitmqFactory.java    From shine-mq with Apache License 2.0 5 votes vote down vote up
private RabbitmqFactory(MqProperties config) {
    Objects.requireNonNull(config, "The RabbitmqProperties is empty.");
    this.config = config;
    this.rabbit = config.getRabbit();
    rabbitAdmin = new RabbitAdmin(rabbitConnectionFactory);
    rabbitTemplate = new RabbitTemplate(rabbitConnectionFactory);
    rabbitTemplate.setMessageConverter(serializerMessageConverter);
    if (config.getDistributed().isTransaction()) {
        setRabbitTemplateForDis(config);
    }
    template = new RabbitmqTemplate(this, rabbitTemplate, serializerMessageConverter);
}
 
Example 7
Source File: RabbitConfigurer.java    From bird-java with MIT License 5 votes vote down vote up
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate template = new RabbitTemplate(connectionFactory());
    template.setMessageConverter(messageConverter());

    return template;
}
 
Example 8
Source File: ScrapingResultConsumerConfiguration.java    From scraping-microservice-java-python-rabbitmq with Apache License 2.0 5 votes vote down vote up
@Bean
public RabbitTemplate rabbitTemplate() {
	RabbitTemplate template = new RabbitTemplate(connectionFactory());
	template.setRoutingKey(this.scrapingResultQueue);
	template.setQueue(this.scrapingResultQueue);
       template.setMessageConverter(jsonMessageConverter());
	return template;
}
 
Example 9
Source File: RabbitMessageSender.java    From common-project with Apache License 2.0 5 votes vote down vote up
public RabbitMessageSender(RabbitTemplate rabbitTemplate, RedisService redisService) {
    rabbitTemplate.setMessageConverter(new ProtobufMessageConverter());
    RabbitMessagingTemplate rabbitMessagingTemplate = new RabbitMessagingTemplate();
    rabbitMessagingTemplate.setRabbitTemplate(rabbitTemplate);
    this.rabbitTemplate = rabbitTemplate;
    this.redisService = redisService;
}
 
Example 10
Source File: TaskProducerConfiguration.java    From scraping-microservice-java-python-rabbitmq with Apache License 2.0 5 votes vote down vote up
@Bean
public RabbitTemplate rabbitTemplate()
{
    RabbitTemplate template = new RabbitTemplate(connectionFactory());
    template.setRoutingKey(this.tasksQueue);
    template.setQueue(this.tasksQueue);
    template.setMessageConverter(jsonMessageConverter());
    return template;
}
 
Example 11
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 12
Source File: AmqpConfig.java    From demo_springboot_rabbitmq with Apache License 2.0 5 votes vote down vote up
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
//必须是prototype类型
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate template = new RabbitTemplate(this.connectionFactory());
    template.setMessageConverter(this.jsonMessageConverter());
    template.setMandatory(true);
    return template;
}
 
Example 13
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 14
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 15
Source File: RabbitMqConfiguration.java    From shipping with Apache License 2.0 4 votes vote down vote up
@Bean
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate template = new RabbitTemplate(connectionFactory());
    template.setMessageConverter(jsonMessageConverter());
    return template;
}
 
Example 16
Source File: RabbitTemplateConfig.java    From spring-boot-tutorials with Apache License 2.0 4 votes vote down vote up
@Bean
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate consumerRabbitTemplate = new RabbitTemplate(connectionFactory());
    consumerRabbitTemplate.setMessageConverter(jackson2JsonMessageConverter());
    return consumerRabbitTemplate;
}
 
Example 17
Source File: RabbitTemplateConfig.java    From spring-boot-tutorials with Apache License 2.0 4 votes vote down vote up
@Bean
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate consumerRabbitTemplate = new RabbitTemplate(connectionFactory());
    consumerRabbitTemplate.setMessageConverter(jackson2JsonMessageConverter());
    return consumerRabbitTemplate;
}
 
Example 18
Source File: RabbitConfiguration.java    From JuniperBot with GNU General Public License v3.0 4 votes vote down vote up
@Bean
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate template = new RabbitTemplate(connectionFactory());
    template.setMessageConverter(messageConverter());
    return template;
}
 
Example 19
Source File: RabbitWithRabbitTemplateConfig.java    From java-spring-rabbitmq with Apache License 2.0 4 votes vote down vote up
static RabbitTemplate configureRabbitTemplate(RabbitTemplate rabbitTemplate) {
  SimpleMessageConverter messageConverter = createMessageConverter();
  rabbitTemplate.setMessageConverter(messageConverter);
  rabbitTemplate.setReplyTimeout(RABBIT_TEMPLATE_REPLY_TIMEOUT_MILLIS);
  return rabbitTemplate;
}