RabbitMQ四種Exchange類型之Topic (Java)

我的個人博客網站雲諾說上線啦!所有文章都搬到新地址了,點擊圍觀吧!

Topic類型的Exchange是要進行路由鍵匹配的。此時需要通過路由鍵將隊列綁定要一個交換器上。規則如下

  • 符號“#”匹配一個或多個詞,例如:“logs.#”能夠匹配到“logs.error”、“logs.info.toc”
  • 符號“*”只能匹配一個詞。例如:“logs.*” 只能匹配到“logs.error”,不能匹配到“logs.info.toc” 。

 

如下圖:

下面就直接上代碼吧!!

Consumer:

package topic;

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

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;


public class TopicConsumer {

	private static final String EXCHANGE_NAME 	= "exchange_topic";
	public static void main(String[] argv) throws IOException, TimeoutException  {
		
		new ExchangeTopic("logs.info");
		new ExchangeTopic("logs.*");
		new ExchangeTopic("logs.#");
	}

	static class ExchangeTopic{
		public  ExchangeTopic(final String routingKey) throws IOException, TimeoutException {
			ConnectionFactory factory = new ConnectionFactory();
			//rabbitmq監聽IP
			factory.setHost("192.168.249.128");
			//rabbitmq監聽默認端口
			factory.setPort(5672);
			//設置訪問的用戶
			factory.setUsername("test");
			factory.setPassword("test");
			Connection connection = factory.newConnection();
			Channel channel = connection.createChannel();
			//聲明路由名字和類型
			channel.exchangeDeclare(EXCHANGE_NAME, "topic", false, true, null);
			//隊列名稱
			String queueName = routingKey + ".queue";
			//創建隊列
			channel.queueDeclare(queueName, false, false, true, null);
			//把隊列綁定到路由上
			channel.queueBind(queueName, EXCHANGE_NAME, routingKey);

			System.out.println(" [routingKey = "+ routingKey +"] Waiting for msg....");

			Consumer consumer = new DefaultConsumer(channel) {
				@Override
				public void handleDelivery(String consumerTag, Envelope envelope,
						AMQP.BasicProperties properties, byte[] body) throws IOException {
					String message = new String(body, "UTF-8");
					
					System.out.println("[routingKey = "+ routingKey +"] Received msg is '" + message + "'");
				}
			};
			channel.basicConsume(queueName, true, consumer);
		}

	}

}

Producer:

package topic;

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

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;


public class TopicProducer {

	private static final String EXCHANGE_NAME = "exchange_topic";

	public static void main(String[] argv) throws Exception{
		new ExchangeTopic("logs.info", "logs Info test !!");
		new ExchangeTopic("logs.error", "logs error test !!");
		new ExchangeTopic("logs.error.toc", "logs error toc test !!");
	}
	
	static class ExchangeTopic{
		public ExchangeTopic(String routingKey,String message) throws IOException, TimeoutException{
			ConnectionFactory factory = new ConnectionFactory();
			//rabbitmq監聽IP
			factory.setHost("192.168.249.128");
			//rabbitmq監聽默認端口
			factory.setPort(5672);
			//設置訪問的用戶
			factory.setUsername("test");
			factory.setPassword("test");
			Connection connection = factory.newConnection();
			Channel channel = connection.createChannel();

			//聲明路由名字和類型
			channel.exchangeDeclare(EXCHANGE_NAME, "topic", false, true, null);
			
			channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
			System.out.println("[routingKey = "+routingKey+"] Sent msg is '" + message + "'");

			channel.close();
			connection.close();
			
		}
	}

}

結果:



 

從結果中可以看出,路由鍵爲"logs.info"只匹配到了Producer路由鍵是"logs.info"的信息,路由鍵爲"logs.*"則匹配到了Producer路由鍵是"logs.info","logs.error"兩條信息,而路由鍵爲"logs.#"則匹配到了Producer路由鍵是"logs.info","logs.error","logs.error.toc"三條信息。

 

祝生活愉快!!!

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章