|
| 1 | +package com.ttianjun.config; |
| 2 | + |
| 3 | +import com.ttianjun.listen.ListenDemo; |
| 4 | +import org.springframework.amqp.core.*; |
| 5 | +import org.springframework.amqp.rabbit.connection.ConnectionFactory; |
| 6 | +import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; |
| 7 | +import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.context.annotation.Bean; |
| 10 | +import org.springframework.context.annotation.Configuration; |
| 11 | + |
| 12 | +/** |
| 13 | + * @user tianjun |
| 14 | + * @date 16/6/24 |
| 15 | + */ |
| 16 | +@Configuration |
| 17 | +public class MessageConfig { |
| 18 | + @Value("${default-exchange}") |
| 19 | + private String exchange; |
| 20 | + |
| 21 | + private final String queueName="queue-fanout"; |
| 22 | + @Bean |
| 23 | + Queue queue() { |
| 24 | + return new Queue(queueName, false); |
| 25 | + } |
| 26 | + |
| 27 | + @Bean |
| 28 | + FanoutExchange exchange() { |
| 29 | + return new FanoutExchange(exchange); |
| 30 | + } |
| 31 | + |
| 32 | + @Bean |
| 33 | + Binding binding(Queue queue, FanoutExchange exchange) { |
| 34 | + return BindingBuilder.bind(queue).to(exchange); |
| 35 | + } |
| 36 | + |
| 37 | + @Bean |
| 38 | + SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, ListenDemo listenDemo) { |
| 39 | + SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); |
| 40 | + container.setConnectionFactory(connectionFactory); |
| 41 | + container.setQueueNames(queueName); |
| 42 | + container.setMessageListener(new MessageListenerAdapter(listenDemo,"handleMessage")); |
| 43 | + return container; |
| 44 | + } |
| 45 | +} |
0 commit comments