RabbitMQ 最常用的三大模式

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

Connection connection = factory.newConnection();

//3. 通过 Connection 来创建 Channel

Channel channel = connection.createChannel();

//4. 声明

String exchangeName = “test_direct_exchange”;

String queueName = “test_direct_queue”;

String routingKey = “item.direct”;

channel.exchangeDeclare(exchangeName, “direct”, 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 + "’");

}

};

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

channel.basicConsume(queueName, true, consumer);

}

}

Send message : this is direct msg

[x] Received ‘this is direct msg’

Topic 模式

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

可以使用通配符进行模糊匹配

  • 符号’#" 匹配一个或多个词

  • 符号"*”匹配不多不少一个词

例如:

  • '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);

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

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 并进行设置

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_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 + "’");

}

};

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

channel.basicConsume(queueName, true, consumer);

}

}

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.ConnectionFactory;

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

分享

1、算法大厂——字节跳动面试题

2、2000页互联网Java面试题大全

3、高阶必备,算法学习

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
03032d36.jpg" alt=“img” style=“zoom: 33%;” />

分享

1、算法大厂——字节跳动面试题

[外链图片转存中…(img-FuJyoums-1713272707436)]

2、2000页互联网Java面试题大全

[外链图片转存中…(img-MK3KwYJ1-1713272707436)]

3、高阶必备,算法学习

[外链图片转存中…(img-bcFfrDwR-1713272707437)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值