并发框架Disruptor场景应用
- public class MyInParkingDataEvent {
- private String carLicense; // 车牌号
- public String getCarLicense() {
- return carLicense;
- }
- public void setCarLicense(String carLicense) {
- this.carLicense = carLicense;
- }
- }
Handler类:一个负责存储汽车数据,一个负责发送kafka信息到其他系统中,最后一个负责给车主发短信通知
- import com.lmax.disruptor.EventHandler;
- import com.lmax.disruptor.WorkHandler;
- /**
- * Handler 第一个消费者,负责保存进场汽车的信息
- *
- */
- public class MyParkingDataInDbHandler implements EventHandler<MyInParkingDataEvent> , WorkHandler<MyInParkingDataEvent>{
- @Override
- public void onEvent(MyInParkingDataEvent myInParkingDataEvent) throws Exception {
- long threadId = Thread.currentThread().getId(); // 获取当前线程id
- String carLicense = myInParkingDataEvent.getCarLicense(); // 获取车牌号
- System.out.println(String.format("Thread Id %s 保存 %s 到数据库中 ....", threadId, carLicense));
- }
- @Override
- public void onEvent(MyInParkingDataEvent myInParkingDataEvent, long sequence, boolean endOfBatch)
- throws Exception {
- this.onEvent(myInParkingDataEvent);
- }
- }
- import com.lmax.disruptor.EventHandler;
- /**
- * 第二个消费者,负责发送通知告知工作人员(Kafka是一种高吞吐量的分布式发布订阅消息系统)
- */
- public class MyParkingDataToKafkaHandler implements EventHandler<MyInParkingDataEvent>{
- @Override
- public void onEvent(MyInParkingDataEvent myInParkingDataEvent, long sequence, boolean endOfBatch)
- throws Exception {
- long threadId = Thread.currentThread().getId(); // 获取当前线程id
- String carLicense = myInParkingDataEvent.getCarLicense(); // 获取车牌号
- System.out.println(String.format("Thread Id %s 发送 %s 进入停车场信息给 kafka系统...", threadId, carLicense));
- }
- }
- import com.lmax.disruptor.EventHandler;
- /**
- * 第三个消费者,sms短信服务,告知司机你已经进入停车场,计费开始。
- */
- public class MyParkingDataSmsHandler implements EventHandler<MyInParkingDataEvent>{
- @Override
- public void onEvent(MyInParkingDataEvent myInParkingDataEvent, long sequence, boolean endOfBatch)
- throws Exception {
- long threadId = Thread.currentThread().getId(); // 获取当前线程id
- String carLicense = myInParkingDataEvent.getCarLicense(); // 获取车牌号
- System.out.println(String.format("Thread Id %s 给 %s 的车主发送一条短信,并告知他计费开始了 ....", threadId, carLicense));
- }
- }
Producer类:负责上报停车数据
- import java.util.concurrent.CountDownLatch;
- import com.lmax.disruptor.EventTranslator;
- import com.lmax.disruptor.dsl.Disruptor;
- /**
- * 生产者,进入停车场的车辆
- */
- public class MyInParkingDataEventPublisher implements Runnable{
- private CountDownLatch countDownLatch; // 用于监听初始化操作,等初始化执行完毕后,通知主线程继续工作
- private Disruptor<MyInParkingDataEvent> disruptor;
- private static final Integer NUM = 1; // 1,10,100,1000
- public MyInParkingDataEventPublisher(CountDownLatch countDownLatch,
- Disruptor<MyInParkingDataEvent> disruptor) {
- this.countDownLatch = countDownLatch;
- this.disruptor = disruptor;
- }
- @Override
- public void run() {
- MyInParkingDataEventTranslator eventTranslator = new MyInParkingDataEventTranslator();
- try {
- for(int i = 0; i < NUM; i ++) {
- disruptor.publishEvent(eventTranslator);
- Thread.sleep(1000); // 假设一秒钟进一辆车
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- countDownLatch.countDown(); // 执行完毕后通知 await()方法
- System.out.println(NUM + "辆车已经全部进入进入停车场!");
- }
- }
- }
- class MyInParkingDataEventTranslator implements EventTranslator<MyInParkingDataEvent> {
- @Override
- public void translateTo(MyInParkingDataEvent myInParkingDataEvent, long sequence) {
- this.generateData(myInParkingDataEvent);
- }
- private MyInParkingDataEvent generateData(MyInParkingDataEvent myInParkingDataEvent) {
- myInParkingDataEvent.setCarLicense("车牌号: 鄂A-" + (int)(Math.random() * 100000)); // 随机生成一个车牌号
- System.out.println("Thread Id " + Thread.currentThread().getId() + " 写完一个event");
- return myInParkingDataEvent;
- }
- }
执行的Main方法:
- import com.lmax.disruptor.EventFactory;
- import com.lmax.disruptor.YieldingWaitStrategy;
- import com.lmax.disruptor.dsl.Disruptor;
- import com.lmax.disruptor.dsl.EventHandlerGroup;
- import com.lmax.disruptor.dsl.ProducerType;
- /**
- * 执行的Main方法 ,
- * 一个生产者(汽车进入停车场);
- * 三个消费者(一个记录汽车信息,一个发送消息给系统,一个发送消息告知司机)
- * 前两个消费者同步执行,都有结果了再执行第三个消费者
- */
- public class MyInParkingDataEventMain {
- public static void main(String[] args) {
- long beginTime=System.currentTimeMillis();
- int bufferSize = 2048; // 2的N次方
- try {
- // 创建线程池,负责处理Disruptor的四个消费者
- ExecutorService executor = Executors.newFixedThreadPool(4);
- // 初始化一个 Disruptor
- Disruptor<MyInParkingDataEvent> disruptor = new Disruptor<MyInParkingDataEvent>(new EventFactory<MyInParkingDataEvent>() {
- @Override
- public MyInParkingDataEvent newInstance() {
- return new MyInParkingDataEvent(); // Event 初始化工厂
- }
- }, bufferSize, executor, ProducerType.SINGLE, new YieldingWaitStrategy());
- // 使用disruptor创建消费者组 MyParkingDataInDbHandler 和 MyParkingDataToKafkaHandler
- EventHandlerGroup<MyInParkingDataEvent> handlerGroup = disruptor.handleEventsWith(
- new MyParkingDataInDbHandler(), new MyParkingDataToKafkaHandler());
- // 当上面两个消费者处理结束后在消耗 smsHandler
- MyParkingDataSmsHandler myParkingDataSmsHandler = new MyParkingDataSmsHandler();
- handlerGroup.then(myParkingDataSmsHandler);
- // 启动Disruptor
- disruptor.start();
- CountDownLatch countDownLatch = new CountDownLatch(1); // 一个生产者线程准备好了就可以通知主线程继续工作了
- // 生产者生成数据
- executor.submit(new MyInParkingDataEventPublisher(countDownLatch, disruptor));
- countDownLatch.await(); // 等待生产者结束
- disruptor.shutdown();
- executor.shutdown();
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("总耗时:"+(System.currentTimeMillis()-beginTime));
- }
- }
--------------------- 本文来自 ITDragon龙 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/qq_19558705/article/details/77247912?utm_source=copy
并发框架Disruptor场景应用的更多相关文章
- 无锁并发框架Disruptor学习入门
刚刚听说disruptor,大概理一下,只为方便自己理解,文末是一些自己认为比较好的博文,如果有需要的同学可以参考. 本文目标:快速了解Disruptor是什么,主要概念,怎么用 1.Disrupto ...
- 并发框架Disruptor译文
Martin Fowler在自己网站上写了一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金融交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JVM平台上,其核心是一个业务逻辑 ...
- 并发框架Disruptor浅析
1.引言 Disruptor是一个开源的Java框架,它被设计用于在生产者—消费者(producer-consumer problem,简称PCP)问题上获得尽量高的吞吐量(TPS)和尽量低的延迟.D ...
- Java 并发框架Disruptor(七)
Disruptor VS BlockingQueue的压测对比: import java.util.concurrent.ArrayBlockingQueue; public class ArrayB ...
- Disruptor并发框架(一)简介&上手demo
框架简介 Martin Fowler在自己网站上写了一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金融交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JVM平台上,其核心是一 ...
- 并发编程之Disruptor并发框架
一.什么是Disruptor Martin Fowler在自己网站上写了一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金融交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JV ...
- Disruptor 并发框架
什么是Disruptor Martin Fowler在自己网站上写了一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金融交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JVM平 ...
- Disruptor并发框架简介
Martin Fowler在自己网站上写一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金额交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JVM平台上,其核心是一个业务逻辑处 ...
- 来,带你鸟瞰 Java 中4款常用的并发框架!
1. 为什么要写这篇文章 几年前 NoSQL 开始流行的时候,像其他团队一样,我们的团队也热衷于令人兴奋的新东西,并且计划替换一个应用程序的数据库. 但是,当深入实现细节时,我们想起了一位智者曾经说过 ...
随机推荐
- C#版 - Leetcode 13. 罗马数字转整数 - 题解
C#版 - Leetcode 13. 罗马数字转整数 - 题解 Leetcode 13. Roman to Integer 在线提交: https://leetcode.com/problems/ro ...
- 利用好浏览器的空闲时间 --- requestIdleCallback
页面流畅与 FPS 页面是一帧一帧绘制出来的,当每秒绘制的帧数(FPS)达到 60 时,页面是流畅的,小于这个值时,用户会感觉到卡顿. 1s 60帧,所以每一帧分到的时间是 1000/60 ≈ 16 ...
- 自己构建一个Spring自定义标签以及原理讲解
平时不论是在Spring配置文件中引入其他中间件(比如dubbo),还是使用切面时,都会用到自定义标签.那么配置文件中的自定义标签是如何发挥作用的,或者说程序是如何通过你添加的自定义标签实现相应的功能 ...
- 【Java基础】【07面向对象-构造方法&静态static】
07.01_面向对象(构造方法Constructor概述和格式)(掌握) A:构造方法概述和作用 给对象的数据(属性)进行初始化 B:构造方法格式特点 a:方法名与类名相同(大小也要与类名一致) b: ...
- JaveWeb学习之Servlet(二):ServletConfig和ServletContext
原文同步发表至个人博客[夜月归途] 原文链接:http://www.guitu18.com/se/java/2018-07-26/20.html 作者:夜月归途 出处:http://www.guitu ...
- c#调用com组件,程序 发生意外<hr=0x80020009>
引用dll,确认dll没有问题,版本正确,可是一直报发生意外,没有任何其他提示. 解决方案: 看dll引用选项配置 复制到本地:设为true,我的就是false; 嵌入互操作类型:false,如果是t ...
- [Linux] memache打印所有的key
1.在使用memcache的时候 , 经常需要查看下里面存储的值 , 前提是要先知道key是啥,memcache没有redis的keys命令 2.下面两个命令的结合,可以查看到key stats it ...
- TSP(Traveling Salesman Problem)-----浅谈旅行商问题(动态规划,回溯实现)
1.什么是TSP问题 一个售货员必须访问n个城市,这n个城市是一个完全图,售货员需要恰好访问所有城市的一次,并且回到最终的城市. 城市于城市之间有一个旅行费用,售货员希望旅行费用之和最少. 完全图:完 ...
- vue+vuecli+webapck2实现多页面应用
准备工作 在本地用vue-cli新建一个项目,首先安装vue-cil,命令: npm install -g vue-cli 新建一个vue项目,创建一个基于"webpack"的项目 ...
- Dynamics CRM项目实例之六:积分管理,汇总字段,计算字段,快速查看视图
关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复137或者20141228可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me! 博文讲述的主要是如 ...