RabbitMQ
【黑马程序员RabbitMQ全套教程,rabbitmq消息中间件到实战】
文章目录
- RabbitMQ
- 第一天 基础
- 5 Spring 整合RabbitMQ
- 5.2 Spring 整合 RabbitMQ【消费者】
- 5.2.1 消费者
- 5.3 小结
第一天 基础
5 Spring 整合RabbitMQ
5.2 Spring 整合 RabbitMQ【消费者】
5.2.1 消费者
之前我们已经完成了 生活者的代码 编写
并且往MQ 中搞了一些 消息进去
现在 要做的就是消费 它们 了
【消费者】
坐标我们已经导过了
【添加配置文件】
rabbitmq.properties
rabbitmq.host=xxxxxxxxxxx # 自己的服务器IP
rabbitmq.port=5672
rabbitmq.username=dingjiaxiong
rabbitmq.password=12345
rabbitmq.virtual-host=/ding
spring-rabbitmq-consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<!--加载配置文件-->
<context:property-placeholder location="classpath:rabbitmq.properties"/>
<!-- 定义rabbitmq connectionFactory -->
<rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
port="${rabbitmq.port}"
username="${rabbitmq.username}"
password="${rabbitmq.password}"
virtual-host="${rabbitmq.virtual-host}"/>
<bean id="springQueueListener" class="com.dingjiaxiong.rabbitmq.listener.SpringQueueListener"/>
<!-- <bean id="fanoutListener1" class="com.dingjiaxiong.rabbitmq.listener.FanoutListener1"/>-->
<!-- <bean id="fanoutListener2" class="com.dingjiaxiong.rabbitmq.listener.FanoutListener2"/>-->
<!-- <bean id="topicListenerStar" class="com.dingjiaxiong.rabbitmq.listener.TopicListenerStar"/>-->
<!-- <bean id="topicListenerWell" class="com.dingjiaxiong.rabbitmq.listener.TopicListenerWell"/>-->
<!-- <bean id="topicListenerWell2" class="com.dingjiaxiong.rabbitmq.listener.TopicListenerWell2"/>-->
<rabbit:listener-container connection-factory="connectionFactory" auto-declare="true">
<rabbit:listener ref="springQueueListener" queue-names="spring_queue"/>
<!-- <rabbit:listener ref="fanoutListener1" queue-names="spring_fanout_queue_1"/>-->
<!-- <rabbit:listener ref="fanoutListener2" queue-names="spring_fanout_queue_2"/>-->
<!-- <rabbit:listener ref="topicListenerStar" queue-names="spring_topic_queue_star"/>-->
<!-- <rabbit:listener ref="topicListenerWell" queue-names="spring_topic_queue_well"/>-->
<!-- <rabbit:listener ref="topicListenerWell2" queue-names="spring_topic_queue_well2"/>-->
</rabbit:listener-container>
</beans>
【监听类】
package com.dingjiaxiong.rabbitmq.listener;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
/**
* ClassName: SpringQueueListener
* date: 2022/11/16 16:02
*
* @author DingJiaxiong
*/
public class SpringQueueListener implements MessageListener {
@Override
public void onMessage(Message message) {
//打印消息
System.out.println(new String(message.getBody()));
}
}
【编写测试】
package com.dingjiaxiong;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* ClassName: ConsumerTest
* date: 2022/11/16 16:06
*
* @author DingJiaxiong
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-consumer.xml")
public class ConsumerTest {
@Test
public void test1(){
while (true){
}
}
}
直接运行
OK
已经拿到了 ,这是简单模式的消息
OK,把其他监听类 也搞定就行
【老师留作了 课后作业,笔者就快速搞一下】
这个地方,感觉IDEA 也是抽风,都运行成功 了,还飘红
【fanout】
package com.dingjiaxiong.rabbitmq.listener;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
/**
* ClassName: FanoutListener1
* date: 2022/11/16 16:11
*
* @author DingJiaxiong
*/
public class FanoutListener1 implements MessageListener {
@Override
public void onMessage(Message message) {
System.out.println("FanoutListener1 ============");
System.out.println(new String(message.getBody()));
System.out.println("=============================");
}
}
package com.dingjiaxiong.rabbitmq.listener;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
/**
* ClassName: FanoutListener2
* date: 2022/11/16 16:12
*
* @author DingJiaxiong
*/
public class FanoutListener2 implements MessageListener {
@Override
public void onMessage(Message message) {
System.out.println("FanoutListener2 ============");
System.out.println(new String(message.getBody()));
System.out.println("=============================");
}
}
OK,直接再次启动测试
没毛病
【还有一个 topic 模式】
package com.dingjiaxiong.rabbitmq.listener;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
/**
* ClassName: TopicListenerStar
* date: 2022/11/16 16:14
*
* @author DingJiaxiong
*/
public class TopicListenerStar implements MessageListener {
@Override
public void onMessage(Message message) {
System.out.println("TopicListenerStar ============");
System.out.println(new String(message.getBody()));
System.out.println("=============================");
}
}
package com.dingjiaxiong.rabbitmq.listener;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
/**
* ClassName: TopicListenerWell
* date: 2022/11/16 16:14
*
* @author DingJiaxiong
*/
public class TopicListenerWell implements MessageListener {
@Override
public void onMessage(Message message) {
System.out.println("TopicListenerWell ============");
System.out.println(new String(message.getBody()));
System.out.println("=============================");
}
}
package com.dingjiaxiong.rabbitmq.listener;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
/**
* ClassName: TopicListenerWell2
* date: 2022/11/16 16:15
*
* @author DingJiaxiong
*/
public class TopicListenerWell2 implements MessageListener {
@Override
public void onMessage(Message message) {
System.out.println("TopicListenerWell2 ============");
System.out.println(new String(message.getBody()));
System.out.println("=============================");
}
}
OK,三个监听器,监听三个队列
直接重新运行测试类
没毛病,查看一下管控台
所有消息消费 完毕
5.3 小结
- 使用 Spring 整合 RabbitMQ 将组件全部使用配置方式实现,简化编码
- Spring 提供 RabbitTemplate 简化发送消息 API
- 使用监听机制简化消费者编码