java rabbitmq模拟生产者,消费者demo
1.exchange类型,rabbitmq交换机类型
exchange类型
fanout 扇形交换机,它会把所有发送到该交换机的消息路由到所有与该交换机绑定的队列中。
direct 直连交换机,它会把消息路由到那些BindingKey和RoutingKey完全匹配的队列中。
topic 主题交换机,与direct类似,但它可以通过通配符进行模糊匹配,topic模糊匹配 井号代表全部 星号代表一个字符。
headers 头交换机,不依赖于路由键的匹配规则来路由消息,而是根据发送的消息内容中的headers属性进行匹配,headers类型的交换机性能很差,而且也不实用。
2.
EndPoint.java 定义连接,channel,队列,绑定交换机和路由键
Producer.java 生产者 发送消息,基于交换机,路由键来发送
QueueConsumer.java 消费者 基于队列来接收消息
MqTest.java MQ测试类
package com.redis.demo.rabbitmq; import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory; import java.io.IOException; public abstract class EndPoint { protected Channel channel;
protected Connection connection;
protected String endPointName; public EndPoint(String endpointName) throws Exception {
this.endPointName = endpointName;
//Create a connection factory
ConnectionFactory factory = new ConnectionFactory(); //hostname of your rabbitmq server
factory.setHost("localhost");
factory.setPort(5672);
factory.setUsername("guest");
factory.setPassword("guest"); //getting a connection
connection = factory.newConnection(); //creating a channel
channel = connection.createChannel(); //declaring a queue for this channel. If queue does not exist,
//it will be created on the server.
//定义队列
channel.queueDeclare(endpointName, false, false, false, null); //exchange
// channel.exchangeDeclare("fanout_test","fanout");
// //rount-key 空, 绑定队列
// channel.queueBind(endpointName,"fanout_test",""); //direct,topic
// channel.exchangeDeclare("direct_test","direct");
// channel.queueBind(endpointName,"direct_test","com.login.message");
//
channel.exchangeDeclare("topic_test","topic");
// channel.queueBind(endpointName,"topic_test","com.order.*");
channel.queueBind(endpointName,"topic_test","com.order.#");
} public void close() throws Exception{
this.channel.close();
this.connection.close();
} }
package com.redis.demo.rabbitmq;
import org.springframework.util.SerializationUtils;
import java.io.Serializable;
public class Producer extends EndPoint{
public Producer(String endpointName) throws Exception {
super(endpointName);
}
/**
* 发送消息
* @param object
* @throws Exception
*/
public void sendMessage(Serializable object) throws Exception {
//发送到队列
// channel.basicPublish("",endPointName, null, SerializationUtils.serialize(object));
//发送rount-key direct
// channel.basicPublish("direct_test","com.login.message",null,SerializationUtils.serialize(object));
//发送,群发各个业务线,匹配规则
channel.basicPublish("topic_test","com.order.create.success",null,SerializationUtils.serialize(object));
// channel.basicPublish("topic_test","com.order.sms",null,SerializationUtils.serialize(object));
// channel.basicPublish("topic_test","com.order.pay.ok",null,SerializationUtils.serialize(object));
}
}
package com.redis.demo.rabbitmq; import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.ShutdownSignalException;
import org.springframework.util.SerializationUtils; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; public class QueueConsumer extends EndPoint implements Runnable, Consumer {
public QueueConsumer(String endpointName) throws Exception {
super(endpointName);
} @Override
public void handleConsumeOk(String consumerTag) {
System.out.println("Consumer "+consumerTag +" registered");
} @Override
public void handleCancelOk(String s) { } @Override
public void handleCancel(String s) throws IOException { } @Override
public void handleShutdownSignal(String s, ShutdownSignalException e) { } @Override
public void handleRecoverOk(String s) {
System.out.println("handleRecoverOk "+s +" registered");
} @Override
public void handleDelivery(String s, Envelope envelope, AMQP.BasicProperties basicProperties, byte[] body) throws IOException {
System.out.println("handleDelivery "+s +" registered, type=" + basicProperties.getType());
Map map = (HashMap) SerializationUtils.deserialize(body);
System.out.println("Message Number "+ map.get("message number") + " received."); } @Override
public void run() {
try {
//start consuming messages. Auto acknowledge messages.
channel.basicConsume(endPointName, true,this);
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.redis.demo.rabbitmq;
import java.util.HashMap;
public class MqTest {
public static void main(String[] args) throws Exception{
//启动线程接收消息
QueueConsumer consumer = new QueueConsumer("queue");
Thread consumerThread = new Thread(consumer);
consumerThread.start();
System.out.println("消费端启动完成===============");
Producer producer = new Producer("queue");
//发送消息
for (int i = 0; i < 10; i++) {
HashMap message = new HashMap();
message.put("message number", i);
//发送map对象
producer.sendMessage(message);
System.out.println("Message Number "+ i +" send success.");
}
}
}
3.pom jar导入
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.14.2</version>
</dependency>
java rabbitmq模拟生产者,消费者demo的更多相关文章
- java多线程模拟生产者消费者问题,公司面试常常问的题。。。
package com.cn.test3; //java多线程模拟生产者消费者问题 //ProducerConsumer是主类,Producer生产者,Consumer消费者,Product产品 // ...
- java简单模拟生产者消费者问题
本文来自:http://www.cnblogs.com/happyPawpaw/archive/2013/01/18/2865957.html 引言 生产者和消费者问题是线程模型中的经典问题:生产者和 ...
- 第23章 java线程通信——生产者/消费者模型案例
第23章 java线程通信--生产者/消费者模型案例 1.案例: package com.rocco; /** * 生产者消费者问题,涉及到几个类 * 第一,这个问题本身就是一个类,即主类 * 第二, ...
- java多线程解决生产者消费者问题
import java.util.ArrayList; import java.util.List; /** * Created by ccc on 16-4-27. */ public class ...
- java+反射+多线程+生产者消费者模式+读取xml(SAX)入数据库mysql-【费元星Q9715234】
java+反射+多线程+生产者消费者模式+读取xml(SAX)入数据库mysql-[费元星Q9715234] 说明如下,不懂的问题直接我[费元星Q9715234] 1.反射的意义在于不将xml tag ...
- Java设计模式之生产者消费者模式
Java设计模式之生产者消费者模式 博客分类: 设计模式 设计模式Java多线程编程thread 转载 对于多线程程序来说,不管任何编程语言,生产者和消费者模型都是最经典的.就像学习每一门编程语言一 ...
- JAVA多线程之生产者 消费者模式 妈妈做面包案例
创建四个类 1.面包类 锅里只可以放10个面包 ---装面包的容器2.厨房 kitchen 生产面包 和消费面包 最多生产100个面包3.生产者4消费者5.测试类 多线程经典案例 import ja ...
- 用Java写一个生产者-消费者队列
生产者消费者的模型作用 通过平衡生产者的生产能力和消费者的消费能力来提升整个系统的运行效率,这是生产者消费者模型最重要的作用. 解耦,这是生产者消费者模型附带的作用,解耦意味着生产者和消费者之间的联系 ...
- Java里的生产者-消费者模型(Producer and Consumer Pattern in Java)
生产者-消费者模型是多线程问题里面的经典问题,也是面试的常见问题.有如下几个常见的实现方法: 1. wait()/notify() 2. lock & condition 3. Blockin ...
- Java并发之:生产者消费者问题
生产者消费者问题是Java并发中的常见问题之一,在实现时,一般可以考虑使用juc包下的BlockingQueue接口,至于具体使用哪个类,则就需要根据具体的使用场景具体分析了.本文主要实现一个生产者消 ...
随机推荐
- 双引擎驱动Quick BI十亿数据0.3秒分析,首屏展示时间缩短30%
简介:在规划中,Quick BI制定了产品竞争力建设的三大方向,包括Quick(快)能力.移动端能力和集成能力.针对其中的产品"报表查看打开慢""报表开发数据同步慢&q ...
- 自己动手写符合自己业务需求的eslint规则
简介:eslint是构建在AST Parser基础上的规则扫描器,缺省情况下使用espree作为AST解析器.rules写好对于AST事件的回调,linter处理源代码之后会根据相应的事件来回调ru ...
- HarmonyOS 实战开发-Worker子线程中解压文件
介绍 本示例介绍在Worker子线程使用@ohos.zlib提供的zlib.decompressfile接口对沙箱目录中的压缩文件进行解压操作,解压成功后将解压路径返回主线程,获取解压文件列表. 效果 ...
- dotnet 6 使用 Obfuscar 进行代码混淆
本文来安利大家 Obfuscar 这个好用的基于 MIT 协议开源的混淆工具.这是一个非常老牌的混淆工具,从 2014 年就对外分发,如今已有累计 495.5K 的 nuget 下载量.而且此工具也在 ...
- k8s证书延长时间(二)
1.查看证书有效时间 # 通过下面可看到ca证书有效期是10年,2022-2032 [root@master ~]# openssl x509 -in /etc/kubernetes/pki/ca.c ...
- LVGL scroll超出父界面不隐藏
问题 超出父界面不隐藏问题,即时使用了lv_obj_set_style_clip_corner()函数,也不起作用,如下图所示: 即使使用lv_obj_set_style_clip_corner(vi ...
- STM32的半主机与MicroLIB机制
一.半主机模式 半主机机制的作用 半主机是作用于ARM目标的一种机制,可以将来自STM32单片机应用程序的输入与输出请求传送至运行仿真器的PC主机上.使用此机制可以启用C库中的函数,如printf() ...
- 2023 Stack Overflow 调研
一.Programming, scripting, and markup languages 二.Databases 三.Web frameworks and technologies 四.Other ...
- Threading Programming Guide:One
苹果支持的产生线程的方式 Operation Object 使用OperationQueue,具体可以参考:Concurrency Programming Guide GCD 使用诸如dispatch ...
- centos 文件系统权限
模板:drwxrwxrwx r表是读 (Read) .w表示写 (Write) .x表示执行 (eXecute) 读.写.运行三项权限可以用数字表示,就是r=4,w=2,x=1, 777就是rwxrw ...