Topic exchange
这一章说的主题交换和上一章,很类似,可以说是上一章的加强版吧!!!
上一章的路由太局限了,看看这章,你就知道了。。。
还是用图说话吧!这图是啥意思呢?和上一章一样,也是可以理解对消息的过滤。
好了,现在一个消息来了,这个消息的路由键值是:
quick.orange.rabbit----Q1、Q2都可以接收到
lazy.orange.elephant----Q1、Q2都可以接收到
quick.orange.fox----只有Q1可以接收到
lazy.brown.fox----只有Q2可以接收到
quick.brown.fox----不匹配,将被丢弃
quick.orange.male.rabbit?----结果是被丢弃
lazy.orange.male.rabbit?----结果是被Q2接收
所以,小结一下,当使用#作为key时,就相当于fanout exchange,当不使用“#”和“*”时,就相当于direct exchange。
package topic;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
public class EmitLogTopic {
private static final String EXCHANGE_NAME = "topic_logs";
public static void main(String[] argv) {
Connection connection = null;
Channel channel = null;
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
connection = factory.newConnection();
channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
String[] routingKeys = new String[]{"quick.orange.rabbit","lazy.orange.elephant","quick.orange.fox"
,"lazy.brown.fox","quick.brown.fox","quick.orange.male.rabbit","lazy.orange.male.rabbit"};
String message = "hello world!";
for (String routingKey : routingKeys) {
channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (connection != null) {
try {
connection.close();
}
catch (Exception ignore) {}
}
}
}
private static String getRouting(String[] strings){
if (strings.length < 1)
return "anonymous.info";
return strings[0];
}
private static String getMessage(String[] strings){
if (strings.length < 2)
return "Hello World!";
return joinStrings(strings, " ", 1);
}
private static String joinStrings(String[] strings, String delimiter, int startIndex) {
int length = strings.length;
if (length == 0 ) return "";
if (length < startIndex ) return "";
StringBuilder words = new StringBuilder(strings[startIndex]);
for (int i = startIndex + 1; i < length; i++) {
words.append(delimiter).append(strings[i]);
}
return words.toString();
}
}
package topic;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
public class ReceiveLogsTopic {
private static final String EXCHANGE_NAME = "topic_logs";
public static void main(String[] argv) {
// argv = new String[]{"*.orange.*"};
argv = new String[]{"*.*.rabbit","lazy.#"};
Connection connection = null;
Channel channel = null;
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
connection = factory.newConnection();
channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
String queueName = channel.queueDeclare().getQueue();
if (argv.length < 1){
System.err.println("Usage: ReceiveLogsTopic [binding_key]...");
System.exit(1);
}
for(String bindingKey : argv){
channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);
System.out.println(bindingKey+"、");
}
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, true, consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
String routingKey = delivery.getEnvelope().getRoutingKey();
System.out.println(" [x] Received '" + routingKey + "':'" + message + "'");
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (connection != null) {
try {
connection.close();
}
catch (Exception ignore) {}
}
}
}
}
结果:
[x] Sent 'quick.orange.rabbit':'hello world!'
[x] Sent 'lazy.orange.elephant':'hello world!'
[x] Sent 'quick.orange.fox':'hello world!'
[x] Sent 'lazy.brown.fox':'hello world!'
[x] Sent 'quick.brown.fox':'hello world!'
[x] Sent 'quick.orange.male.rabbit':'hello world!'
[x] Sent 'lazy.orange.male.rabbit':'hello world!'
Q1:
*.orange.*、
[*] Waiting for messages. To exit press CTRL+C
[x] Received 'quick.orange.rabbit':'hello world!'
[x] Received 'lazy.orange.elephant':'hello world!'
[x] Received 'quick.orange.fox':'hello world!'
Q2:
*.*.rabbit、
lazy.#、
[*] Waiting for messages. To exit press CTRL+C
[x] Received 'quick.orange.rabbit':'hello world!'
[x] Received 'lazy.orange.elephant':'hello world!'
[x] Received 'lazy.brown.fox':'hello world!'
[x] Received 'lazy.orange.male.rabbit':'hello world!'