RabbitMQ 消息發送、消息監聽


文章轉載自:
https://www.cnblogs.com/wangyaobk/articles/7885052.html

 

本文是基於spring-rabbit中間件來實現消息的發送接受功能

項目搭建采用spring-boot:

pom.xml如下:      

1  <!--AMQP-->
2         <dependency>
3             <groupId>org.springframework.boot</groupId>
4             <artifactId>spring-boot-starter-amqp</artifactId>
5         </dependency> 

 

rabbitMQ配置:

1 rabbitmq.host=
2 rabbitmq.userName=
3 rabbitmq.password=
4 rabbitmq.port=5672 5 rabbitmq.sendQueueName=test 6 rabbitmq.receiverQueueName=test

配置類:

 

復制代碼
 1 @Configuration
 2 @PropertySource("classpath:rabbitmq.properties")
 3 public class RabbitMQConfiguration {
 4     @Value("${rabbitmq.host}")
 5     private String host;
 6     @Value("${rabbitmq.userName}")
 7     private String userName;
 8     @Value("${rabbitmq.password}")
 9     private String password;
10     @Value("${rabbitmq.port}")
11     private Integer port;
12     @Value("${rabbitmq.sendQueueName}")
13     private String sendQueueName;
14     @Value("${rabbitmq.receiverQueueName}")
15     private String receiverQueueName;
16 
17     //此處省略getter ,setter
}
復制代碼

  

 Amqp注冊與監聽:

 

復制代碼
/**
 * /**
 * Amqp消息注冊監聽
 */
@Configuration
public class AmqpListener {


    @Autowired
    private RabbitMQConfiguration rabbitMQConfiguration;

    @Bean
    public MessageListener exampleListener() {
        return new MessageListener() {
            public void onMessage(Message message) {
                //amqpReceiver.onMessage(message);
                System.out.print("接收消息:" + new String(message.getBody()));
            }
        };
    }

    @Bean
    public SimpleMessageListenerContainer messageListenerContainer() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(rabbitConnectionFactory());
        //設置監聽的隊列名,數組[]"abc","test4"
        String[] types = {rabbitMQConfiguration.getReceiverQueueName()};
        container.setQueueNames(types);
        container.setMessageListener(exampleListener());
        return container;
    }

    @Bean
    public ConnectionFactory rabbitConnectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitMQConfiguration.getHost());
        connectionFactory.setUsername(rabbitMQConfiguration.getUserName());
        connectionFactory.setPassword(rabbitMQConfiguration.getPassword());
        connectionFactory.setPort(rabbitMQConfiguration.getPort());
        return connectionFactory;
    }

}
復制代碼

 

 下面介紹簡單的消息發送:

  

復制代碼
 1 /**
 2  * 發送消息mq隊列
 3  */
 4 public class AmqpSend {
 5 
 6     private static Logger log = LoggerFactory.getLogger(AmqpSend.class);
 7 
 8     private AmqpTemplate rabbitTemplate;
 9     private RabbitMQConfiguration rabbitMQConfiguration;
10     private String context;
11 
12     public AmqpSend(String context, RabbitMQConfiguration rabbitMQConfiguration, AmqpTemplate rabbitTemplate) {
13         this.context = context;
14         this.rabbitMQConfiguration = rabbitMQConfiguration;
15         this.rabbitTemplate = rabbitTemplate;
16     }
17 
18     public void send() {
19         try {
20          this.rabbitTemplate.convertAndSend(rabbitMQConfiguration.getSendQueueName(), context);
21             log.info("消息發送成功");
22         } catch (Exception e) {
23             log.error(e.getMessage());
24             log.error("消息發送失敗");
25         }
26         
27     }
28 }
復制代碼

 

 然后登錄rabbitmq web界面:

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM