disruptor 链路实战 三
一、创建Event类 Trade
import java.util.concurrent.atomic.AtomicInteger; public class Trade { private String id;
private String name;
private double price;
private AtomicInteger count = new AtomicInteger(0); public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
} public AtomicInteger getCount() {
return count;
} public void setCount(AtomicInteger count) {
this.count = count;
}
}
二、构建 Disruptor
1.创建测试类并构建Disruptor
import com.lmax.disruptor.BusySpinWaitStrategy;
import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class TestMain {
public static void main(String[] args) throws Exception{ //构建一个线程池用于提交任务
ExecutorService es = Executors.newFixedThreadPool(4);
ExecutorService es2 = Executors.newFixedThreadPool(5); //1 构建disruptor
Disruptor<Trade> disruptor = new Disruptor<Trade>(new EventFactory<Trade>() {
@Override
public Trade newInstance() {
return new Trade();
}
},
1024 * 1024,
es2,
ProducerType.SINGLE,
new BusySpinWaitStrategy()); //2 把消费者设置到Disruptor中HandleEventsWtith //2.1 串行操作
// disruptor.handleEventsWith(new Handler1())
// .handleEventsWith(new Handler2())
// .handleEventsWith(new Handler3()); //2.2 并行操作
// disruptor.handleEventsWith(new Handler1());
// disruptor.handleEventsWith(new Handler2());
// disruptor.handleEventsWith(new Handler3());
//或者
// disruptor.handleEventsWith(new Handler1(), new Handler2(), new Handler3()); //2.3 菱形操作 一,即并行加串行执行 此处是 handler1 和 handler2执行完之后再执行handler3
// disruptor.handleEventsWith(new Handler1(), new Handler2())
// .handleEventsWith(new Handler3()); //2.3 菱形操作 二
// EventHandlerGroup<Trade> ehGrop = disruptor.handleEventsWith(new Handler1(), new Handler2());
// ehGrop.then(new Handler3()); //2.4 六边形操作 由于此处使用了5个Handler需要启动5个线程,此时需要修改disruptor的线程池es2的大小为5
Handler1 h1 = new Handler1();
Handler2 h2 = new Handler2();
Handler3 h3 = new Handler3();
Handler4 h4 = new Handler4();
Handler5 h5 = new Handler5();
disruptor.handleEventsWith(h1, h4);
disruptor.after(h1).handleEventsWith(h2);
disruptor.after(h4).handleEventsWith(h5);
disruptor.after(h2, h5).handleEventsWith(h3); //3 启动disruptor
RingBuffer<Trade> ringBuffer = disruptor.start(); long begin = System.currentTimeMillis();
//异步提交
CountDownLatch latch = new CountDownLatch(1);
es.submit(new TradePushlisher(disruptor, latch)); // 异步代码执行完毕 向下进行
latch.await(); disruptor.shutdown();
es.shutdown();
es2.shutdown(); System.out.println("总耗时:"+ (System.currentTimeMillis() - begin));
}
}
2.创建异步提交类
import com.lmax.disruptor.EventTranslator;
import com.lmax.disruptor.dsl.Disruptor; import java.util.Random;
import java.util.concurrent.CountDownLatch; public class TradePushlisher implements Runnable{ private Disruptor<Trade> disruptor;
private CountDownLatch latch;
private static int PUBLISH_COUNT = 10; public TradePushlisher(Disruptor<Trade> disruptor, CountDownLatch latch) {
this.disruptor = disruptor;
this.latch = latch;
} @Override
public void run() {
TradeEventTranslator tradeEventTranslator = new TradeEventTranslator();
// 使用disruptor提交任务
disruptor.publishEvent(tradeEventTranslator);
latch.countDown();
}
} class TradeEventTranslator implements EventTranslator<Trade> { private Random random = new Random(); @Override
public void translateTo(Trade event, long sequence) {
generateTrade(event);
} private void generateTrade(Trade trade) {
trade.setPrice(random.nextDouble() * 9999);
}
}
三、链路讲解
1.创建5个Handler
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.WorkHandler; public class Handler1 implements EventHandler<Trade>, WorkHandler<Trade> { //EventHandler
@Override
public void onEvent(Trade event, long sequence, boolean endOfBatch) throws Exception {
this.onEvent(event);
} //WorkHandler
@Override
public void onEvent(Trade event) throws Exception {
System.out.println("handler 1 : SET NAME");
event.setName("H1");
Thread.sleep(1000);
}
}
import com.lmax.disruptor.EventHandler; import java.util.UUID; public class Handler2 implements EventHandler<Trade> { @Override
public void onEvent(Trade event, long sequence, boolean endOfBatch) throws Exception {
System.out.println("handler 2 : SET ID");
Thread.sleep(1000);
event.setId(UUID.randomUUID().toString());
}
}
import com.lmax.disruptor.EventHandler; public class Handler3 implements EventHandler<Trade> {
@Override
public void onEvent(Trade event, long sequence, boolean endOfBatch) throws Exception {
System.out.println("handler 3 : Name:"+ event.getName() + ", ID:" + event.getId() + ",price:" + event.getPrice());
}
}
import com.lmax.disruptor.EventHandler; public class Handler4 implements EventHandler<Trade> {
@Override
public void onEvent(Trade event, long sequence, boolean endOfBatch) throws Exception {
System.out.println("handler 4 : SET PRICE");
event.setPrice(17.0);
}
}
import com.lmax.disruptor.EventHandler; public class Handler5 implements EventHandler<Trade> {
@Override
public void onEvent(Trade event, long sequence, boolean endOfBatch) throws Exception {
System.out.println("handler 5 : GET PRICE: " + event.getPrice());
event.setPrice(event.getPrice() + 3.0);
}
}
2.消费者链路说明 上面测试类中已经包含有代码了,此处画个图做个说明
① 串行操作
disruptor.handleEventsWith(new Handler1())
.handleEventsWith(new Handler2())
.handleEventsWith(new Handler3());
② 并行操作
disruptor.handleEventsWith(new Handler1());
disruptor.handleEventsWith(new Handler2());
disruptor.handleEventsWith(new Handler3());
// 或者
disruptor.handleEventsWith(new Handler1(), new Handler2(), new Handler3());
③ 菱形操作
disruptor.handleEventsWith(new Handler1(), new Handler2())
.handleEventsWith(new Handler3());
//或者
EventHandlerGroup<Trade> ehGrop = disruptor.handleEventsWith(new Handler1(), new Handler2());
ehGrop.then(new Handler3());
④ 六边形操作
Handler1 h1 = new Handler1();
Handler2 h2 = new Handler2();
Handler3 h3 = new Handler3();
Handler4 h4 = new Handler4();
Handler5 h5 = new Handler5();
disruptor.handleEventsWith(h1, h4);
disruptor.after(h1).handleEventsWith(h2);
disruptor.after(h4).handleEventsWith(h5);
disruptor.after(h2, h5).handleEventsWith(h3);
disruptor 链路实战 三的更多相关文章
- coreseek实战(三):全文搜索在php中应用(使用api接口)
coreseek实战(三):全文搜索在php中应用(使用api接口) 这一篇文章开始学习在php页面中通过api接口,使用coreseek全文搜索. 第一步:综合一下前两篇文章,coreseek实战( ...
- Python爬虫实战三之实现山东大学无线网络掉线自动重连
综述 最近山大软件园校区QLSC_STU无线网掉线掉的厉害,连上之后平均十分钟左右掉线一次,很是让人心烦,还能不能愉快地上自习了?能忍吗?反正我是不能忍了,嗯,自己动手,丰衣足食!写个程序解决掉它! ...
- Thrift RPC实战(三) thrift序列化揭秘
本文主要讲解Thrift的序列化机制, 看看thrift作为数据交换格式是如何工作的? 1.构造应用场景: 1). 首先我们先来定义下thrift的简单结构. 1 2 3 4 5 namespace ...
- miniFTP项目实战三
项目简介: 在Linux环境下用C语言开发的Vsftpd的简化版本,拥有部分Vsftpd功能和相同的FTP协议,系统的主要架构采用多进程模型,每当有一个新的客户连接到达,主进程就会派生出一个ftp服务 ...
- 应用程序框架实战三十八:项目示例VS解决方案的创建(一)
进行项目开发的第一步,是创建出适合自己团队习惯的VS解决方案,虽然我已经提供了项目示例,但毕竟是我创建的,你直接使用可能并不合适,另外你如果尝试模仿重新创建该示例,中间可能碰到各种障碍,特别是项目间的 ...
- 应用程序框架实战三十六:CRUD实战演练介绍
从本篇开始,本系列将进入实战演练阶段. 前面主要介绍了一些应用程序框架的概念和基类,本来想把所有概念介绍完,再把框架内部实现都讲完了,再进入实战,这样可以让初学者基础牢靠.不过我的精力很有限,文章进度 ...
- 链路层三种类型的MAC地址
若需要转载,请注明出处. 我们知道,链路层都是以MAC地址来进行通信双方的地址标识的,如下图:在应用中根据接收方的多寡来进行划分,可分为以下三种: 单播(Unicast) 多播(Multicast) ...
- 【SSH项目实战三】脚本密钥的批量分发与执行
[SSH项目实战]脚本密钥的批量分发与执行 标签(空格分隔): Linux服务搭建-陈思齐 ---本教学笔记是本人学习和工作生涯中的摘记整理而成,此为初稿(尚有诸多不完善之处),为原创作品,允许转载, ...
- C# Redis实战(三)
三.程序配置 在C# Redis实战(二)中我们安装好了Redis的系统服务,此时Redis服务已经运行. 现在我们需要让我们的程序能正确读取到Redis服务地址等一系列的配置信息,首先,需要在Web ...
随机推荐
- The General Addition Rule|complementation rule|special addition rule|
5.3 Some Rules of Probability 如图所示,AorB是所有蓝色区域,所以P(AorB)=PA+PB,但是若非互斥事件,则不能直接相加: If you think of the ...
- Mock测试,何去何从
2016-10-24 出处:Qtest之道 作/译者:闫耀珍 上面的情景是不是似曾相识呢?现今的业务系统已经很少是孤立存在的了,尤其对于一个大公司而言,各个部门之间的配合非常密切,我们或多或 ...
- [LC] 13. Roman to Integer
Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value I 1 V ...
- LabVIEW的优点
知道Labview的英文全称是什么吗?Labview的创始公司的名字是什么吗?哈哈,其实这就是NI(National Instruments)美国国家仪器公司创造Labview的初衷:代替传统测量仪器 ...
- 错误修改.bashrc文件导致所有命令无法使用解决方法
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
- svn使用2(转)
首先打开VisualSVN Server Manager,如图: 可以在窗口的右边看到版本库的一些信息,比如状态,日志,用户认证,版本库等.要建立版本库,需要右键单击左边窗口的Repositores, ...
- C# 内置的类型转换方法
C# 提供了下列内置的类型转换方法: 序号 方法 & 描述 1 ToBoolean把类型转换为布尔型. 2 ToByte把类型转换为字节类型. 3 ToChar如果可能的话,把类型转换为单个 ...
- <JZOJ5943>树
一开始t了五个点我就一脸懵逼 然后 发现高级操作... 就是那个tor的数组2333 可以让一些不需要改的不再去改啦 位运算果然是神奇的东西XD 魔性哈哈哈 #include<cstdio> ...
- mongoDB连接信息及生成对应的collection生成代码
.net,个人封装MONGODDB的操作. using System; using System.Collections.Generic; using System.Linq; using Syste ...
- 修改xampp中phpmyadmin用户管理
用相关软件打开位于D:/xampp/phpMyAdmin文件夹中的config.inc.php文件. 搜索$cfg['Servers'][$i]['auth_type'] = 'config'; 将其 ...