RabbitMq七种工作模式,结合简单的java实例使用,别再说你不会( 四 )


文章插图
 

  • 不同密钥绑定不同的队列,可以发挥出不同日志级别发送到不同的队列的效果 。

RabbitMq七种工作模式,结合简单的java实例使用,别再说你不会

文章插图
 
4.1.1、Senderimport 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 。
RabbitMq七种工作模式,结合简单的java实例使用,别再说你不会

文章插图
 

RabbitMq七种工作模式,结合简单的java实例使用,别再说你不会

文章插图
 

RabbitMq七种工作模式,结合简单的java实例使用,别再说你不会

文章插图
 

RabbitMq七种工作模式,结合简单的java实例使用,别再说你不会

文章插图
 

RabbitMq七种工作模式,结合简单的java实例使用,别再说你不会

文章插图
 
5、话题(topic)话题也是一个持久的消息,只要交换机还在,每个上线的消费者都可以消费一次自己感兴趣的topic 。
  • *(星号)可以代替一个单词 。
  • #(哈希)可以替代零个或多个单词 。

RabbitMq七种工作模式,结合简单的java实例使用,别再说你不会

文章插图
 
5.1.1、Sender.javaimport 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();}}


推荐阅读