rabbitmq學習記錄(六)交換機Exchange-direct

實現功能:一條消息發送給多個消費者java

交換機模式:directide

相比於以前的fanout模式,能夠進一步的篩選獲取消息的消費者。debug

fanout模式下,只要消費者監聽的隊列,已經與接收生產者消息的交換機綁定,那消費者就能收到消息。code

direct模式下,消費者監聽的隊列,不只須要已經與接收生產者消息的交換機進行綁定,並且綁定交換機時指定的routingKey要匹配生產者發送消息時指定的routingKey(徹底匹配)。blog

生產者:rabbitmq

package com.example.demo.queue.exchangeToQueue.direct;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.example.demo.utils.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class Producer {

	private static final String EXCHANGE_NAME = "exchange_direct"; 
	
	public static void main(String[] args) {
		Connection connection = null;
		Channel channel = null;
		try {
			// 獲取鏈接
			connection = ConnectionUtil.getConnection();
			// 建立通道
			channel = connection.createChannel();
			// 聲明交換機
			channel.exchangeDeclare(EXCHANGE_NAME, "direct");
			String routingKey = "debug";
			// 生產者發送的信息
			String msg = "routingKey="+routingKey;
			System.out.println("send msg : "+msg);
			// 發送信息
			channel.basicPublish(EXCHANGE_NAME, routingKey, null, msg.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 關閉通道
			try {
				channel.close();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (TimeoutException e) {
				e.printStackTrace();
			}
			// 關閉鏈接
			try {
				connection.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
}

消費者1:隊列

package com.example.demo.queue.exchangeToQueue.direct;

import java.io.IOException;

import com.example.demo.utils.ConnectionUtil;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class Consumer01 {

	private static final String EXCHANGE_NAME = "exchange_direct"; 
	
	private static final String QUEUE_NAME = "direct_exchange_to_queue_01";
	
	public static void main(String[] args) {
		try {
			// 獲取鏈接
			Connection connection = ConnectionUtil.getConnection();
			// 建立通道
			final Channel channel = connection.createChannel();
			// 聲明隊列
			channel.queueDeclare(QUEUE_NAME, true, false, false, null);
			// 綁定隊列到交換機
			channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info");
			// 定義消費者
			DefaultConsumer consumer = new DefaultConsumer(channel) {
				@Override
				public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException {
					String msg = new String(body,"UTF-8");
					System.out.println("[1]:receive msg:"+msg);
					System.out.println("[1]:deal msg successful.");
				}
				
			};
			// 接收信息
			channel.basicConsume(QUEUE_NAME, true, consumer);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

消費者2:get

package com.example.demo.queue.exchangeToQueue.direct;

import java.io.IOException;

import com.example.demo.utils.ConnectionUtil;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class Consumer02 {

	private static final String EXCHANGE_NAME = "exchange_direct"; 
	
	private static final String QUEUE_NAME = "direct_exchange_to_queue_02";
	
	public static void main(String[] args) {
		try {
			// 獲取鏈接
			Connection connection = ConnectionUtil.getConnection();
			// 建立通道
			final Channel channel = connection.createChannel();
			// 聲明隊列
			channel.queueDeclare(QUEUE_NAME, true, false, false, null);
			// 綁定隊列到交換機
			channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info");
			channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error");
			// 定義消費者
			DefaultConsumer consumer = new DefaultConsumer(channel) {
				@Override
				public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException {
					String msg = new String(body,"UTF-8");
					System.out.println("[2]:receive msg:"+msg);
					System.out.println("[2]:deal msg successful.");
				}
			};
			// 接收信息
			channel.basicConsume(QUEUE_NAME, true, consumer);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

消費者3:it

package com.example.demo.queue.exchangeToQueue.direct;

import java.io.IOException;

import com.example.demo.utils.ConnectionUtil;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class Consumer03 {

	private static final String EXCHANGE_NAME = "exchange_direct"; 
	
	private static final String QUEUE_NAME = "direct_exchange_to_queue_03";
	
	public static void main(String[] args) {
		try {
			// 獲取鏈接
			Connection connection = ConnectionUtil.getConnection();
			// 建立通道
			final Channel channel = connection.createChannel();
			// 聲明隊列
			channel.queueDeclare(QUEUE_NAME, true, false, false, null);
			// 綁定隊列到交換機
			channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info");
			channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error");
			channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "debug");
			// 定義消費者
			DefaultConsumer consumer = new DefaultConsumer(channel) {
				@Override
				public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException {
					String msg = new String(body,"UTF-8");
					System.out.println("[3]:receive msg:"+msg);
					System.out.println("[3]:deal msg successful.");
					// 反饋消息處理完畢
					channel.basicAck(envelope.getDeliveryTag(), false);
				}
			};
			// 接收信息
			channel.basicConsume(QUEUE_NAME, true, consumer);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

和以前同樣,涉及到了交換機,而在消費者類中中並無聲明交換機,因此須要先執行生產者類的main方法io

接下來,咱們就能夠依次執行三個消費者類的main方法,以後再執行生產者類的main方法。

上述生產者類指定的routingKey是debug,採用的是徹底匹配,理論上是隻有消費者3能收到消息

執行生產者類的main方法獲得的結果以下

消費者1終端:

消費者2終端:

消費者3終端:

結果與理論一致。

相關文章
相關標籤/搜索