zoukankan      html  css  js  c++  java
  • RabbitMQ消息队列生产者和消费者

    概述

    生产者生产数据至 RabbitMQ 队列,消费者消费 RabbitMQ 队列里的数据。

    详细

    一、准备工作

    1、安装 RabbitMQ 服务和 RabbitMQ Management

    RabbitMQ在windows下的安装 (点击查看) 

    RabbitMQ官网(可下载安装包)(点击查看)

     

    2、在 RabbitMQ 管理界面创建用户 test 密码 test,创建名为 test_vhost 的 Virtual Hosts ,将 test_vhost 分配 给 test用户

    3、本实例主要演示如何发送消息至 RabbitMQ 队列 ,以及如何消费 RabbitMQ 队列的消息

    二、程序实现

    1、程序结构

    2.gif

    2、实现思路

    配置发送的 Exchange 和 Queue

        <rabbit:queue id="queue.ljaer.test" name="queue.ljaer.test"
            auto-declare="true" auto-delete="false" exclusive="false" durable="true"
            declared-by="rabbitAdmin" />
            
        <!-- Exchange Type 为   topic 配置方法 -->
        <rabbit:topic-exchange id="exchange.topic.producer"
            auto-declare="true" name="exchange.topic.producer" auto-delete="false"
            durable="true" declared-by="rabbitAdmin">
            <rabbit:bindings>
                <rabbit:binding pattern="queue.ljaer.test" queue="queue.ljaer.test" />
            </rabbit:bindings>
        </rabbit:topic-exchange>

    连接 RabbitMQ 发送数据至队列

    public class RabbitmqProducerTest {
    
        private static ApplicationContext context;
    
        public static void main(String[] args) {
            context = new ClassPathXmlApplicationContext("send-rabbitMq.xml");
            AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
            JSONObject json = new JSONObject();
            json.put("name", "张三");
            json.put("age", "15");
            amqpTemplate.convertAndSend("queue.ljaer.test", json);
            //amqpTemplate.convertAndSend("exchange.topic.producer","queue.ljaer.test", json);
            System.out.println("success");
        }
    }

    配置监听

        <!--定义queue 接收数据 -->
        <rabbit:queue id="queue.ljaer.test" name="queue.ljaer.test"
            auto-declare="true" auto-delete="false" exclusive="false" durable="true"
            declared-by="rabbitAdmin" />
    
        <!-- 消息监听器 -->
        <bean id="rabbitmqConsumerTest" class="com.test.mq.RabbitmqConsumerTest" />
    
        <!-- 队列监听 -->
        <rabbit:listener-container
            connection-factory="connectionFactory" acknowledge="auto">
            <rabbit:listener queues="queue.ljaer.test" ref="rabbitmqConsumerTest" />
        </rabbit:listener-container>

    监听消费 RabbitMQ 队列的数据

    public class RabbitmqConsumerTest implements MessageListener {
        
        public void onMessage(Message message) {
            System.out.println("receive message:{}"+message.getBody());
            try {
                String s = new String(message.getBody(), "UTF-8");
                System.out.println("------>MQ接收到的数据:"+s);
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    3、配置文件说明

    #mq
    mq.host=192.168.99.100
    mq.username=test
    mq.password=test
    mq.port=5672
    mq.vhost=/test_vhost

    三、运行效果

    1、导入项目至 Eclipse,修改 rabbit.properties 里面的连接信息,连接至你本地的 RabbitMQ 服务

    2、执行 RabbitmqProducerTest 的 main 方法,发送消息至 RabbitMQ 队列

    1.gif

    3.gif

    3、执行 RabbitmqConsumerMain 的 main 方法,进行队列监听,消费 RabbitMQ 队列里的数据

    4.gif

    消费完之后,在 RabbitMQ Managemenet 里面查看也会看到队列数据减少

    四、其他补充

    1、注意用 guest 用户创建 test 用户之后,需要使用 test 用户登录才能看到该用户下的队列数据

    2、如果选择其他类型的 exchange ,注意配文件与 mq 上保持一致

    注:本文著作权归作者,由demo大师发表,拒绝转载,转载需要作者授权

  • 相关阅读:
    sql server 中拥有相同字段值的记录某个字段合并问题解答
    一个SQL语句实例
    Sql server 2005中output用法解析
    SQl 2005 For XMl 简单查询(Raw,Auto,Path模式)转载
    .NET/C#中对对象集合进行查询的方法 以及相关的 Predicate<T> 及 Action<T> 的用法
    SQLServer Case具有两种格式:简单Case函数和Case搜索函数
    sql server compute by 子句用法实例
    vs2008 一件悲剧的事情
    SQl 2005 For XMl 简单查询(Raw,Auto,Path模式)
    SQL Server2005中四种排名函数的使用
  • 原文地址:https://www.cnblogs.com/demodashi/p/8486491.html
Copyright © 2011-2022 走看看