RabbitMq 集成 spring boot 消息队列 入门Demo

简介:

spring boot 集成 RabbitMq还是很方便的。现在来一个简单的例子来集成rabbitmq。入门demo。

主要概念:

其中比较重要的概念有 4 个,分别为:虚拟主机,交换机,队列,和绑定。


虚拟主机:一个虚拟主机持有一组交换机、队列和绑定。为什么需要多个虚拟主机呢?很简单,RabbitMQ当中,用户只能在虚拟主机的粒度进行权限控制。 因此,如果需要禁止A组访问B组的交换机/队列/绑定,必须为A和B分别创建一个虚拟主机。每一个RabbitMQ服务器都有一个默认的虚拟主机“/”。

交换机:Exchange 用于转发消息,但是它不会做存储 ,如果没有 Queue bind 到 Exchange 的话,它会直接丢弃掉 Producer 发送过来的消息。 这里有一个比较重要的概念:路由键 。消息到交换机的时候,交互机会转发到对应的队列中,那么究竟转发到哪个队列,就要根据该路由键。

绑定:也就是交换机需要和队列相绑定,这其中如上图所示,是多对多的关系。


首先是配制文件。

1
2
3
4
5
#spring.application.name=spring-boot-rabbitmq
spring.rabbitmq.host= 127.0 . 0.1
spring.rabbitmq.port= 5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest


发送者:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package  com.basic.rabbitmq.send;
 
import  com.basic.rabbitmq.configuration.RabbitMqConfig2;
import  org.springframework.amqp.core.AmqpTemplate;
import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.stereotype.Component;
import  org.springframework.stereotype.Service;
 
import  java.util.Date;
 
/**
  * Created by sdc on 2017/6/17.
  */
@Service ( "helloSender" )
public  class  HelloSender {
 
     @Autowired
     private  AmqpTemplate amqpTemplate;
 
//    private Rabbitt
 
     public  void  send() {
         String contenxt =  "order_queue_message" ;
         this .amqpTemplate.convertAndSend(RabbitMqConfig2.QUEUE_EXCHANGE_NAME, "order_queue_routing" ,contenxt);
//        this.amqpTemplate.conver
     }
 
}


配制信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package  com.basic.rabbitmq.configuration;
 
import  com.basic.rabbitmq.receiver.Receiver;
import  com.rabbitmq.client.Channel;
import  com.rabbitmq.client.ConfirmListener;
import  org.springframework.amqp.core.*;
import  org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import  org.springframework.amqp.rabbit.connection.ConnectionFactory;
import  org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import  org.springframework.amqp.rabbit.listener.MessageListenerContainer;
import  org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import  org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import  org.springframework.context.annotation.Bean;
import  org.springframework.context.annotation.Configuration;
 
import  java.io.IOException;
 
/**
  * Created by sdc on 2017/6/17.
  */
@Configuration
public  class  RabbitMqConfig2 {
 
     public  static  final  String QUEUE_NAME =  "order_queue" ;
 
     public  static  final  String QUEUE_EXCHANGE_NAME =  "topic_exchange_new" ;
 
     public  static  final   String routing_key =  "order_queue_routing" ;
 
     @Bean
     public  Queue queue() {
         //是否持久化
         boolean  durable =  false ;
         //仅创建者可以使用该队列,断开后自动删除
         boolean  exclusive =  false ;
         //当所有消费者都断开连接后,是否删除队列
         boolean  autoDelete =  false ;
         return  new  Queue(QUEUE_NAME, durable, exclusive, autoDelete);
     }
 
     @Bean
     public  TopicExchange exchange() {
         //是否持久化
         boolean  durable =  false ;
         //当所有消费者都断开连接后,是否删除队列
         boolean  autoDelete =  false ;
         return   new  TopicExchange(QUEUE_EXCHANGE_NAME, durable, autoDelete);
     }
 
     @Bean
     public  Binding binding() {
         return  BindingBuilder.bind(queue()).to(exchange()).with(routing_key);
     }
 
     @Bean
     public  ConnectionFactory connectionFactory() {
         CachingConnectionFactory connectionFactory =  new  CachingConnectionFactory( "127.0.0.1" , 5672 );
 
         connectionFactory.setUsername( "admin" );
         connectionFactory.setPassword( "admin" );
         connectionFactory.setVirtualHost( "/" );
         /** 如果要进行消息回调,则这里必须要设置为true */
         connectionFactory.setPublisherConfirms( true );  // 必须要设置
//        connectionFactory.setPublisherReturns();
         return  connectionFactory;
     }
 
     @Bean
     SimpleMessageListenerContainer container() {
 
         SimpleMessageListenerContainer container =  new  SimpleMessageListenerContainer();
         container.setConnectionFactory(connectionFactory());
//        container.setQueueNames(QUEUE_NAME);
         container.setQueues(queue());
         container.setAcknowledgeMode(AcknowledgeMode.MANUAL);  //设置确认模式手工确认
         container.setMessageListener( new  ChannelAwareMessageListener() {
             @Override
             public  void  onMessage(Message message, Channel channel)  throws  Exception {
                 byte [] body = message.getBody();
                 System.out.println( "收到消息 : "  new  String(body));
                 channel.queueDeclare(QUEUE_NAME,  true false false null );
//                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); //确认消息成功消费
//                channel.basicAck(); //应答
//                channel.basicReject();//拒绝
//                channel.basicRecover(); //恢复
//                channel.basicQos();
//                channel.addConfirmListener(new ConfirmListener() {
//                    @Override
//                    public void handleAck(long deliveryTag, boolean multiple) throws IOException {
//                        //失败重发
//                    }
//
//                    @Override
//                    public void handleNack(long deliveryTag, boolean multiple) throws IOException {
//                        //确认ok
//                    }
//                });
             }
         });
         return   container;
     }
 
//    @Bean
//    MessageListenerAdapter listenerAdapter(Receiver receiver) {
//        return new MessageListenerAdapter(receiver, "receiveMessage");
//    }
 
}


测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package  com.rabbit.test;
 
import  com.basic.rabbitmq.send.HelloSender;
import  com.basic.system.Application;
import  org.junit.Test;
import  org.junit.runner.RunWith;
import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.boot.test.context.SpringBootTest;
import  org.springframework.test.context.junit4.SpringRunner;
 
import  javax.annotation.Resource;
 
/**
  * Created by sdc on 2017/6/17.
  */
@RunWith(SpringRunner. class )
@SpringBootTest(classes = Application. class )
public  class  RabbitMqTest {
 
     @Autowired
     public  HelloSender helloSender;
 
     @Test
     public  void helloword() throws  Exception {
         helloSender.send();
     }
 
 
}


这只是一个demo,学习的时候会测试各种的事情,在这基础上更改就可以了,心中的疑虑测试没了就可以写一些项目了。


本文转自 豆芽菜橙 51CTO博客,原文链接:http://blog.51cto.com/shangdc/1944218


相关实践学习
RocketMQ一站式入门使用
从源码编译、部署broker、部署namesrv,使用java客户端首发消息等一站式入门RocketMQ。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
相关文章
|
1月前
|
消息中间件 存储 监控
RabbitMQ:分布式系统中的高效消息队列
RabbitMQ:分布式系统中的高效消息队列
|
2月前
|
消息中间件 Java Maven
一文搞懂Spring Boot整合RocketMQ
一文搞懂Spring Boot整合RocketMQ
91 0
|
2月前
|
存储 安全 Java
SpringBoot搭建Spring Security 入门
SpringBoot搭建Spring Security 入门
112 0
|
2月前
|
消息中间件 存储 监控
搭建消息时光机:深入探究RabbitMQ_recent_history_exchange在Spring Boot中的应用【RabbitMQ实战 二】
搭建消息时光机:深入探究RabbitMQ_recent_history_exchange在Spring Boot中的应用【RabbitMQ实战 二】
32 1
|
2月前
|
消息中间件 监控 Java
Spring Boot中的RabbitMQ死信队列魔法:从异常到延迟,一网打尽【RabbitMQ实战 一】
Spring Boot中的RabbitMQ死信队列魔法:从异常到延迟,一网打尽【RabbitMQ实战 一】
58 0
|
2月前
|
消息中间件 NoSQL Java
Redis Streams在Spring Boot中的应用:构建可靠的消息队列解决方案【redis实战 二】
Redis Streams在Spring Boot中的应用:构建可靠的消息队列解决方案【redis实战 二】
187 1
|
10天前
|
前端开发 Java 数据库连接
Spring系列文章1:Spring入门程序
Spring系列文章1:Spring入门程序
|
1月前
|
消息中间件 Java
springboot整合消息队列——RabbitMQ
springboot整合消息队列——RabbitMQ
74 0
|
7天前
|
XML Java 数据格式
从入门到精通:Spring基础注解的全面解析
从入门到精通:Spring基础注解的全面解析
26 2
从入门到精通:Spring基础注解的全面解析
|
12天前
|
消息中间件 存储 负载均衡
消息队列学习之RabbitMQ
【4月更文挑战第3天】消息队列学习之RabbitMQ,一种基于erlang语言开发的流行的开源消息中间件。
15 0

相关产品

  • 云消息队列 MQ