properties

    1. rabbitmq.host=192.168.31.128
    2. rabbitmq.port=5672
    3. rabbitmq.username=guest
    4. rabbitmq.password=guest
    5. rabbitmq.virtual-host=/

    spring.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. -->
    25. <!--
    26. idbean的名称
    27. namequeue的名称
    28. auto-declare:自动创建
    29. auto-delete:自动删除。 最后一个消费者和该队列断开连接后,自动删除队列
    30. exclusive:是否独占
    31. durable:是否持久化
    32. -->
    33. <rabbit:queue id="spring_queue" name="spring_queue" auto-declare="true"/>
    34. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~广播;所有队列都能收到消息~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    35. <!--定义广播交换机中的持久化队列,不存在则自动创建-->
    36. <rabbit:queue id="spring_fanout_queue_1" name="spring_fanout_queue_1" auto-declare="true"/>
    37. <!--定义广播交换机中的持久化队列,不存在则自动创建-->
    38. <rabbit:queue id="spring_fanout_queue_2" name="spring_fanout_queue_2" auto-declare="true"/>
    39. <!--定义广播类型交换机;并绑定上述两个队列-->
    40. <rabbit:fanout-exchange id="spring_fanout_exchange" name="spring_fanout_exchange" auto-declare="true">
    41. <rabbit:bindings>
    42. <rabbit:binding queue="spring_fanout_queue_1" />
    43. <rabbit:binding queue="spring_fanout_queue_2"/>
    44. </rabbit:bindings>
    45. </rabbit:fanout-exchange>
    46. <!--<rabbit:direct-exchange name="aa" >
    47. <rabbit:bindings>
    48. &lt;!&ndash;direct 类型的交换机绑定队列 key :路由key queue:队列名称&ndash;&gt;
    49. <rabbit:binding queue="spring_queue" key="xxx"></rabbit:binding>
    50. </rabbit:bindings>
    51. </rabbit:direct-exchange>-->
    52. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~通配符;*匹配一个单词,#匹配多个单词 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    53. <!--定义广播交换机中的持久化队列,不存在则自动创建-->
    54. <rabbit:queue id="spring_topic_queue_star" name="spring_topic_queue_star" auto-declare="true"/>
    55. <!--定义广播交换机中的持久化队列,不存在则自动创建-->
    56. <rabbit:queue id="spring_topic_queue_well" name="spring_topic_queue_well" auto-declare="true"/>
    57. <!--定义广播交换机中的持久化队列,不存在则自动创建-->
    58. <rabbit:queue id="spring_topic_queue_well2" name="spring_topic_queue_well2" auto-declare="true"/>
    59. <rabbit:topic-exchange id="spring_topic_exchange" name="spring_topic_exchange" auto-declare="true">
    60. <rabbit:bindings>
    61. <rabbit:binding pattern="heima.*" queue="spring_topic_queue_star"/>
    62. <rabbit:binding pattern="heima.#" queue="spring_topic_queue_well"/>
    63. <rabbit:binding pattern="itcast.#" queue="spring_topic_queue_well2"/>
    64. </rabbit:bindings>
    65. </rabbit:topic-exchange>
    66. <!--定义rabbitTemplate对象操作可以在代码中方便发送消息-->
    67. <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
    68. </beans>

    生产者代码

    1. package com.itheima;
    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. @RunWith(SpringJUnit4ClassRunner.class)
    9. @ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
    10. public class ProducerTest {
    11. @Autowired
    12. private RabbitTemplate rabbitTemplate;
    13. //使用spring内置的工具类去使用
    14. @Test
    15. public void testHelloWorld() {
    16. rabbitTemplate.convertAndSend("spring_queue", "hello Spring RabbitMQ");
    17. }
    18. }

    消费者代码

    1. //消费者只要实现MessageListener接口就可直接获取到信息
    2. public class SpringQueueListener implements MessageListener {
    3. @Override
    4. public void onMessage(Message message) {
    5. System.out.println(new String(message.getBody()));
    6. }
    7. }