当前位置:   article > 正文

RabbitMQ整合Spring项目xml配置文件形式_rabbitmq xml配置

rabbitmq xml配置

依赖

<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-rabbit</artifactId>
    <version>2.2.8.RELEASE</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.8.RELEASE</version>
</dependency>
<!--Jackson 核心库-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.9</version>
        </dependency>
        <!--Jackson 序列化库-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.9</version>
        </dependency>
        <!--Jackson 注解支持-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.8.9</version>
        </dependency>
  • 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

生产者xml配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/rabbit
   http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">



    <bean id="ProducerController" class="com.hy.hy114.util.rabbitmq.ProducerController"/>

    <!-- 配置连接工厂 -->
    <rabbit:connection-factory id="connectionFactory"
                               host="192.168.103.10" port="5672" username="guest" password="guest" />

    <!-- 定义mq管理 -->
    <rabbit:admin connection-factory="connectionFactory" />

    <!-- 声明队列 -->
    <rabbit:queue name="que_producerOne" auto-declare="true" durable="true" />

    <!-- 定义交换机绑定队列(路由模式) -->
    <rabbit:direct-exchange name="IExchange" id="IExchange">
        <rabbit:bindings>
            <rabbit:binding queue="que_producerOne" key="que_producerOne_key" />
        </rabbit:bindings>
    </rabbit:direct-exchange>

    <!-- 消息对象json转换类 -->
    <bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />

    <!-- 定义模版 -->
    <rabbit:template id="rabbitTemplate"
    connection-factory="connectionFactory" exchange="IExchange"
    message-converter="jsonMessageConverter" />

</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

消费者xml配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/rabbit
   http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">


    <!-- 配置连接工厂 -->
    <rabbit:connection-factory id="connectionFactory"
                               host="192.168.103.10" port="5672" username="guest" password="guest" />

    <!-- 定义mq管理 -->
    <rabbit:admin connection-factory="connectionFactory" />

    <!-- 声明队列 -->
    <rabbit:queue name="que_producerOne" auto-declare="true" durable="true" />

    <!-- 定义消费者 -->
    <bean name="ConsumerOne" class="com.hy.hy114.util.rabbitmq.ConsumerOne" />

    <!-- 定义消费者监听队列 -->
    <rabbit:listener-container
            connection-factory="connectionFactory">
        <rabbit:listener ref="ConsumerOne" queues="que_producerOne" />
    </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

Spring的配置文件

在applicationContext.xml配置文件中引入以上两个xml文件即可。
在这里插入图片描述

消费者消费


public class ConsumerOne implements MessageListener {


    private static final ObjectMapper MAPPER = new ObjectMapper();

    public void onMessage(Message msg) {
    
System.out.println("===============start===============");
            JsonNode jsonNode = MAPPER.readTree(msg.getBody());
            String date = jsonNode.get("date").asText();
            String date = jsonNode.get("data").asText();
            System.out.println(msg);
System.out.println("===============已消费===============");

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

测试类生产消息

注入RabbitTemplate来操作RabbitMQ

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class RabbitTest {
    @Autowired
    RabbitTemplate rabbitTemplate;
	@Test
    public void test1(){
        HashMap<String, String> map = new HashMap<String, String>();
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        map.put("date",dateTimeFormatter.format(now) );
        map.put("data", "test");
        //根据key发送到对应的队列
       rabbitTemplate.convertAndSend("que_producerOne_key", map);
        System.out.println("已生产");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

搞定!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/139173
推荐阅读
相关标签
  

闽ICP备14008679号