分布式 java-rabbitmq java RabbitMQ - channel
一、为什么使用RabbitMQ及其优点:
1、RabbitMQ基于AMQP的协议。AMQP协议:高级消息队列协议。它支持很多种协议消息中间件的里面的这种这种业务场景,比如是这种点对点的这种发布、以及这种交换机路由、包括那种发布订阅模式,通过这种AMQP协议提供的多种各样的模式,从而适应了咱们项目中的不同的业务场景。这是它的第一个广受欢迎的原因之一。
2、它使用erlang语言开发,那erlang语言本身就是做这种socket的这个编程,做这个在socket编程里面做这个效率是非常不错的。
3、RabbitMQ,它天生的和咱们的spring框架无缝的整合,我们在使用Spring框架去整合RabbitMQ,非常的简单,快捷快速。
4、RabbitMQ自身的特点了,它对数据一致性的要求,以及这个消息的错误丢失错误处理,非常的友好,它可以说是甚至不丢失任何的数据。即使咱们数据出现错误的话,通过它内部的机制,咱们也可以对消息进行恢复。
AMQP是一种协议,更准确的说是一种链接协议, AMQP不是从API层进行限定的,而是直接从网络的这个传输的过程中进行数据交换的,这就使得了AMQP的这个这个生产者具有天然性的这种跨平台,AMQP里面的协议模型里面,有这个生产者和这个消费者。生产者和消费者之间是互相没有做任何通信的,是利用了RabbitMQ去进行通信。因为RabbitMQ的本质,咱们也给大家说过了,他要实现这个跨系统之间通信的解耦:
我们在使用RabbitMQ的时候,首先我们要构建一个虚拟主机,紧接着给虚拟主机去绑定用户,我们通过生产者去发送消息时,要连接到RabbitMQ服务,通过用户名密码,指定连接到RabbitMQ服务里面对应的某一个虚拟主机,紧接着生产者才可以将消息通过通道发布给我们对应的交换机或者是消息队列中,这取决于我们采用哪种消息模型;
消费者在消费消息时,也需要去连接到RabbitMQ服务以及对应的虚拟主机,我们才能去消费这个虚拟主机中消息队列里面对应的数据。
这就是AMQP协议
二、 RabbitMQ 支持的消息模型:
1、简单模式(直连) 2、工作模式 3、发布/订阅模式 4、路由模式 5、Topics主题模式/动态路由模式 6、发布确认模式
三、简单模式:
3.1、概念:
在简单模式中,没有交换机,生产者通过通道将消息发送到消息队列,然后由我的消费者通过通道去队列中获取消息,进行消费。消费者会一直等待消息的到来,只要有消息,他就会立即去做消费,生产者源源不断的生产消息依次的进入队列。消费者不断的从队列中取出消息进行消费
3.2、代码实现:
junit
junit
4.11
test
com.rabbitmq
amqp-client
5.13.1
生产者:
public class Provider {
@Test
public void testSendMessage() throws IOException, TimeoutException {
//1、创建连接MQ的连接工厂对象
ConnectionFactory connectionFactory = new ConnectionFactory();
//2、设置连接RabbitMQ的主机
connectionFactory.setHost("123.57.144.31");
//3、设置端口号
connectionFactory.setPort(5672);
//4、设置访问虚拟主机的用户名、密码
connectionFactory.setUsername("ems");
connectionFactory.setPassword("123");
//5、设置连接哪个虚拟主机
connectionFactory.setVirtualHost("/emsHost");
//6、获取连接对象
Connection connection = connectionFactory.newConnection();
//7、获取通道对象
Channel channel = connection.createChannel();
//8、通道绑定对应的消息队列: 消费者和生产者在这个方法里面的参数一定要一致
// 参数1 queue: 消息队列的名称,如果该队列不存在,会自动创建; 参数2durable:是否持久化队列
// 参数3 exclusive: 是否独占队列 参数4 autoDelete:消费者消费完成后,是否删除队列
channel.queueDeclare("hello",false,false,false,null);
//9、发布消息
// 参数1 exchange:交换机名称,helloworld模式中不涉及交换机 参数2:队列名称
// 参数3 basicProperties:传递消息额外的设置 参数4:具体的消息内容
channel.basicPublish("","hello",null,"rabbitMq的helloWorld模式22".getBytes());
channel.close();
connection.close();
}
}
消费者:
public class Consumer {
public static void main(String[] args) throws IOException, TimeoutException {
//1、创建连接MQ的连接工厂对象
ConnectionFactory connectionFactory = new ConnectionFactory();
//2、设置连接RabbitMQ的主机
connectionFactory.setHost("123.57.144.31");
//3、设置端口号
connectionFactory.setPort(5672);
//4、设置访问虚拟主机的用户名、密码
connectionFactory.setUsername("ems");
connectionFactory.setPassword("123");
//5、设置连接哪个虚拟主机
connectionFactory.setVirtualHost("/emsHost");
//6、获取连接对象
Connection connection = connectionFactory.newConnection();
//7、获取通道对象
Channel channel = connection.createChannel();
//8、通道绑定对应的消息队列: 消费者和生产者在这个方法里面的参数一定要一致
// 参数1 queue: 消息队列的名称,如果该队列不存在,会自动创建; 参数2durable:是否持久化队列
// 参数3 exclusive: 是否独占队列 参数4 autoDelete:消费者消费完成后,是否删除队列
channel.queueDeclare("hello",false,false,false,null);
//9、消费消息
//参数1 queue:消费哪个队列
//参数2 autoAck:开始消息的自动确认机制
//参数3:消费消息时的回调接口
channel.basicConsume("hello",true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException
{ //byte[] body参数:消息队列中取出的消息
System.out.println("新消息"+new String(body));
}
});
//我们不需要去关闭这个通道。因为我们的消费者要不断监听我们的消息队列
}
}
一些参数的解释:
指定通过通道将消息发送给哪个消息队,同一个通道可以向不同的队列发送消息的,如果你绑定的队列和发布消息的队列不一致也是可以的,basicPublish()才是真正的发布消息去具体的某一个队列:
如果队列没有持久化,就不会把这个消息队列保存在磁盘中,队列里面的消息也会丢失,所以当我们的RabbitMQ服务停止/重新启动时,会自动删除我们的队列:
刷新后,没有任何队列:
如果我们设置为true呢,发布一条消息:
重启rabbitmq后:
因为我们把队列持久化了,但是队列里面的消息并没有持久化,所以重启rabbitMQ服务后,队列里面的消息就会丢失,那我们如何既保证队列的持久化,也保证队列里面的消息的持久化呢:
我们要在发布消息时告诉RabbitMQ,要对发布的消息做持久化设置:
在RabbitMQ正常启动时,消息队列和队列里面的消息是在内存中的, RabbitMQ服务停止后,就会把队列和队列里的消息持久化到硬盘中,再次启动时,会把队列中的消息进行恢复。
独占队列:我们的队列只能被当前通道所绑定,不能被其他的连接所绑定,如果有其他的通道或连接再使用此队列的话,会直接报错,一般设置为false:
autoDelete:消费者在消费完队列,并且彻底与队列断开连接后,此队列会自动删除 ,如果为true的话,会有此标签:
四、工作模式:
4.1、概念:
当消息处理比较耗时的时候,可能生产者生产消息的速度会远远大于消费者消费消息的速度。长此以往,消息队列中的消息就会产生堆积,越来越多,无法及时处理。此时就可以使用work 模型:
让多个消费者绑定同一个消息队列,共同消费队列中的消息,队列中的消息一旦消费,就会消失,因此任务是不会被重复执行的,并且我们希望消息处理快的消费者能处理更多的消息。
生产者还是直接绑定队列,没有经过中间的交换机
4.2、代码实现:
工具类:
public class RabbitMQUtils {
private static ConnectionFactory connectionFactory;
static{
//重量级资源,类加载时执行,并且只执行一次
connectionFactory = new ConnectionFactory();
connectionFactory.setHost("123.57.144.31");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/emsHost");
connectionFactory.setUsername("ems");
connectionFactory.setPassword("123");
}
public static Connection getConnection(){
try {
return connectionFactory.newConnection();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (TimeoutException e) {
throw new RuntimeException(e);
}
}
public static void closeConnectionAndChanel(Channel channel, Connection connection){
try{
if(channel != null) channel.close();
if(connection != null) connection.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
生产者:
public class Provider {
//while循环,模拟不断的往我们的消息队列中发送消息。
public static void main(String[] args) throws IOException {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
//通道绑定对应的消息队列: 消费者和生产者在这个方法里面的参数一定要一致
// 参数1 queue: 消息队列的名称,如果该队列不存在,会自动创建; 参数2durable:是否持久化队列
// 参数3 exclusive: 是否独占队列 参数4 autoDelete:消费者消费完成后,是否删除队列
channel.queueDeclare("work",true,false,false,null);
for (int i = 0; i < 10; i++) {
// 发布消息
// 参数1 exchange:交换机名称,workqueue模式中不涉及交换机 参数2:队列名称
// 参数3 basicProperties:消息持久化 参数4:具体的消息内容
channel.basicPublish("","work", MessageProperties.PERSISTENT_TEXT_PLAIN,("第"+i+"个任务:work queue模式").getBytes());
}
RabbitMQUtils.closeConnectionAndChanel(channel,connection);
}
}
传统的消费者1:
public class Consumer1 {
public static void main(String[] args) throws IOException {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
// 通道绑定对应的消息队列: 消费者和生产者在这个方法里面的参数一定要一致
// 参数1 queue: 消息队列的名称,如果该队列不存在,会自动创建; 参数2durable:是否持久化队列
// 参数3 exclusive: 是否独占队列 参数4 autoDelete:消费者消费完成后,是否删除队列
channel.queueDeclare("work",true,false,false,null);
// 消费消息
//参数1 queue:消费哪个队列
//参数2 autoAck:开始消息的自动确认机制
//参数3:消费消息时的回调接口
channel.basicConsume("work",true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException
{
System.out.println("消费者1"+new String(body));
}
});
}
}
传统的消费者2:
public class Consumer2 {
public static void main(String[] args) throws IOException {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
// 通道绑定对应的消息队列: 消费者和生产者在这个方法里面的参数一定要一致
// 参数1 queue: 消息队列的名称,如果该队列不存在,会自动创建; 参数2durable:是否持久化队列
// 参数3 exclusive: 是否独占队列 参数4 autoDelete:消费者消费完成后,是否删除队列
channel.queueDeclare("work",true,false,false,null);
// 消费消息
//参数1 queue:消费哪个队列
//参数2 autoAck:开始消息的自动确认机制
//参数3:消费消息时的回调接口
channel.basicConsume("work",true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException
{
System.out.println("消费者2"+new String(body));
}
});
}
}
测试:
我们先开启消费者1、2,然后再开启生产者:
由上图可以看出,在workqueue模式中,默认情况下,RabbitMQ会按顺序的将每个消息发送给下一个消费者,也就是将消息平均分配,轮询给消费者。平均而言,每个消费者都会收到相同数量的消息。这跟我们消息的自动确认机制有关,在 channel.basicConsume(“work”,true,new DefaultConsumer(channel)中的第二个参数设置为true,表示消息会自动确认,即消费者只要一拿到消息,就会告诉生产者消费成功,生产者都会将消息队列中的消息继续发给该消费者,RabbitMQ 会自动把发送出去的消息置为确认,然后从内存(或磁盘)中删除,不管消费者是否真正地消费到了这些消息,在这种情况下,突然有个消费者挂掉了,我们将丢失正在处理的消息,以及后续发送给该消费者的消息; 二是我们更希望能者多劳,处理快的能多处理一点,处理慢的能少处理一点,不想让它出现这种平均处理的这种情况。
消息应答机制:
关闭消息的自动确认机制,开启手动确认。为了保证消息在发送过程中不丢失,RabbitMQ 引入了消息应答机制,消息应答就是:消费者在接收到消息并且处理该消息之后,手动确认,告诉 RabbitMQ 它已经处理了,RabbitMQ 可以把该消息删除了。
采用消息应答机制后,只要设置 autoAck 参数为 false,消费者就有足够的时间处理消息(任务),不用担心处理消息过程中消费者进程挂掉后消息丢失的问题,因为 RabbitMQ 会一直等待消费者消费完消息并且手动确认,显式调用 Basic.Ack 命令为止。(即告诉 RabbitMQ 它已经处理了,RabbitMQ 才可以把该消息删除了。)
当 autoAck 参数置为 false,对于 RabbitMQ 服务端而言,队列中的消息分成了两个部分:一部分是等待投递给消费者的消息;一部分是已经投递给消费者,但是还没有收到消费者确认信号的消息。如果 RabbitMQ 一直没有收到消费者的确认信号,并且消费此消息的消费者已经断开连接,则 RabbitMQ 会安排该消息重新进入队列,等待投递给下一个消费者,当然也有可能还是原来的那个消费者。
RabbitMQ 不会为未确认的消息设置过期时间,它判断此消息是否需要重新投递给消费者的唯一依据是消费该消息的消费者连接是否已经断开,这么设计的原因是 RabbitMQ 允许消费者消费一条消息的时间可以很久很久。
改进方式:
设置通道一次只能消费一个消息:
channel.basicQos(1) //一次只接受一条未确认的消息
关闭消息的自动确认,开启手动确认消息
//手动确认 参数1:手动确认消息标识 参数2:false 每次确认一个:
channel.basicAck(envelpoe.getDeliveryTag(),false)
第一个参数:它得需要我们传一个类型标志,队列中那么多消息,你确认的是队列中的哪个具体的消息。
第二个参数:是否开启多个消息同时确认。我们通道中只有一个消息,每次确认一个就行了
消费者1:
public class Consumer1 {
public static void main(String[] args) throws IOException {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
// 通道绑定对应的消息队列: 消费者和生产者在这个方法里面的参数一定要一致
// 参数1 queue: 消息队列的名称,如果该队列不存在,会自动创建; 参数2durable:是否持久化队列
// 参数3 exclusive: 是否独占队列 参数4 autoDelete:消费者消费完成后,是否删除队列
channel.basicQos(1);
channel.queueDeclare("work",true,false,false,null);
// 消费消息
//参数1 queue:消费哪个队列
//参数2 autoAck:开始消息的自动确认机制
//参数3:消费消息时的回调接口
channel.basicConsume("work",false,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("消费者1"+new String(body));
channel.basicAck(envelope.getDeliveryTag(),false);
}
});
}
}
消费者2:
public class Consumer2 {
public static void main(String[] args) throws IOException {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
// 通道绑定对应的消息队列: 消费者和生产者在这个方法里面的参数一定要一致
// 参数1 queue: 消息队列的名称,如果该队列不存在,会自动创建; 参数2durable:是否持久化队列
// 参数3 exclusive: 是否独占队列 参数4 autoDelete:消费者消费完成后,是否删除队列
channel.basicQos(1);
channel.queueDeclare("work",true,false,false,null);
// 消费消息
//参数1 queue:消费哪个队列
//参数2 autoAck:开始消息的自动确认机制
//参数3:消费消息时的回调接口
channel.basicConsume("work",false,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException
{
System.out.println("消费者2"+new String(body));
channel.basicAck(envelope.getDeliveryTag(),false);
}
});
}
}
五、“fanout”模式:
fanout也称为广播
前两种模式中,我们的交换机都写的是空字符串。不是说没用交换机,是用的这个RabbitMQ 中给我们提供的这种默认交换机。之前我们在给大家讲AMQP的协议的时候,我们也给大家说过,说交换机实际上是AMQP协议中的比较重要的一个一点。
生产者对接的已经不再是某一个队列了,生产者把消息发给交换机:也就是说生产者以后不直接与队列打交道了。生产者把消息发到交换机里面,由交换机去决定,把交换机中的这些消息广播给咱们的哪些队列,每一个消费者对应的是一个临时队列,这个临时队列用来接收咱们交换机的广播的消息,实现一条消息被多个消费者的消费
生产者:
public class Provider {
public static void main(String[] args) throws IOException {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
//通道声明指定的交换机 参数1:交换机名称 参数2:交换机类型 fanout广播类型
// 在交换机不存在的时候创建这个logs交换机
channel.exchangeDeclare("logs","fanout");
//发布消息
for (int i = 0; i < 10; i++) {
//参数一:交换机的名字 参数二:routingKey,在这种fanout模式中,这个routingKey是没有任何意义的。
//参数三:消息持久化的特性 参数四:具体的消息
channel.basicPublish("logs","",null,"这是fanout模型".getBytes());
}
RabbitMQUtils.closeConnectionAndChanel(channel,connection);
}
}
绑定交换机,生成临时队列,绑定交换机和临时队列
消费者1:
public class Consumer1 {
public static void main(String[] args) throws IOException {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
// 通道声明指定的交换机 参数1:交换机名称 参数2:交换机类型:fanout广播类型
// 在交换机不存在的时候创建这个logs交换机
channel.exchangeDeclare("logs","fanout");
// 临时队列
String queueName = channel.queueDeclare().getQueue();
// 绑定交换机和队列
// 参数一: 队列的名称 参数二:交换机的名称 参数三:routingKey
// routingKey在fanout模型里面没有意义
channel.queueBind(queueName,"logs","");
// 消费消息
//参数1 queue:消费哪个队列
//参数2 autoAck:开始消息的自动确认机制
//参数3:消费消息时的回调接口
channel.basicConsume(queueName,true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("消费者1:"+new String(body));
}
});
}
}
消费者2:
public class Consumer2 {
public static void main(String[] args) throws IOException {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
// 通道声明指定的交换机 参数1:交换机名称 参数2:交换机类型:fanout广播类型
// 在交换机不存在的时候创建这个logs交换机
channel.exchangeDeclare("logs","fanout");
// 临时队列
String queueName = channel.queueDeclare().getQueue();
// 绑定交换机和队列
// 参数一: 队列的名称 参数二:交换机的名称 参数三:routingKey
// routingKey在fanout模型里面没有意义
channel.queueBind(queueName,"logs","");
// 消费消息
//参数1 queue:消费哪个队列
//参数2 autoAck:开始消息的自动确认机制
//参数3:消费消息时的回调接口
channel.basicConsume(queueName,true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("消费者2:"+new String(body));
}
});
}
}
六、Routing模式:
在Routing模型下
队列与交换机的绑定,不能是任意绑定了,而是要指定一个 RoutingKey (路由key) 消息的发送方在向Exchange发送消息时,也必须指定消息的 RoutingKey。 Exchange不再把消息交给每一个绑定的队列,而是根据消息的 RoutingKey进行判断,只有队列的 RoutingKey与消息的 Routing key 完全一致,才会接收到消息
fanout广播模型,只要生产者一发送,交换机会把所有的消息发给所有的广播者。在广播模式中,一条消息会被所有订阅的队列都消费,但是在某种场景下,我们希望不同的消息被不同的队列消费。这时候就要用到direct的类型的交换机。
direct类型的的交换机,它的核心作用就是不同的消息可以被不同消费者消费,同时我们也可以让所有的消费者去消费这一个消息,生产者向交换机发消息,不能单纯的再发消息了,我在发消息的同时还要指定一个路由key,我们可以通过路由key的不同去决定我们的消息可以被哪些消费者消费。
生产者:
public class Provider {
public static void main(String[] args) throws IOException {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
// 通道声明指定的交换机 参数1:交换机名称 参数2:交换机类型
// 当交换机不存在的时候会创建这个Routing_direct交换机
channel.exchangeDeclare("Routing_direct","direct");
//指定路由key
String routingKey = "info";
//发布消息 参数一:交换机的名字 参数二:routingKey
// 参数三:消息持久化的特性 参数四:具体的消息
channel.basicPublish("Routing_direct",routingKey,null,("这是Routing模型发布的,交换机类型为direct,routingKey为:"+routingKey).getBytes());
RabbitMQUtils.closeConnectionAndChanel(channel,connection);
}
}
消费者1:
public class Consumer1 {
public static void main(String[] args) throws IOException {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
// 通道声明指定的交换机 参数1:交换机名称 参数2:交换机类型
// 当交换机不存在的时候会创建这个Routing_direct交换机
channel.exchangeDeclare("Routing_direct","direct");
// 临时队列
String queueName = channel.queueDeclare().getQueue();
// 基于Routingkey绑定交换机和队列
// 参数一:队列名称 参数二:交换机名称 参数三: Routingkey
channel.queueBind(queueName,"Routing_direct","error");
channel.basicConsume(queueName,true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException
{
System.out.println("消费者1,具体消息为:"+new String(body));
}
});
}
}
消费者2:
public class Consumer2 {
public static void main(String[] args) throws IOException {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
// 通道声明指定的交换机 参数1:交换机名称 参数2:交换机类型
// 当交换机不存在的时候会创建这个Routing_direct交换机
channel.exchangeDeclare("Routing_direct","direct");
// 临时队列
String queueName = channel.queueDeclare().getQueue();
// 基于Routingkey绑定交换机和队列
// 参数一:队列名称 参数二:交换机名称 参数三: Routingkey
channel.queueBind(queueName,"Routing_direct","info");
channel.queueBind(queueName,"Routing_direct","error");
channel.queueBind(queueName,"Routing_direct","warning");
channel.basicConsume(queueName,true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException
{
System.out.println("消费者2,具体消息为:"+new String(body));
}
});
}
}
七、Topics模式:
Topic类型的Exchange与Routing相比,都是可以根据RoutingKey把消息路由到不同的队列。只不过Topic类型Exchange可以让队列在绑定Routing key 的时候使用通配符!这种模型Routingkey 一般都是由一个或多个单词组成,多个单词之间以”.”分割,例如: lucky.dog.
统配符 拼多多帮助力入口
* 匹配不多不少恰好1个词# 匹配一个或多个词
如:
lucky.# 匹配lucky.dog.cat或者 lucky.dog或者lucky等lucky.* 只能匹配 lucky.dog
生产者:
public class Provider {
public static void main(String[] args) throws IOException {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
// 通道声明指定的交换机 参数1:交换机名称 参数2:交换机类型
// 当交换机不存在的时候会创建这个topics_exchange交换机
channel.exchangeDeclare("topics_exchange","topic");
//指定路由key
String routingKey = "user.save";
//发布消息 参数一:交换机的名字 参数二:routingKey
// 参数三:消息持久化的特性 参数四:具体的消息
channel.basicPublish("topics_exchange",routingKey,null,("这里是topics动态路由,routingKey为:"+routingKey).getBytes());
RabbitMQUtils.closeConnectionAndChanel(channel,connection);
}
}
消费者1:
public class Consumer1 {
public static void main(String[] args) throws IOException {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
// 通道声明指定的交换机 参数1:交换机名称 参数2:交换机类型
// 当交换机不存在的时候会创建这个topics_exchange交换机
channel.exchangeDeclare("topics_exchange","topic");
// 临时队列
String queueName = channel.queueDeclare().getQueue();
// 基于Routingkey绑定交换机和队列
// 参数一:队列名称 参数二:交换机名称 参数三: Routingkey
channel.queueBind(queueName,"topics_exchange","user.*");
channel.basicConsume(queueName,true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException
{
System.out.println("消费者1,具体消息为:"+new String(body));
}
});
}
}
消费者2:
public class Consumer2 {
public static void main(String[] args) throws IOException {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
// 通道声明指定的交换机 参数1:交换机名称 参数2:交换机类型
// 当交换机不存在的时候会创建这个topics_exchange交换机
channel.exchangeDeclare("topics_exchange","topic");
// 临时队列
String queueName = channel.queueDeclare().getQueue();
// 基于Routingkey绑定交换机和队列
// 参数一:队列名称 参数二:交换机名称 参数三: Routingkey
channel.queueBind(queueName,"topics_exchange","user.#");
channel.basicConsume(queueName,true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException
{
System.out.println("消费者2,具体消息为:"+new String(body));
}
});
}
}
精彩链接