写点什么

RabbitMQ 最常用的三大模式

  • 2022 年 4 月 16 日
  • 本文字数:2197 字

    阅读完需:约 7 分钟

例如:


  • 'log.#"能够匹配到'log.info.oa"

  • "log.*"只会匹配到"log.erro“



import com.rabbitmq.client.Channel;


import com.rabbitmq.client.Connection;


import com.rabbitmq.client.ConnectionFactory;


public class TopicProducer {


public static void main(String[] args) throws Exception {


//1. 创建一个 ConnectionFactory 并进行设置


ConnectionFactory factory = new ConnectionFactory();


factory.setHost("localhost");


factory.setVirtualHost("/");


factory.setUsername("guest");


factory.setPassword("guest");


//2. 通过连接工厂来创建连接


Connection connection = factory.newConnection();


//3. 通过 Connection 来创建 Channel


Channel channel = connection.createChannel();


//4. 声明


String exchangeName = "test_topic_exchange";


String routingKey1 = "item.update";


String routingKey2 = "item.delete";


String routingKey3 = "user.add";


//5. 发送


String msg = "this is topic msg";


channel.basicPublish(exchangeName, routingKey1, null, msg.getBytes());


channel.basicPublish(exchangeName, routingKey2, null, msg.getBytes());


channel.basicPublish(exchangeName, routingKey3, null, msg.getBytes());


System.out.println("Send message : " + msg);


//6. 关闭连接


channel.close();


connection.close();


}


}


import com.rabbitmq.client.*;


import java.io.IOException;


public class TopicConsumer {


public static void main(String[] args) throws Exception {


//1. 创建一个 ConnectionFactory 并进行设置


ConnectionFactory factory = new ConnectionFactory();


factory.setHost("localhost");


factory.setVirtualHost("/");


factory.setUsername("guest");


factory.setPassword("guest");


factory.setAutomaticRecoveryEnabled(true);


factory.setNetworkRecoveryInterval(3000);


//2. 通过连接工厂来创建连接


Connection connection = factory.newConnection();


//3. 通过 Connection 来创建 Channel


Channel channel = connection.createChannel();


//4. 声明


String exchangeName = "test_topic_exchange";


String queueName = "test_topic_queue";


String routingKey = "item.#";


channel.exchangeDeclare(exchangeName, "topic", true, false, null);


channel.queueDeclare(queueName, false, false, false, null);


//一般不用代码绑定,在管理界面手动绑定《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》开源 channel.queueBind(queueName, exchangeName, routingKey);


//5. 创建消费者并接收消息


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(" [x] Received '" + message + "'");


}


};


//6. 设置 Channel 消费者绑定队列


channel.basicConsume(queueName, true, consumer);


}


}


Send message : this is topc msg


[x] Received 'this is topc msg'


[x] Received 'this is topc msg'


Fanout 模式


=============


不处理路由键,只需要简单的将队列绑定到交换机上发送到交换机的消息都会被转发到与该交换机绑定的所有队列上。


Fanout 交换机转发消息是最快的。



import com.rabbitmq.client.*;


import java.io.IOException;


public class FanoutConsumer {


public static void main(String[] args) throws Exception {


//1. 创建一个 ConnectionFactory 并进行设置


Connec Java 开源项目【ali1024.coding.net/public/P7/Java/git】 tionFactory factory = new ConnectionFactory();


factory.setHost("localhost");


factory.setVirtualHost("/");


factory.setUsername("guest");


factory.setPassword("guest");


factory.setAutomaticRecoveryEnabled(true);


factory.setNetworkRecoveryInterval(3000);


//2. 通过连接工厂来创建连接


Connection connection = factory.newConnection();


//3. 通过 Connection 来创建 Channel


Channel channel = connection.createChannel();


//4. 声明


String exchangeName = "test_fanout_exchange";


String queueName = "test_fanout_queue";


String routingKey = "item.#";


channel.exchangeDeclare(exchangeName, "fanout", true, false, null);


channel.queueDeclare(queueName, false, false, false, null);


//一般不用代码绑定,在管理界面手动绑定


channel.queueBind(queueName, exchangeName, routingKey);


//5. 创建消费者并接收消息


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(" [x] Received '" + message + "'");


}

最后

针对最近很多人都在面试,我这边也整理了相当多的面试专题资料,也有其他大厂的面经。希望可以帮助到大家。


最新整理面试题


上述的面试题答案都整理成文档笔记。也还整理了一些面试资料 &最新 2021 收集的一些大厂的面试真题


最新整理电子书



最新整理大厂面试文档



以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

用户头像

还未添加个人签名 2022.04.13 加入

还未添加个人简介

评论

发布
暂无评论
RabbitMQ 最常用的三大模式_Java_爱好编程进阶_InfoQ写作平台