1.消费者
package com.touch.topic;
import com.rabbitmq.client.*;
import com.touch.utils.RabbitMQUtil;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* 专门用于接受 *.news 消息
*/
public class TestCustomer4News {
public final static String EXCHANGE_NAME="topics_exchange";
public static void main(String[] args) throws IOException, TimeoutException {
//为当前消费者取名称
final String name = "consumer-news";
//判断服务器是否启动
RabbitMQUtil.checkServer();
// 创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
//设置RabbitMQ地址
factory.setHost("localhost");
//创建一个新的连接
Connection connection = factory.newConnection();
//创建一个通道
Channel channel = connection.createChannel();
//交换机声明(参数为:交换机名称;交换机类型)
channel.exchangeDeclare(EXCHANGE_NAME,"topic");
//获取一个临时队列
String queueName = channel.queueDeclare().getQueue();
//接受 USA 信息
channel.queueBind(queueName, EXCHANGE_NAME, "*.news");
System.out.println(name +" 等待接受消息");
//DefaultConsumer类实现了Consumer接口,通过传入一个频道,
// 告诉服务器我们需要那个频道的消息,如果频道中有消息,就会执行回调函数handleDelivery
Consumer consumer = new DefaultConsumer(channel) {
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body)
throws IOException {
String message = new String(body, "UTF-8");
System.out.println(name + " 接收到消息 '" + message + "'");
}
};
//自动回复队列应答 -- RabbitMQ中的消息确认机制
channel.basicConsume(queueName, true, consumer);
}
}
2.消费者
package com.touch.topic;
import com.rabbitmq.client.*;
import com.touch.utils.RabbitMQUtil;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* 专门用于接受 usa.* 消息
*/
public class TestCustomer4USA {
public final static String EXCHANGE_NAME = "topics_exchange";
public static void main(String[] args) throws IOException, TimeoutException {
//为当前消费者取名称
final String name = "consumer-usa";
//判断服务器是否启动
RabbitMQUtil.checkServer();
// 创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
//设置RabbitMQ地址
factory.setHost("localhost");
//创建一个新的连接
Connection connection = factory.newConnection();
//创建一个通道
Channel channel = connection.createChannel();
//交换机声明(参数为:交换机名称;交换机类型)
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
//获取一个临时队列
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, EXCHANGE_NAME, "usa.*");
System.out.println(name +" 等待接受消息");
//DefaultConsumer类实现了Consumer接口,通过传入一个频道,
// 告诉服务器我们需要那个频道的消息,如果频道中有消息,就会执行回调函数handleDelivery
Consumer consumer = new DefaultConsumer(channel) {
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body)
throws IOException {
String message = new String(body, "UTF-8");
System.out.println(name + " 接收到消息 '" + message + "'");
}
};
//自动回复队列应答 -- RabbitMQ中的消息确认机制
channel.basicConsume(queueName, true, consumer);
}
}
3.生产者
package com.touch.topic;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.touch.utils.RabbitMQUtil;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* 四个路由:"usa.news", "usa.weather", "europe.news", "europe.weather"
* 上发布 "美国新闻", "美国天气", "欧洲新闻", "欧洲天气"
* 于是就能在消费者端看到 不同的主题收到对应的消息了。
* 例如一个消费者是美国主题:能收到"美国新闻", "美国天气";
* 一个消费者是天气主题:能收到"美国天气","欧洲天气"
*/
public class TestProducer {
public final static String EXCHANGE_NAME="topics_exchange";
public static void main(String[] args) throws IOException, TimeoutException {
RabbitMQUtil.checkServer();
//创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
//设置RabbitMQ相关信息
factory.setHost("localhost");
//创建一个新的连接
Connection connection = factory.newConnection();
//创建一个通道
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
String[] routing_keys = new String[]{"usa.news","usa.weather","europe.news","europe.weather"};
String[] messages = new String[] { "美国新闻", "美国天气", "欧洲新闻", "欧洲天气" };
for (int d = 0; d < routing_keys.length; d++) {
String routingkey=routing_keys[d];
String message = messages[d];
channel.basicPublish(EXCHANGE_NAME,routingkey,null,message.getBytes());
System.out.printf("发送到消息的路由: %s,内容是: %s%n ",routingkey,message);
}
channel.close();
connection.close();
}
}
[源代码地址:https://github.com/hyghub/javanotes]
[学 习 地 址:http://how2j.cn/k/message/message-rabbitmq-type/2031.html]