当前位置:   article > 正文

spring 整合 rabbitmq 配置文件_java springmvc整合rabbitmq消费者topic配置文件

java springmvc整合rabbitmq消费者topic配置文件

spring 整合 rabbitmq 配置文件

说明: spring 整合 rabbitmq 的主要之处是配置文件信息, 代码量减少

1. 创建生产者

  • rabbitmq.properties 配置文件信息
rabbitmq.host=192.168.192.137
rabbitmq.port=5672
rabbitmq.username=qmh
rabbitmq.password=qmh
rabbitmq.virtual-host=/qmh
  • 1
  • 2
  • 3
  • 4
  • 5
  • spring-rabbitmq-producer.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}"/>
    <!--定义管理交换机、队列-->
    <rabbit:admin connection-factory="connectionFactory"/>

    <!--定义持久化队列,不存在则自动创建;不绑定到交换机则绑定到默认交换机
    默认交换机类型为direct,名字为:"",路由键为队列的名称
    id: bean容器的名称
    name: 队列queue的名称
    auto-declare: 自动创建
    -->
    <rabbit:queue id="spring_queue" name="spring_queue" auto-declare="true"/>

    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~广播;所有队列都能收到消息~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!--定义广播交换机中的持久化队列,不存在则自动创建-->
    <rabbit:queue id="spring_fanout_queue_one" name="spring_fanout_queue_one" auto-declare="true"/>

    <!--定义广播交换机中的持久化队列,不存在则自动创建-->
    <rabbit:queue id="spring_fanout_queue_two" name="spring_fanout_queue_two" auto-declare="true"/>

    <!--定义广播类型交换机;并绑定上述两个队列-->
    <rabbit:fanout-exchange id="spring_fanout_exchange" name="spring_fanout_exchange" auto-declare="true">
        <rabbit:bindings>
            <rabbit:binding queue="spring_fanout_queue_one"/>
            <rabbit:binding queue="spring_fanout_queue_two"/>
        </rabbit:bindings>
    </rabbit:fanout-exchange>

    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~路由;指定routing key的队列都能收到消息~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!--定义广播交换机中的持久化队列,不存在则自动创建-->
    <rabbit:queue id="spring_direct_queue_one" name="spring_direct_queue_one" auto-declare="true"/>

    <!--定义广播交换机中的持久化队列,不存在则自动创建-->
    <rabbit:queue id="spring_direct_queue_two" name="spring_direct_queue_two" auto-declare="true"/>

    <rabbit:direct-exchange id="spring_direct_exchange" name="spring_direct_exchange" auto-declare="true">
        <rabbit:bindings>
            <rabbit:binding queue="spring_direct_queue_one" key="qmh"></rabbit:binding>
            <rabbit:binding queue="spring_direct_queue_one" key="lyq"></rabbit:binding>
            <rabbit:binding queue="spring_direct_queue_two" key="qmh love lyq"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:direct-exchange>

    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~通配符;*匹配一个单词,#匹配多个单词 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!--定义广播交换机中的持久化队列,不存在则自动创建-->
    <rabbit:queue id="spring_topic_queue_star" name="spring_topic_queue_star" auto-declare="true"/>
    <!--定义广播交换机中的持久化队列,不存在则自动创建-->
    <rabbit:queue id="spring_topic_queue_well" name="spring_topic_queue_well" auto-declare="true"/>
    <!--定义广播交换机中的持久化队列,不存在则自动创建-->
    <rabbit:queue id="spring_topic_queue_well_copy" name="spring_topic_queue_well_copy" auto-declare="true"/>

    <rabbit:topic-exchange id="spring_topic_exchange" name="spring_topic_exchange" auto-declare="true">
        <rabbit:bindings>
            <rabbit:binding pattern="qmh.*" queue="spring_topic_queue_star"/>
            <rabbit:binding pattern="qmh.#" queue="spring_topic_queue_well"/>
            <rabbit:binding pattern="lyq.#" queue="spring_topic_queue_well_copy"/>
        </rabbit:bindings>
    </rabbit:topic-exchange>

    <!--定义rabbitTemplate对象操作可以在代码中方便发送消息-->
    <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
</beans>

  • 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
  • 编写测试类, 发送消息
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
public class SpringAndRabbitMQTest {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void helloWorldTest(){
        // 发送消息
        rabbitTemplate.convertAndSend("spring_queue","Hello,spring and rabbitmq!");
    }

    @Test
    public void helloFanoutTest(){
        // 发送消息
        rabbitTemplate.convertAndSend("spring_fanout_exchange","","Hello,rabbitmq fanout test!");
    }

    @Test
    public void helloDirectTest(){
        // 发送消息
        rabbitTemplate.convertAndSend("spring_direct_exchange","qmh love lyq","Hello,qmh love lyq!");
    }

    @Test
    public void helloTopicTest(){
        // 发送消息
        rabbitTemplate.convertAndSend("spring_topic_exchange","qmh.lyq.love","Hello,rabbitmq topic test!");
    }
}

  • 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

2. 创建消费者

  • rabbitmq.properties 配置文件信息
rabbitmq.host=192.168.192.137
rabbitmq.port=5672
rabbitmq.username=qmh
rabbitmq.password=qmh
rabbitmq.virtual-host=/qmh
  • 1
  • 2
  • 3
  • 4
  • 5
  • 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}"/>

	<!-- 定义对应的消费者实现类  SpringQueueListener,FanoutListenerOne.... -->
    <bean id="springQueueListener" class="com.itheima.rabbitmqListener.SpringQueueListener"/>
    <bean id="fanoutListenerOne" class="com.itheima.rabbitmqListener.FanoutListenerOne"/>
    <bean id="fanoutListenerTwo" class="com.itheima.rabbitmqListener.FanoutListenerTwo"/>
    <bean id="directListenerOne" class="com.itheima.rabbitmqListener.DirectListenerOne"/>
    <bean id="directListenerTwo" class="com.itheima.rabbitmqListener.DirectListenerTwo"/>
    <bean id="topicListenerStar" class="com.itheima.rabbitmqListener.TopicListenerStar"/>
    <bean id="topicListenerWell" class="com.itheima.rabbitmqListener.TopicListenerWell"/>
    <bean id="topicListenerWellCopy" class="com.itheima.rabbitmqListener.TopicListenerWellCopy"/>

	<!-- 为对应的消费者实现类添加监听器, 绑定对应的队列.... -->
    <rabbit:listener-container connection-factory="connectionFactory" auto-declare="true">
        <rabbit:listener ref="springQueueListener" queue-names="spring_queue"/>
        <rabbit:listener ref="fanoutListenerOne" queue-names="spring_fanout_queue_one"/>
        <rabbit:listener ref="fanoutListenerTwo" queue-names="spring_fanout_queue_two"/>
        <rabbit:listener ref="directListenerOne" queue-names="spring_direct_queue_one"/>
        <rabbit:listener ref="directListenerTwo" queue-names="spring_direct_queue_two"/>
        <rabbit:listener ref="topicListenerStar" queue-names="spring_topic_queue_star"/>
        <rabbit:listener ref="topicListenerWell" queue-names="spring_topic_queue_well"/>
        <rabbit:listener ref="topicListenerWellCopy" queue-names="spring_topic_queue_well_copy"/>
    </rabbit:listener-container>
</beans>

  • 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
  • 编写对应的消费者实现类(实现 MessageListener 消息监听器类)
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

/**
 * spring 整合 rabbitmq
 */
public class SpringQueueListener implements MessageListener {
    @Override
    public void onMessage(Message message) {
        System.out.println("spring_queue:" + new String(message.getBody()));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 编写测试类, 使消费者运行起来
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-consumer.xml")
public class ListenerTest {
    @Test
    public void test(){
        Boolean flag = true;
        while (true){

        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/139094
推荐阅读
相关标签
  

闽ICP备14008679号