public class TopicsProducer {
//队列名称
private static final String QUEUE_INFORM_1 = "topic_exchange_queue_1";
private static final String QUEUE_INFORM_2 = "topic_exchange_queue_2";
private static final String QUEUE_INFORM_3 = "topic_exchange_queue_3";
private static final String EXCHANGE_TOPICS_INFORM="exchange_topics_inform";
private static final String ROUTINGKEY_UPDATE="item.update";
private static final String ROUTINGKEY_DELETE="item.delete";
private static final String ROUTINGKEY_INSTALL="item.install";
private static final String ROUTINGKEY_ALL="item.#";
public static void main(String[] args) {
Connection connection = null;
Channel channel = null;
try {
//建立新连接
connection = ConnectionUtil.getConnection();
//创建会话通道,生产者和mq服务所有通信都在channel通道中完成
channel = connection.createChannel();
channel.queueDeclare(QUEUE_INFORM_1, true, false, false, null);
channel.queueDeclare(QUEUE_INFORM_2, true, false, false, null);
channel.queueDeclare(QUEUE_INFORM_3, true, false, false, null);
//声明一个交换机
channel.exchangeDeclare(EXCHANGE_TOPICS_INFORM, BuiltinExchangeType.TOPIC);
//进行交换机和队列绑定
channel.queueBind(QUEUE_INFORM_1, EXCHANGE_TOPICS_INFORM, ROUTINGKEY_UPDATE);
channel.queueBind(QUEUE_INFORM_1, EXCHANGE_TOPICS_INFORM, ROUTINGKEY_DELETE);
channel.queueBind(QUEUE_INFORM_2, EXCHANGE_TOPICS_INFORM, ROUTINGKEY_INSTALL);
channel.queueBind(QUEUE_INFORM_3, EXCHANGE_TOPICS_INFORM, ROUTINGKEY_ALL);
//发送消息
//发送消息的时候指定routingKey
String message = "新增商品";
channel.basicPublish(EXCHANGE_TOPICS_INFORM, "item.install", null, message.getBytes());
// String message = "删除商品";
// channel.basicPublish(EXCHANGE_TOPICS_INFORM, "item.delete", null, message.getBytes());
// String message = "修改商品";
// channel.basicPublish(EXCHANGE_TOPICS_INFORM, "item.update", null, message.getBytes());
System.out.println("send to mq " + message);
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭连接
//先关闭通道
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class TopicsComsumer1 {
//队列名称
private static final String QUEUE_INFORM_1 = "topic_exchange_queue_1";;
private static final String EXCHANGE_TOPICS_INFORM="exchange_topics_inform";
private static final String ROUTINGKEY_UPDATE="item.update";
private static final String ROUTINGKEY_DELETE="item.delete";
public static void main(String[] args) throws Exception {
//建立新连接
Connection connection = ConnectionUtil.getConnection();
//创建会话通道,生产者和mq服务所有通信都在channel通道中完成
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_INFORM_1,true,false,false,null);
//声明一个交换机
channel.exchangeDeclare(EXCHANGE_TOPICS_INFORM, BuiltinExchangeType.TOPIC);
//进行交换机和队列绑定
channel.queueBind(QUEUE_INFORM_1, EXCHANGE_TOPICS_INFORM,ROUTINGKEY_UPDATE);
channel.queueBind(QUEUE_INFORM_1, EXCHANGE_TOPICS_INFORM,ROUTINGKEY_DELETE);
//实现消费方法
DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//交换机
String exchange = envelope.getExchange();
//消息内容
String message= new String(body,"utf-8");
System.out.println("【消费者1】:"+message);
}
};
//监听队列
channel.basicConsume(QUEUE_INFORM_1,true,defaultConsumer);
}
}
public class TopicsComsumer2 {
//队列名称
private static final String QUEUE_INFORM_2 = "topic_exchange_queue_2";
private static final String EXCHANGE_TOPICS_INFORM="exchange_topics_inform";
private static final String ROUTINGKEY_INSTALL="item.install";
public static void main(String[] args) throws Exception {
//建立新连接
Connection connection = ConnectionUtil.getConnection();
//创建会话通道,生产者和mq服务所有通信都在channel通道中完成
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_INFORM_2,true,false,false,null);
//声明一个交换机
channel.exchangeDeclare(EXCHANGE_TOPICS_INFORM, BuiltinExchangeType.TOPIC);
//进行交换机和队列绑定
channel.queueBind(QUEUE_INFORM_2, EXCHANGE_TOPICS_INFORM,ROUTINGKEY_INSTALL);
//实现消费方法
DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//交换机
String exchange = envelope.getExchange();
//消息内容
String message= new String(body,"utf-8");
System.out.println("【消费者2】:"+message);
}
};
//监听队列
channel.basicConsume(QUEUE_INFORM_2,true,defaultConsumer);
}
}
public class TopicsComsumer3 {
//队列名称
private static final String QUEUE_INFORM_3 = "topic_exchange_queue_3";
private static final String EXCHANGE_TOPICS_INFORM="exchange_topics_inform";
private static final String ROUTINGKEY_ALL="item.#";
public static void main(String[] args) throws Exception {
//建立新连接
Connection connection = ConnectionUtil.getConnection();
//创建会话通道,生产者和mq服务所有通信都在channel通道中完成
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_INFORM_3,true,false,false,null);
//声明一个交换机
channel.exchangeDeclare(EXCHANGE_TOPICS_INFORM, BuiltinExchangeType.TOPIC);
//进行交换机和队列绑定
channel.queueBind(QUEUE_INFORM_3, EXCHANGE_TOPICS_INFORM,ROUTINGKEY_ALL);
//实现消费方法
DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//交换机
String exchange = envelope.getExchange();
//消息内容
String message= new String(body,"utf-8");
System.out.println("【消费者3】:"+message);
}
};
//监听队列
channel.basicConsume(QUEUE_INFORM_3,true,defaultConsumer);
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |