1. 程式人生 > >RabbitMQ訊息佇列生產者和消費者

RabbitMQ訊息佇列生產者和消費者

概述

生產者生產資料至 RabbitMQ 佇列,消費者消費 RabbitMQ 佇列裡的資料。

詳細

一、準備工作

1、安裝 RabbitMQ 服務和 RabbitMQ Management

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 上保持一致