disruptor 入门 一
一、disruptor基本概念
https://www.cnblogs.com/haiq/p/4112689.html
二、disruptor入门程序
导入disruptor包
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.3.</version>
</dependency>
实现disruptor的四部操作
1.建立一个工厂Event类,用于创建Event类实例对象
public class OrderEvent {
private long value;
public long getValue() {
return value;
}
public void setValue(long value) {
this.value = value;
}
}
public class OrderEventFactory implements EventFactory<OrderEvent> {
@Override
public OrderEvent newInstance() {
return new OrderEvent(); //这个方法就是为了返回空的数据对象Event
}
}
2.创建事件监听类,用于处理数据
import com.lmax.disruptor.EventHandler;
public class OrderEventHandler implements EventHandler<OrderEvent> {
@Override
public void onEvent(OrderEvent orderEvent, long l, boolean b) throws Exception {
System.out.println("消费者:"+orderEvent.getValue());
}
}
3.实例化Disruptor实例,配置一系列的参数,编写Disruptory核心组件
public static void main(String[] args) {
OrderEventFactory orderEventFactory = new OrderEventFactory();
int ringBufferSize = 1024 * 1024;//指定容器的大小
ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); //指定线程,建议使用自定义线程池
/**
* 1.实例化disruptor对象
* 参数一:orderEventFactory 消息(event)工厂对象
* 参数二:ringBufferSize 容器的长度
* 参数三:线程池(建议使用自定义线程池)RejectedExecutionHandler
* 参数四:ProducerType 单生产者还是多生产者
* 参数五:waitStrategy 等待策略
*/
Disruptor<OrderEvent> disruptor = new Disruptor<OrderEvent>(orderEventFactory, ringBufferSize, executorService, ProducerType.SINGLE, new BlockingWaitStrategy());
//2.添加消费者的监听
disruptor.handleEventsWith(new OrderEventHandler());
//3.启动disruptor
disruptor.start();
//4.获取实际存储数据的容器: RingBuffer
RingBuffer<OrderEvent> ringBuffer = disruptor.getRingBuffer();
OrderEventProducer producer = new OrderEventProducer(ringBuffer);
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
for (long i = 0; i < 100; i++) {
byteBuffer.putLong(0, i);
producer.sendData(byteBuffer);
}
disruptor.shutdown();
executorService.shutdown();
}
4.编写生产者组件,向Disruptor容器中去投递数据 (此步骤对应上述main方法中的4)
import com.lmax.disruptor.RingBuffer;
import java.nio.ByteBuffer; public class OrderEventProducer { private RingBuffer<OrderEvent> ringBuffer; public OrderEventProducer(RingBuffer<OrderEvent> ringBuffer) {
this.ringBuffer = ringBuffer;
} public void sendData(ByteBuffer data) { //1 在生产者发送消息的时候,首先需要从 ringBuffer 中获取一个可用的序号
long sequence = ringBuffer.next();
try {
//2 根据这个序号找到具体的 OrderEvent 元素,草地上获取的OrderEvent对象是一个没有被赋值的空对象
OrderEvent orderEvent = ringBuffer.get(sequence); //3 进行时间赋值处理
orderEvent.setValue(data.getLong(0));
} finally {
//4 提交操作
ringBuffer.publish(sequence);
}
}
}
disruptor 入门 一的更多相关文章
- Disruptor入门
一.什么是 Disruptor Disruptor是一个高性能的异步处理框架,或者可以认为是最快的消息框架(轻量的JMS),也可以认为是一个观察者模式实现,或者事件-监听模式的实现,直接称disrup ...
- 无锁并发框架Disruptor学习入门
刚刚听说disruptor,大概理一下,只为方便自己理解,文末是一些自己认为比较好的博文,如果有需要的同学可以参考. 本文目标:快速了解Disruptor是什么,主要概念,怎么用 1.Disrupto ...
- 高性能队列Disruptor系列1--传统队列的不足
在前一篇文章Java中的阻塞队列(BlockingQueue)中介绍了Java中的阻塞队列.从性能上我们能得出一个结论:数组优于链表,CAS优于锁.那么有没有一种队列,通过数组的方式实现,而且采用无锁 ...
- 高性能队列Disruptor系列2--浅析Disruptor
1. Disruptor简单介绍 Disruptor是一个由LMAX开源的Java并发框架.LMAX是一种新型零售金融交易平台,这个系统是建立在 JVM 平台上,核心是一个业务逻辑处理器,它能够在一个 ...
- Disruptor
高性能队列Disruptor系列2--浅析Disruptor 目录 1. Disruptor简单介绍2. 为什么Disruptor如此之快3. Disruptor结构分析 1. Disruptor ...
- Java 并发基础
Java 并发基础 标签 : Java基础 线程简述 线程是进程的执行部分,用来完成一定的任务; 线程拥有自己的堆栈,程序计数器和自己的局部变量,但不拥有系统资源, 他与其他线程共享父进程的共享资源及 ...
- Disruptor_学习_00_资源帖
一.官方 disruptor-github disruptor-api LMAX Disruptor 二.精选资料 Disruptor入门-官方文档翻译-方腾飞 Disruptor官方文档实现 Dis ...
- java架构《并发编程框架篇 __Disruptor》
Disruptor入门 获得Disruptor 可以通过Maven或者下载jar来安装Disruptor.只要把对应的jar放在Java classpath就可以了. 基本的事件生产和消费 我们从 ...
- Disruptor 系列(一)快速入门
Disruptor 系列(一)快速入门 Disruptor:是一个开源的并发框架,能够在 无锁 的情况下实现网络的 Queue 并发操作,所以处理数据的能力比 Java 本身提供的并发类容器要大的多, ...
随机推荐
- 什么是CDN
1.什么是cdn cdn全称是内容分发网络.其目的是让用户能够更快速的得到请求的数据.简单来讲,cdn就是用来加速的,他能让用户就近访问数据,这样就更更快的获取到需要的数据.举个例子,现在服务器 ...
- vue实现动态绑定class--(boolean)绑定class,点击有,再点击取消
<template> <div :class="{'flag':selected}" @click=clickBtn>xxx</div>< ...
- 吴裕雄--天生自然python学习笔记:pandas模块强大的数据处理套件
用 Python 进行数据分析处理,其中最炫酷的就属 Pa ndas 套件了 . 比如,如果我 们通过 Requests 及 Beautifulsoup 来抓取网页中的表格数据 , 需要进行较复 杂的 ...
- django框架进阶-AJAX-长期维护
################## 预备知识---json ####################### 预备知识,json 什么是json?这是一种数据格式,和语言没有关系, 为什么 ...
- in+sb's+基数词的复数形式|UFO|the minutes|
Hawking became world-famous in ________. A. his thirties in the 1970's B. the thirties in his 1970 ...
- 蓝桥杯-PREV31-小朋友排队
解法: 这题有点像冒泡排序,但是做这题并不需要冒泡排序. 假设第i个小朋友比第j个小朋友高,而且i < j 为了把队伍排成从小到大,第i个小朋友一定要去第j个小朋友的右边.又因为只能交换位置相邻 ...
- Java IO: Reader和Writer
作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) Reader 原文链接 Reader是Java IO中所有Reader的基类.Reader与Input ...
- Golang Middleware Part 1 · To Be A Better Man
如何在Golang中实现中间件-Part 1 当使用net/http包实现服务的时候,一般使用的是如下的两中处理方式: http.HandleFunc http.Handle http.HandleF ...
- Markdown快速使用指南
Markdown 是一种轻量级的标记语言,他简洁.高效,目前被越来越多的写作爱好者,撰稿者广泛使用.Markdown的语法十分简单.常用的标记符号也不超过十个,这种相对于更为复杂的 HTML 标记语言 ...
- java 实体类中日期格式转换
@JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss&quo ...