SpringBoot的RabbitMQ消息队列: 一、消息发送接收第一印象

1.在原来项目的基础上.

修改pom.xml配置,导入jar包

增加配置

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2.

修改系统配置文件application.properties,编制RabbitMQ的链接参数

spring.application.name=rabbitmq-hello
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456


ps:这里特别注意,mq的账号和密码都正常,还提示连接不上,这个时候就需要注意改账号的权限问题.

在打开localhost:15672 使用上述配置的账号密码登陆,进入admin选项卡,查看是否有权限,

Can access virtual hosts
 看这个是否有赋予对应的权限.....没有则需要加上,不然是访问不到的.

 

编制RabbitMQ配置、发送、接受的代码

1、编写配置文件类

在com.example包中增加类,名称为HelloRabbitConfig,并修改代码为

[java]  view plain  copy
  1. package com.example;  
  2.   
  3. import org.springframework.amqp.core.Queue;  
  4. import org.springframework.context.annotation.Bean;  
  5. import org.springframework.context.annotation.Configuration;  
  6.   
  7. @Configuration  
  8. public class HelloRabbitConfig {  
  9.   
  10.     @Bean  
  11.     public Queue helloQueue() {  
  12.         return new Queue("hello");  
  13.     }  
  14. }  

2、编写发送消息类

在com.example包中增加类,名称为HelloSender,并修改代码为

[java]  view plain  copy
  1. package com.example;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.slf4j.Logger;  
  6. import org.slf4j.LoggerFactory;  
  7. import org.springframework.amqp.core.AmqpTemplate;  
  8. import org.springframework.beans.factory.annotation.Autowired;  
  9. import org.springframework.stereotype.Component;  
  10.   
  11. @Component  
  12. public class HelloSender {  
  13.   
  14.     protected static Logger logger=LoggerFactory.getLogger(HelloSender.class);   
  15.       
  16.     @Autowired  
  17.     private AmqpTemplate rabbitTemplate;  
  18.   
  19.     public String send(String name) {  
  20.         String context = "hello "+name+" --" + new Date();  
  21.         logger.debug("HelloSender: " + context);  
  22.         this.rabbitTemplate.convertAndSend("hello", context);  
  23.         return context;  
  24.     }  
  25. }  

3、编写接受消息类

在com.example包中增加类,名称为HelloReceiver,并修改代码为

[html]  view plain  copy
  1. package com.example;  
  2.   
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5. import org.springframework.amqp.rabbit.annotation.RabbitHandler;  
  6. import org.springframework.amqp.rabbit.annotation.RabbitListener;  
  7. import org.springframework.stereotype.Component;  
  8.   
  9. @Component  
  10. @RabbitListener(queues = "hello")  
  11. public class HelloReceiver {  
  12.     protected static Logger logger = LoggerFactory.getLogger(HelloReceiver.class);  
  13.   
  14.     @RabbitHandler  
  15.     public void process(String hello) {  
  16.         logger.debug("HelloReceiver : " + hello);  
  17.     }  
  18. }  

4、编写RestController,调用发送消息

在com.example包中增加类,名称为HelloController,并修改代码为

[java]  view plain  copy
  1. package com.example;  
  2.   
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.web.bind.annotation.PathVariable;  
  7. import org.springframework.web.bind.annotation.RequestMapping;  
  8. import org.springframework.web.bind.annotation.RestController;  
  9.   
  10. @RestController   
  11. public class HelloController {  
  12.     protected static Logger logger=LoggerFactory.getLogger(HelloController.class);   
  13.       
  14.     @Autowired  
  15.         private HelloSender helloSender;  
  16.       
  17.     @RequestMapping("/send/{name}")  
  18.     public String helloworld(@PathVariable String name) {  
  19.         return helloSender.send(name);  
  20.     }  
  21. }  

5、运行测试

A、启动Docker中容器rabbitmq5672。docker start rabbitmq5672   (如果已启动,则忽略)

B、启动工程。在工程所在的文件夹打开命令行,且在命令行中执行 mvn spring-boot:run

C、在浏览器中输入 http://localhost:8080/send/上帝

浏览器中显示

控制台中显示,成功发送、接收消息

三、小结

1、rabbitmq连接

1.1 springboot,在启动时,根据系统配置文件的参数建立一个rabbitmq连接。参看控制台日志:

 (MBeanExporter.java:431)- Registering beans for JMX exposure on startup

(MBeanExporter.java:916)- Bean with name 'rabbitConnectionFactory' has been autodetected for JMX exposure

 (MBeanExporter.java:678)- Located managed bean 'rabbitConnectionFactory': registering with JMX server as MBean [org.springframework.amqp.rabbit.connection:name=rabbitConnectionFactory,type=CachingConnectionFactory]

(DefaultLifecycleProcessor.java:343)- Starting beans in phase -2147482648

(DefaultLifecycleProcessor.java:343)- Starting beans in phase 2147483647

(AbstractConnectionFactory.java:347)- Created new connection: SimpleConnection@7075a8f1 [delegate=amqp://test@127.0.0.1:5672/, localPort= 62881]

1.2、在springboot启动之后,查看rabbitmq(http://localhost:15672/)的connections,也可以查看到。当然,你也可以停止springboot去看链接(停止之后,当然就没有链接了).
1.3、把logbak.xml的root的等级修改为debug,你会发现监听在不断的链接reabitmq;它就是一个心跳。通过这个心跳,rabbitmq也知道存在哪些监听器在监听那个队列。

2、配置文件

在配置文件中,仅仅配置了一个名为hello的队列。以后发送、接收都与这个队列相关。

3、发送消息

3.1、发送消息使用模板机制,用springboot自动注入的rabbitTemplate,它已经封装好链接、管道、转换等。

3.2、消息转换和发送

void convertAndSend(String routingKey, Object message) throws AmqpException;

它的注释是:Convert a Java object to an Amqp {@link Message} and send it to a default exchange with a specific routing key.

转换一个Java对象为Amqp消息,然后再用缺省的交换机指定路由键发送消息。

4、接受消息

4.1、监听消息

在类上声明@RabbitListener(queues = "hello"),表示监听hello队列

4.2、消息处理句柄

在接收处理消息的方法上声明@RabbitHandler

Annotation that marks a method to be the target of a Rabbit message  listener within a class that is annotated with {@link RabbitListener}.

5、印象

整个工程比较简单,没有什么特殊的配置,仅仅三个类文件即可实现消息的发送和接受。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值