码迷,mamicode.com
首页 > 其他好文 > 详细

rabbitmq配置

时间:2017-06-28 21:45:25      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:mic   void   actor   json   val   ted   uid   getc   nfa   

package me.aizou.tour.guide.config;

import lombok.extern.slf4j.Slf4j;
import me.aizou.tour.guide.domain.event.AnnounceEventMessage;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.ClassMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

/**
* Created by Michael on 2017/5/19.
*/
@EnableRabbit
@Configuration
@Slf4j
public class AmqpConfig {

private final static String QUEUE = "tour-guide-api.announce";
private final static String EXCHANGE = "zouzou.public";

@Value("${spring.rabbitmq.host}")
private String host;

@Value("${spring.rabbitmq.username}")
private String username;

@Value("${spring.rabbitmq.password}")
private String password;

@Bean
 public Queue queue() {
return new Queue(QUEUE, true);
}

@Bean
 public TopicExchange exchange() {
return new TopicExchange(EXCHANGE);
}

// @Bean
// public Binding binding(Queue queue, TopicExchange exchange) {
// return BindingBuilder
// .bind(queue)
// .to(exchange)
// .with("announce.guide");
// }

 @Bean
 List<Binding> bindings() {

return Arrays.asList(
BindingBuilder.bind(queue()).to(exchange()).with("announce"),
BindingBuilder.bind(queue()).to(exchange()).with("announce.guide"));
}

@Bean
 public RabbitTemplate rabbitTemplate() {
//没有指定默认queueName,在接收的监听器指定
 RabbitTemplate template = new RabbitTemplate(connectionFactory());
template.setMessageConverter(new Jackson2JsonMessageConverter());
return template;
}

@Bean("rabbitListenerContainerFactory")
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setMessageConverter(customConverter());
return factory;
}

@Bean
 public Jackson2JsonMessageConverter customConverter() {
Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
ClassMapper classMapper = new ClassMapper() {

@Override
 public Class<?> toClass(MessageProperties properties) {

if(properties.getConsumerQueue().equals("tour-guide-api.announce")) {
return AnnounceEventMessage.class;
}
throw new RuntimeException(properties.getConsumerQueue()+"不支持");
}

@Override
 public void fromClass(Class<?> clazz, MessageProperties properties) {
throw new UnsupportedOperationException("this mapper is only for inbound, do not use for send message");
}
};
converter.setClassMapper(classMapper);
return converter;
}

@Bean
 public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
return connectionFactory;
}
}

rabbitmq配置

标签:mic   void   actor   json   val   ted   uid   getc   nfa   

原文地址:http://www.cnblogs.com/mkxzy/p/7091394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!