• RabbitMQ java客户端集成 Spring 开发环境


    1、在pom.xml文件中引入rabbitmq坐标

        <!-- rabbitmq -->
            <dependency>
                <groupId>org.springframework.amqp</groupId>
                <artifactId>spring-rabbit</artifactId>
                <version>2.0.2.RELEASE</version>
            </dependency>

    2、spring配置文件编写 (笔者独立出一个配置文件 spring-rabbit.xml 装载RabbitMQ配置)

    <?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: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/rabbit
        http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
    
        <bean id="fooMessageListerer"
            class="com.hao.web.listener.FooMessageListerer" />
    
        <!-- 配置连接 -->
        <rabbit:connection-factory
            id="connectionFactory" host="127.0.0.1" port="5672" username="guest"
            password="guest" virtual-host="/" requested-heartbeat="60" />
    
        <!-- 配置RabbitTemplate -->
        <rabbit:template id="amqpTemplate"
            connection-factory="connectionFactory" exchange="myExchange"
            routing-key="foo.bar" />
        
        <!-- 配置 RabbitAdmin -->
        <rabbit:admin connection-factory="connectionFactory" />
    
        <!-- 配置队列名称 -->
        <rabbit:queue name="myQueue" />
    
        <!-- 配置 topic 类型交换器 -->
        <rabbit:topic-exchange name="myExchange">
            <rabbit:bindings>
                <rabbit:binding queue="myQueue" pattern="foo.*"></rabbit:binding>
            </rabbit:bindings>
        </rabbit:topic-exchange>
        
        <!-- 配置监听器 -->
        <rabbit:listener-container connection-factory="connectionFactory">
            <rabbit:listener ref="fooMessageListerer"
                queue-names="myQueue" />
        </rabbit:listener-container>
    
    </beans>

    3、编写监听类

    package com.hao.web.listener;
    
    import org.springframework.amqp.core.Message;
    import org.springframework.amqp.core.MessageListener;
    
    /**
     * @desc rabbitMQ监听器
     * @author zhanh247
     */
    public class FooMessageListerer implements MessageListener {
    
        @Override
        public void onMessage(Message message) {
            String msg = new String(message.getBody());
            System.out.println("spring rabbitmq receive message: " + msg);
        }
    
    }

    4、编写发送消息的测试类

    package com.hao;
    
    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({ "classpath:spring/spring-rabbit.xml" })
    public class RabbitMqTest {
    
        @Autowired
        RabbitTemplate rabbitTemplate;
    
        @Test
        public void sendMessage() {
            rabbitTemplate.convertAndSend("hello spring-rabbit...");
        }
    
    }

    5、启动spring项目,运行测试类,发送测试消息

    看到控制台有如下日志打印出,则说明集成已完成。

  • 相关阅读:
    SpringMVC的入门示例
    [PTA] 数据结构与算法题目集 6-1 单链表逆转
    [PTA] L3-015 球队“食物链”
    [PTA] 1001. 害死人不偿命的(3n+1)猜想 (Basic)
    [PTA] 1002. 写出这个数 (Basic)
    [opengl] 画一个可移动的自行车 二维几何变换(平移、旋转、缩放)
    css inline-block 水平居中
    css 图片裁剪显示
    [leetcode] 19. Remove Nth Node From End of List (Medium)
    [leetcode] 20. Valid Parentheses (easy)
  • 原文地址:https://www.cnblogs.com/zhanh247/p/12046715.html
Copyright © 2020-2023  润新知