文章插图
- 不同密钥绑定不同的队列,可以发挥出不同日志级别发送到不同的队列的效果 。
文章插图
4.1.1、Sender
import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import top.san.RabbitMq.util.RabbitmqConnectionUtil;import java.io.IOException;public class Sender {private final static String EXCHANGE_NAME = "exchange_direct";private final static String EXCHANGE_TYPE = "direct";public static void main(String[] args) throws IOException {Connection connection = RabbitmqConnectionUtil.getConnection();Channel channel = connection.createChannel();channel.exchangeDeclare(EXCHANGE_NAME,EXCHANGE_TYPE);String message = "那一定是蓝色";channel.basicPublish(EXCHANGE_NAME,"key2", null, message.getBytes());System.out.println("[x] Sent '"+message+"'");channel.close();connection.close();}}
4.1.2、Receiver1.javaimport com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.QueueingConsumer;import top.san.RabbitMq.util.RabbitmqConnectionUtil;import java.io.IOException;/** * Created by san */public class Receiver1 {private finalstaticString QUEUE_NAME = "queue_routing";private final static String EXCHANGE_NAME = "exchange_direct";public static void main(String[] args) throws IOException, InterruptedException {// 获取到连接以及mq通道Connection connection = RabbitmqConnectionUtil.getConnection();Channel channel = connection.createChannel();channel.queueDeclare(QUEUE_NAME, false,false,false,null);channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"key");channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"key2");channel.basicQos(1);QueueingConsumer consumer = new QueueingConsumer(channel);channel.basicConsume(QUEUE_NAME, false, consumer);while(true){QueueingConsumer.Delivery delivery = consumer.nextDelivery();String message = new String(delivery.getBody());System.out.println("[x] Received1 "+message);Thread.sleep(10);channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);}}}
4.1.3、Receiver2.javaimport com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.QueueingConsumer;import top.san.RabbitMq.util.RabbitmqConnectionUtil;import java.io.IOException;/** * Created by san */public class Receiver2 {private finalstaticString QUEUE_NAME = "queue_routing2";private final static String EXCHANGE_NAME = "exchange_direct";public static void main(String[] args) throws IOException, InterruptedException {// 获取到连接以及mq通道Connection connection = RabbitmqConnectionUtil.getConnection();Channel channel = connection.createChannel();channel.queueDeclare(QUEUE_NAME, false,false,false,null);channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"key2");channel.basicQos(1);QueueingConsumer consumer = new QueueingConsumer(channel);channel.basicConsume(QUEUE_NAME, false, consumer);while(true){QueueingConsumer.Delivery delivery = consumer.nextDelivery();String message = new String(delivery.getBody());System.out.println("[x] Received2 "+message);Thread.sleep(10);channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);}}}
4.1.4、结果-总结有一点要注意是:在direct下,必须是Exchange(交换机)已经存在,消费端的队列才能绑定到Exchange,否则会报错 。也就说上面的程序第一次运行时,需先启Sender,才能成功启动Reciver 。文章插图
文章插图
文章插图
文章插图
文章插图
5、话题(topic)话题也是一个持久的消息,只要交换机还在,每个上线的消费者都可以消费一次自己感兴趣的topic 。
- *(星号)可以代替一个单词 。
- #(哈希)可以替代零个或多个单词 。
文章插图
5.1.1、Sender.java
import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import top.san.RabbitMq.util.RabbitmqConnectionUtil;import java.io.IOException;public class Sender {private final static String EXCHANGE_NAME = "exchange_topic";private final static String EXCHANGE_TYPE = "topic";public static void main(String[] args) throws IOException {Connection connection = RabbitmqConnectionUtil.getConnection();Channel channel = connection.createChannel();channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE);//消息内容String message = "这里是卢本伟广场";//第二个参数是topic匹配值channel.basicPublish(EXCHANGE_NAME,"lbw.nb",null,message.getBytes());System.out.println("[x] Sent '"+message+"'");//关通道 关连接channel.close();connection.close();}}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 计算机专业学什么语言好找工作?
- 残疾人|本科没拿到学位证对以后找工作有多大影响?
- 鲜虾萝卜饼的做法
- 没有工作的一年|《没有工作的一年》赚钱还是躺平?小雨租房也乐观,却源于这一点
- 招聘|考研究生反而不好找工作,日本人这点很认真,大家觉得应该学吗?
- 教师|想上岸入编找到理想工作,该如何选择单位?随我一起来看看吧!
- 犀角化毒丸怎么样
- 戴尔Precision 5750移动工作站深度测试
- 教你快速清除Excel工作表保护密码
- excel批量拆分工作簿,用VBA一键拆分,把数据分解到N个工作簿