disruptor 单生产者多消费者
demo1 单生产者多消费者创建。
maven 依赖
<!-- https://mvnrepository.com/artifact/com.lmax/disruptor -->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.</version>
</dependency>
1 对象 - Message
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Message2 {
private String id;
private String name;
private double price;
}
2 在主函数中创建 disruptor
Disruptor<Message2> disruptor = new Disruptor<>(
new EventFactory<Message2>() {
@Override
public Message2 newInstance() {
return new Message2();
}
},
<< ,
Executors.defaultThreadFactory(),
ProducerType.SINGLE,
new BusySpinWaitStrategy()
);
3 disruptor 绑定消费者
// disruptor 绑定消费者
disruptor.handleEventsWith(new MessageHandler1()); //创建消费者
@Slf4j
public class MessageHandler1 implements EventHandler<Message2> {
@Override
public void onEvent(Message2 event, long sequence, boolean endOfBatch) throws Exception {
event.setId(UUID.randomUUID().toString());
log.info("【handler1,set id】 id: {}, name: {}, price: {}", event.getId(), event.getName(), event.getPrice());
}
}
4 启动 disruptor
RingBuffer<Message2> ringBuffer = disruptor.start();
5 disruptor 绑定生产者
//绑定生产者
CountDownLatch latch = new CountDownLatch();
ExecutorService es = Executors.newFixedThreadPool();
es.submit(new MessagePublish2(disruptor, latch)); // 生产者类
public class MessagePublish2 implements Runnable {
private Disruptor<Message2> disruptor;
private CountDownLatch latch; public MessagePublish2(Disruptor<Message2> disruptor, CountDownLatch latch) {
this.disruptor = disruptor;
this.latch = latch;
} @Override
public void run() {
for (int i = ; i < ; i++) {
disruptor.publishEvent(new MessageEventTranslator());
}
latch.countDown();
}
}
6 阻塞等待 & 关闭服务
// 阻塞等待
latch.await(); // 关闭服务
es.shutdown();
disruptor.shutdown();
disruptor 单生产者多消费者的更多相关文章
- LMAX Disruptor—多生产者多消费者中,消息复制分发的高性能实现
解决的问题 当我们有多个消息的生产者线程,一个消费者线程时,他们之间如何进行高并发.线程安全的协调? 很简单,用一个队列. 当我们有多个消息的生产者线程,多个消费者线程,并且每一条消息需要被所有的消费 ...
- 使用Disruptor实现生产者和消费者模型
生产者 package cn.lonecloud.procum.disruptor; import cn.lonecloud.procum.Data; import com.lmax.disrupto ...
- disruptor 多生产者多消费者实战 四
一.创建event类 Order public class Order { private String id; private String name; private double price; ...
- Disruptor框架中生产者、消费者的各种复杂依赖场景下的使用总结
版权声明:原创作品,谢绝转载!否则将追究法律责任. Disruptor是一个优秀的并发框架,可以实现单个或多个生产者生产消息,单个或多个消费者消息,且消费者之间可以存在消费消息的依赖关系.网上其他博客 ...
- 从零开始实现lmax-Disruptor队列(一)RingBuffer与单生产者、单消费者工作原理解析
1.lmax-Disruptor队列介绍 disruptor是英国著名的金融交易所lmax旗下技术团队开发的一款java实现的高性能内存队列框架 其发明disruptor的主要目的是为了改进传统的内存 ...
- 【disruptor】2、disruptor中生产者线程与消费者之间的协调
由于ringbuffer是一个环形的队列,那么生产者和消费者在遍历这个队列的时候,如何制衡呢? 1.生产快,消费慢,数据丢失? 生产者速度过快,导致一个对象还没消费完,就循环生产了一个新的对象要加入r ...
- RabbitMQ入门学习系列(二),单生产者消费者
友情提示 我对我的文章负责,发现好多网上的文章 没有实践,都发出来的,让人走很多弯路,如果你在我的文章中遇到无法实现,或者无法走通的问题.可以直接在公众号<爱码农爱生活 >留言.必定会再次 ...
- 线程操作案例--生产者与消费者,Object类对线程的支持
本章目标 1)加深对线程同步的理解 2)了解Object类中对线程的支持方法. 实例 生产者不断生产,消费者不断消费产品. 生产者生产信息后将其放到一个区域中,之后消费者从区域中取出数据. 既然生产的 ...
- PV操作——生产者和消费者
首先,先来看几个概念: 同步:协作的过程,比如,多人开发合作. 相互排斥:争抢资源的过程.比如苦逼的大学选课: 临界区:进程中对临界资源实施操作的那段程序: 临界资源:一次仅仅能一个进程使用的资源,比 ...
随机推荐
- 批处理引擎MapReduce编程模型
批处理引擎MapReduce编程模型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. MapReduce是一个经典的分布式批处理计算引擎,被广泛应用于搜索引擎索引构建,大规模数据处理 ...
- Replica set 的选举策略之一 (转)
首先介绍一下在replica set里分为三种节点类型: 1 primary 负责client的读写. 2 secondary 作为热备节点,应用Primary的oplog读取的操作日志,和pri ...
- bcb中TParamter传NULL值
if (status_Desc.IsEmpty()) Queue_Status->Value = Null(); else Queue_Status->Value = status_Des ...
- Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)
链接: https://codeforces.com/contest/1265/problem/D 题意: An integer sequence is called beautiful if the ...
- CodeForces - 55D - Beautiful numbers(数位DP,离散化)
链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...
- RabbitMQ交换机、RabbitMQ整合springCloud
目标 1.交换机 2.RabbitMQ整合springCloud 交换机 蓝色区域===生产者 红色区域===Server:又称Broker,接受客户端的连接,实现AMQP实体服务 绿色区域===消费 ...
- RMQ--树状数组,ST表,线段树
RMQ Range Minimum/Maximum Query 区间最值问题 树状数组 https://www.cnblogs.com/xenny/p/9739600.html lowbit(x) x ...
- Oracle 日期型 将timestamp类型转换为date类型
Oracle将timestamp类型转换为date类型有三种方法 1.使用to_char先转为字符型,在使用to_date再转为日期型 select to_date(to_char(systimest ...
- 转载:Spark GraphX详解
1.GraphX介绍 1.1 GraphX应用背景 Spark GraphX是一个分布式图处理框架,它是基于Spark平台提供对图计算和图挖掘简洁易用的而丰富的接口,极大的方便了对分布式图处理的需求. ...
- GoCN每日新闻(2019-09-26)
1. go-gin-api 路由中间件:Jaeger 链路追踪(六)https://mp.weixin.qq.com/s/Ea28475_UTNaM9RNfgPqJA2. golang interfa ...