在介绍了rabbitmq的Routing模式之后,这一节我们将阐述它的topics模式的用法。
1、前提约束
- 已经完成rabbitmq的第一个简单的测试程序
https://www.jianshu.com/p/77bfc4fe5a1a -
2、操作步骤
我们马上要测试的rabbitmq的工作模式如下:topics - 在src/main/java文件夹下创建包net.wanho.rabbitmq.topics
- 在net.wanho.rabbitmq.topics创建Producer.java
package net.wanho.rabbitmq.topics;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
/**
* 根据用户的通知设置去通知用户,设置接收Email的用户只接收Email,
* 设置接收sms的用户只接收sms,设置两种 通知类型都接收的则两种通知都有效。
*/
public class producer {
public static void main(String[] args) throws Exception {
String QUEUE_INFORM_EMAIL = "queue_inform_email";
String QUEUE_INFORM_SMS = "queue_inform_sms";
String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";
Connection connection = null;
Channel channel = null;
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
factory.setPort(5672);
factory.setUsername("guest");
factory.setPassword("guest");
factory.setVirtualHost("/");
connection = factory.newConnection();
channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_TOPICS_INFORM, BuiltinExchangeType.TOPIC);
channel.queueDeclare(QUEUE_INFORM_EMAIL, true, false, false, null);
channel.queueDeclare(QUEUE_INFORM_SMS, true, false, false, null);
for (int i = 0; i < 10; i++) {
String message = "email inform to user" + i;
channel.basicPublish(EXCHANGE_TOPICS_INFORM, "inform.email", null, message.getBytes());
System.out.println("Send Message is:'" + message + "'");
}
for (int i = 0; i < 10; i++) {
String message = "sms inform to user" + i;
channel.basicPublish(EXCHANGE_TOPICS_INFORM, "inform.sms", null, message.getBytes());
System.out.println("Send Message is:'" + message + "'");
}
for (int i = 0; i < 10; i++) {
String message = "sms and email inform to user" + i;
channel.basicPublish(EXCHANGE_TOPICS_INFORM, "inform.sms.email", null, message.getBytes());
System.out.println("Send Message is:'" + message + "'");
}
}
}
- 在net.wanho.rabbitmq.topics创建Consumer1.java
package net.wanho.rabbitmq.topics;
import com.rabbitmq.client.*;
import java.io.IOException;
class Consumer1 {
public static void main(String[] args) throws Exception {
String QUEUE_INFORM_EMAIL = "queue_inform_email";
String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
factory.setPort(5672);
factory.setUsername("guest");
factory.setPassword("guest");
factory.setVirtualHost("/");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_INFORM_EMAIL, true, false, false, null);
channel.exchangeDeclare(EXCHANGE_TOPICS_INFORM, BuiltinExchangeType.TOPIC);
channel.queueBind(QUEUE_INFORM_EMAIL, EXCHANGE_TOPICS_INFORM, "inform.#");
DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException {
long deliveryTag = envelope.getDeliveryTag();
String exchange = envelope.getExchange();
String message = new String(body, "utf-8");
System.out.println(message);
}
};
channel.basicConsume(QUEUE_INFORM_EMAIL, true, defaultConsumer);
}
}
- 在net.wanho.rabbitmq.topics创建Consumer2.java
package net.wanho.rabbitmq.topics;
import com.rabbitmq.client.*;
import java.io.IOException;
public class Consumer2 {
public static void main(String[] args) throws Exception {
String QUEUE_INFORM_SMS = "queue_inform_sms";
String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
factory.setPort(5672);
factory.setUsername("guest");
factory.setPassword("guest");
factory.setVirtualHost("/");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_INFORM_SMS, true, false, false, null);
channel.exchangeDeclare(EXCHANGE_TOPICS_INFORM, BuiltinExchangeType.TOPIC);
channel.queueBind(QUEUE_INFORM_SMS, EXCHANGE_TOPICS_INFORM, "inform.*.email");
DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException {
long deliveryTag = envelope.getDeliveryTag();
String exchange = envelope.getExchange();
String message = new String(body, "utf-8");
System.out.println(message);
}
};
channel.basicConsume(QUEUE_INFORM_SMS, true, defaultConsumer);
}
}
- 测试
启动Consumer1,再启动Consumer2,然后启动Producer,我们会看到Consumer1消费了所有消息,因为它的通配符是"inform.#",Consumer2只消费了key值为inform.sms.email的消息,因为它的通配符是"inform.sms.email"。
以上就是我们完成的rabbitmq的topics模式的测试。