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@Slf4jpublic 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; }}
时间: 2024-10-14 16:34:54