spring 整合 RabbitMQ 3.9.11

rabbitmq 3.9.11

创建工程

不管用什么办法创建出一个空的工程出来,或者在已有工程里面创建一个新的module,下面只说创建新module的方法。

  在菜单里选择Project Structure
spring 整合 RabbitMQ 3.9.11 - 图1
先点加号,再点New Module
spring 整合 RabbitMQ 3.9.11 - 图2
然后直接下一步
spring 整合 RabbitMQ 3.9.11 - 图3
然后把Module名字,所在位置都填好之后直接finish
spring 整合 RabbitMQ 3.9.11 - 图4
在idea中新建Module都是这么操作。

maven配置

直接把下面的配置信息根据需要粘贴到自己的pom.xml里面。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.itheima</groupId>
  7. <artifactId>spring-rabbitmq</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <dependencies>
  10. <!--spring 上下文-->
  11. <dependency>
  12. <groupId>org.springframework</groupId>
  13. <artifactId>spring-context</artifactId>
  14. <version>5.2.10.RELEASE</version>
  15. </dependency>
  16. <!--spring整合amqp-->
  17. <dependency>
  18. <groupId>org.springframework.amqp</groupId>
  19. <artifactId>spring-rabbit</artifactId>
  20. <version>2.1.8.RELEASE</version>
  21. </dependency>
  22. <!--测试-->
  23. <dependency>
  24. <groupId>junit</groupId>
  25. <artifactId>junit</artifactId>
  26. <version>4.12</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework</groupId>
  30. <artifactId>spring-test</artifactId>
  31. <version>5.2.10.RELEASE</version>
  32. </dependency>
  33. </dependencies>
  34. <build>
  35. <plugins>
  36. <plugin>
  37. <groupId>org.apache.maven.plugins</groupId>
  38. <artifactId>maven-compiler-plugin</artifactId>
  39. <version>3.8.0</version>
  40. <configuration>
  41. <source>1.8</source>
  42. <target>1.8</target>
  43. </configuration>
  44. </plugin>
  45. </plugins>
  46. </build>
  47. </project>

提醒一下:pom.xml的文件在下面这个位置
spring 整合 RabbitMQ 3.9.11 - 图5

resource 配置文件

以下三个文件直接复制下面的代码就可以,把rabbitmq配置改成自己的就好了,bean什么的写成自己的
spring 整合 RabbitMQ 3.9.11 - 图6

rabbitmq.properties

  1. rabbitmq.host=127.5.19.118 (服务器ip)
  2. rabbitmq.port=5672 rabbitmq端口)
  3. rabbitmq.username=admin rabbitmq用户名)
  4. rabbitmq.password=11111 rabbitmq密码)
  5. rabbitmq.virtual-host=/itcast rabbitmq虚拟机)

spring-rabbitmq-consumer.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:rabbit="http://www.springframework.org/schema/rabbit"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. https://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/rabbit
  11. http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
  12. <!--加载配置文件-->
  13. <context:property-placeholder location="classpath:rabbitmq.properties"/>
  14. <!-- 定义rabbitmq connectionFactory -->
  15. <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
  16. port="${rabbitmq.port}"
  17. username="${rabbitmq.username}"
  18. password="${rabbitmq.password}"
  19. virtual-host="${rabbitmq.virtual-host}"/>
  20. <bean id="springQueueListener" class="com.itheima.springRabbitmq.consumer.SpringQueueListener"/>
  21. <!-- <bean id="fanoutListener1" class="com.itheima.springRabbitmq.consumer.FanoutListener1"/>-->
  22. <!-- <bean id="fanoutListener2" class="com.itheima.springRabbitmq.consumer.FanoutListener2"/>-->
  23. <!-- <bean id="topicListenerStar" class="com.itheima.springRabbitmq.consumer.TopicListenerStar"/>-->
  24. <!-- <bean id="topicListenerWell" class="com.itheima.springRabbitmq.consumer.TopicListenerWell"/>-->
  25. <!-- <bean id="topicListenerWell2" class="com.itheima.springRabbitmq.consumer.TopicListenerWell2"/>-->
  26. <rabbit:listener-container connection-factory="connectionFactory" auto-declare="true">
  27. <rabbit:listener ref="springQueueListener" queue-names="spring_queue"/>
  28. <!-- <rabbit:listener ref="fanoutListener1" queue-names="spring_fanout_queue_1"/>-->
  29. <!-- <rabbit:listener ref="fanoutListener2" queue-names="spring_fanout_queue_2"/>-->
  30. <!-- <rabbit:listener ref="topicListenerStar" queue-names="spring_topic_queue_star"/>-->
  31. <!-- <rabbit:listener ref="topicListenerWell" queue-names="spring_topic_queue_well"/>-->
  32. <!-- <rabbit:listener ref="topicListenerWell2" queue-names="spring_topic_queue_well2"/>-->
  33. </rabbit:listener-container>
  34. </beans>

spring-rabbitmq-producer.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:rabbit="http://www.springframework.org/schema/rabbit"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. https://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/rabbit
  11. http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
  12. <!--加载配置文件-->
  13. <context:property-placeholder location="classpath:rabbitmq.properties"/>
  14. <!-- 定义rabbitmq connectionFactory -->
  15. <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
  16. port="${rabbitmq.port}"
  17. username="${rabbitmq.username}"
  18. password="${rabbitmq.password}"
  19. virtual-host="${rabbitmq.virtual-host}"/>
  20. <!--定义管理交换机、队列-->
  21. <rabbit:admin connection-factory="connectionFactory"/>
  22. <!--定义持久化队列,不存在则自动创建;不绑定到交换机则绑定到默认交换机
  23. 默认交换机类型为direct,名字为:"",路由键为队列的名称-->
  24. <rabbit:queue id="spring_queue" name="spring_queue" auto-declare="true"/>
  25. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~广播;所有队列都能收到消息~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  26. <!--定义广播交换机中的持久化队列,不存在则自动创建-->
  27. <rabbit:queue id="spring_fanout_queue_1" name="spring_fanout_queue_1" auto-declare="true"/>
  28. <!--定义广播交换机中的持久化队列,不存在则自动创建-->
  29. <rabbit:queue id="spring_fanout_queue_2" name="spring_fanout_queue_2" auto-declare="true"/>
  30. <!--定义广播类型交换机;并绑定上述两个队列-->
  31. <rabbit:fanout-exchange id="spring_fanout_exchange" name="spring_fanout_exchange" auto-declare="true">
  32. <rabbit:bindings>
  33. <rabbit:binding queue="spring_fanout_queue_1"/>
  34. <rabbit:binding queue="spring_fanout_queue_2"/>
  35. </rabbit:bindings>
  36. </rabbit:fanout-exchange>
  37. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~通配符;*匹配一个单词,#匹配多个单词 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  38. <!--定义广播交换机中的持久化队列,不存在则自动创建-->
  39. <rabbit:queue id="spring_topic_queue_star" name="spring_topic_queue_star" auto-declare="true"/>
  40. <!--定义广播交换机中的持久化队列,不存在则自动创建-->
  41. <rabbit:queue id="spring_topic_queue_well" name="spring_topic_queue_well" auto-declare="true"/>
  42. <!--定义广播交换机中的持久化队列,不存在则自动创建-->
  43. <rabbit:queue id="spring_topic_queue_well2" name="spring_topic_queue_well2" auto-declare="true"/>
  44. <rabbit:topic-exchange id="spring_topic_exchange" name="spring_topic_exchange" auto-declare="true">
  45. <rabbit:bindings>
  46. <rabbit:binding pattern="heima.*" queue="spring_topic_queue_star"/> <!--星号只能匹配一个单词-->
  47. <rabbit:binding pattern="heima.#" queue="spring_topic_queue_well"/> <!--井号匹配多个单词-->
  48. <rabbit:binding pattern="itcast.#" queue="spring_topic_queue_well2"/>
  49. </rabbit:bindings>
  50. </rabbit:topic-exchange>
  51. <!--定义rabbitTemplate对象操作可以在代码中方便发送消息-->
  52. <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
  53. </beans>

Producer

为了能测试执行代码,就写在测试类里面了。

rabbitTemplate.convertAndSend("spring_fanout_exchange","","fanout消息已经发送了"); 当中的 spring_fanout_exchange 是绑定的上面 spring-rabbitmq-producer.xml 当中的对应的bean (<rabbit:fanout-exchange id="spring_fanout_exchange" name="spring_fanout_exchange" auto-declare="true">),有关exchange绑定 queue的配置都在spring-rabbitmq-producer.xml当中。

  1. package com.itheima.springRabbitmq;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.test.context.ContextConfiguration;
  7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  8. /**
  9. * @Author zhizekai
  10. * @Date 2022-1-11 15:48:45 15:48
  11. * @Version 2.0
  12. */
  13. @RunWith(SpringJUnit4ClassRunner.class)
  14. @ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
  15. public class ProducerTest {
  16. // 注入rabbitmqTemple
  17. @Autowired
  18. private RabbitTemplate rabbitTemplate;
  19. @Test
  20. public void testProducer() throws InterruptedException {
  21. Integer i = 0;
  22. while (true) {
  23. Thread.sleep(5 * 1000);
  24. i++;
  25. rabbitTemplate.convertAndSend("spring_queue","消息发送了"+i);
  26. }
  27. }
  28. @Test
  29. public void testFanout() {
  30. rabbitTemplate.convertAndSend("spring_fanout_exchange","","fanout消息已经发送了");
  31. }
  32. @Test
  33. public void testTopic() {
  34. rabbitTemplate.convertAndSend("spring_topic_exchange","heima.heeh.haha","topic消息已经发送了");
  35. }
  36. }

Consumer

这个代码是跑起来自动监听的 ,实现一下MessageListener这个类,重写onMessage方法,message.getBody()就是消息内容
spring 整合 RabbitMQ 3.9.11 - 图7

  1. package com.itheima.springRabbitmq.consumer;
  2. import org.springframework.amqp.core.Message;
  3. import org.springframework.amqp.core.MessageListener;
  4. /**
  5. * @Author zhizekai
  6. * @Date 2022-1-11 18:53:33 18:53
  7. * @Version 2.0
  8. */
  9. public class SpringQueueListener implements MessageListener {
  10. @Override
  11. public void onMessage(Message message) {
  12. System.out.println("收到的消息:"+new String(message.getBody()));
  13. // System.out.println(message.toString());
  14. }
  15. }

测试代码

  1. package com.itheima.springRabbitmq.consumer;
  2. import junit.framework.TestCase;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.test.context.ContextConfiguration;
  7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  8. /**
  9. * @Author zhizekai
  10. * @Date 19:01
  11. * @Version 2.0
  12. */
  13. @RunWith(SpringJUnit4ClassRunner.class)
  14. @ContextConfiguration(locations = "classpath:spring-rabbitmq-consumer.xml")
  15. public class SpringQueueListenerTest{
  16. @Test
  17. public void testOnMessage() throws InterruptedException {
  18. boolean flag = true;
  19. while (true) {
  20. Thread.sleep(5 * 1000);
  21. System.out.println("正在监听");
  22. }
  23. }
  24. }