RabbitMQ消息服务中Topic类型交换机根据通配符路由消息,*代表一个单词,#代表代表0或多个单词。

 
生产者

消费者

 
代码
Producer.java
 
package com.test.topic2;
 
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
 
public class Producer {
public static void main(String[] args)
{
try
{
//1.
ConnectionFactory cf = new ConnectionFactory();
cf.setUsername("admin");
cf.setPassword("admin");
cf.setHost("192.168.169.142");
cf.setPort(5672);
//2.
Connection con = cf.newConnection();
//3.
Channel channel = con.createChannel();
//4.
String queue1 = "topic_queue1";
String queue2 = "topic_queue2";
String queue3 = "topic_queue3";
//5.
channel.queueDeclare(queue1, false, false, false, null);
channel.queueDeclare(queue2, false, false, false, null);
channel.queueDeclare(queue3, false, false, false, null);
//6.
String exg = "topic_exg";
channel.exchangeDeclare(exg, "topic", false);
//7.
channel.queueBind(queue1, exg, "*.test");//Binding key
channel.queueBind(queue2, exg, "#.test");//Binding key
channel.queueBind(queue3, exg, "my.user.test");//Binding
key
//8.
String message = "Hello Topic Message";
channel.basicPublish(exg, "user.test",
MessageProperties.TEXT_PLAIN, message.getBytes());
//9.
channel.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
 
Customer.java
 
package com.test.topic2;
 
import java.io.IOException;
 
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.ShutdownSignalException;
 
public class Customer implements
com.rabbitmq.client.Consumer{
public static void main(String[] args)
{
try
{
//1.
ConnectionFactory cf = new ConnectionFactory();
cf.setUsername("admin");
cf.setPassword("admin");
cf.setHost("192.168.169.142");
cf.setPort(5672);
//2.
Connection con = cf.newConnection();
//3.
Channel channel = con.createChannel();
//4.
String queue1 = "topic_queue1";
channel.queueDeclare(queue1, false, false, false, null);
com.test.topic2.Customer cust = new
com.test.topic2.Customer();
channel.basicConsume(queue1, true, cust);
 
Thread.sleep(5000);
channel.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
 
@Override
public void handleConsumeOk(String consumerTag) {
// TODO Auto-generated method stub
}
 
@Override
public void handleCancelOk(String consumerTag) {
// TODO Auto-generated method stub
}
 
@Override
public void handleCancel(String consumerTag) throws
IOException {
// TODO Auto-generated method stub
}
 
@Override
public void handleDelivery(java.lang.String consumerTag,
     
      Envelope
envelope,
     
     
AMQP.BasicProperties properties,
     
      byte[]
body) throws IOException {
// TODO Auto-generated method stub
System.out.println("receive=" + new String(body));
}
 
@Override
public void handleShutdownSignal(String consumerTag,
ShutdownSignalException sig) {
// TODO Auto-generated method stub
}
 
@Override
public void handleRecoverOk(String consumerTag) {
// TODO Auto-generated method stub
}
}
 

最新文章

  1. [开源 .NET 跨平台 数据采集 爬虫框架: DotnetSpider] [二] 基本使用
  2. tcpdump用法
  3. js限制文本框只可以输入数字
  4. HDU3657 Game(最小割)
  5. python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
  6. 分段统计与Oracle的分析函数、逻辑判断等知识点的综合运用
  7. HW3.27
  8. Aspose.cells异步读写操作
  9. ST-4
  10. Perf工具
  11. Socket通信例子
  12. Shell 脚本合集
  13. Alpha冲刺(2/10)——追光的人
  14. 字符编码——python学习
  15. 如何查找消耗资源较大的SQL
  16. Zabbix Windos agent 安装
  17. 前端之HTML和CSS
  18. 包嗅探和包回放简介-tcpdump,tcpreplay
  19. Microsoft Data Access Components 2.8
  20. 数据库sql语句的exists和in的区别

热门文章

  1. Magento笔记/记录(1)
  2. 【dart学习】-- dart 安装开发环境
  3. 关于Web前端密码加密是否有意义的总结
  4. springboot接口:CommandLineRunner
  5. split("\\s+") 和 split(" +") 有什么区别?
  6. linux jps命令
  7. IntelliJ快捷键记录
  8. 0 ‘+new Array(017)’ 输出? js+相当于Number()类型转换
  9. Java 8 终于支持 Docker !
  10. [USACO06JAN]牛的舞会The Cow Prom