博客
关于我
无锁并发框架-Disruptor的使用(二)
阅读量:380 次
发布时间:2019-03-05

本文共 4479 字,大约阅读时间需要 14 分钟。

文章目录

1、生产消费模型的应用

1.1、引入依赖

com.lmax
disruptor
3.2.1
org.projectlombok
lombok
1.18.8
true

1.2、定义Event

package com.zhz.disruptor.event;import lombok.Data;/** * @author :zhz * @date :Created in 2020/12/30 * @version: V1.0 * @slogan: 天下风云出我辈,一入代码岁月催 * @description: 定义事件event 通过Disruptor 进行交换的数据类型。 **/@Datapublic class LongEvent {       private Long value;}

1.3、定义EventFactory

package com.zhz.disruptor.event;import com.lmax.disruptor.EventFactory;/** * @author :zhz * @date :Created in 2020/12/30 * @version: V1.0 * @slogan: 天下风云出我辈,一入代码岁月催 * @description: 定义事件工厂 **/public class LongEventFactory implements EventFactory
{ @Override public LongEvent newInstance() { return new LongEvent(); }}

1.4、定义事件消费者

package com.zhz.disruptor;import com.lmax.disruptor.EventHandler;import com.zhz.disruptor.event.LongEvent;import lombok.extern.slf4j.Slf4j;/** * @author :zhz * @date :Created in 2020/12/30 * @version: V1.0 * @slogan: 天下风云出我辈,一入代码岁月催 * @description: 事件消费者 **/@Slf4jpublic class LongEventHandler implements EventHandler
{ private long serial = 0; public LongEventHandler(long serial){ this.serial = serial; } @Override public void onEvent(LongEvent event, long sequence, boolean endOfBatch) throws Exception { log.info("消费者-{}:{}",this.serial,event.getValue()); }}

1.5、定义生产者

package com.zhz.disruptor;import com.lmax.disruptor.RingBuffer;import com.zhz.disruptor.event.LongEvent;import lombok.extern.slf4j.Slf4j;import java.nio.ByteBuffer;/** * @author :zhz * @date :Created in 2020/12/30 * @version: V1.0 * @slogan: 天下风云出我辈,一入代码岁月催 * @description: 事件生产者 **/@Slf4jpublic class LongEventProducer {       public final RingBuffer
ringBuffer; public LongEventProducer(RingBuffer
ringBuffer) { this.ringBuffer = ringBuffer; } public void onData(ByteBuffer byteBuffer) { // 1.ringBuffer 事件队列 下一个槽 long sequence = ringBuffer.next(); Long data = null; try { //2.取出空的事件队列 LongEvent event = ringBuffer.get(sequence); //3.获取事件队列传递的数据 data = byteBuffer.getLong(0); event.setValue(data); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } finally { log.info("生产者准备发送数据"); //4.发布事件, //注意,最后的 ringBuffer.publish 方法必须包含在 finally 中以确保必须得到调用; // 如果某个请求的 sequence 未被提交,将会堵塞后续的发布操作或者其它的 producer。 ringBuffer.publish(sequence); } }}

1.6、定义Main入口

package com.zhz.disruptor;import com.lmax.disruptor.EventFactory;import com.lmax.disruptor.RingBuffer;import com.lmax.disruptor.YieldingWaitStrategy;import com.lmax.disruptor.dsl.Disruptor;import com.lmax.disruptor.dsl.ProducerType;import com.zhz.disruptor.event.LongEvent;import com.zhz.disruptor.event.LongEventFactory;import java.nio.ByteBuffer;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * @author :zhz * @date :Created in 2020/12/30 * @version: V1.0 * @slogan: 天下风云出我辈,一入代码岁月催 * @description: **/public class DisruptorMain {       public static void main(String[] args) {           // 1.创建一个可缓存的线程 提供线程来出发Consumer 的事件处理        ExecutorService executor = Executors.newCachedThreadPool();        // 2.创建工厂        EventFactory eventFactory = new LongEventFactory();        // 3.创建ringBuffer 大小,ringBufferSize大小一定要是2的N次方        int ringBufferSize = 1024;        // 4.创建Disruptor        Disruptor
disruptor = new Disruptor
(eventFactory, ringBufferSize, executor, ProducerType.SINGLE, new YieldingWaitStrategy()); // 5.连接消费端方法 disruptor.handleEventsWith(new LongEventHandler(1),new LongEventHandler(2)); // 6.启动 disruptor.start(); // 7.创建RingBuffer容器 RingBuffer
ringBuffer = disruptor.getRingBuffer(); // 8.创建生产者 LongEventProducer producer = new LongEventProducer(ringBuffer); // 9.指定缓冲区大小 ByteBuffer byteBuffer = ByteBuffer.allocate(8); for (int i = 1; i <= 10; i++) { byteBuffer.putLong(0,i); producer.onData(byteBuffer); } //10.关闭disruptor和executor disruptor.shutdown(); executor.shutdown(); }}

我是小白弟弟,一个在互联网行业的小白,立志成为一名架构师

转载地址:http://fbbwz.baihongyu.com/

你可能感兴趣的文章
Mysql学习总结(70)——MySQL 优化实施方案
查看>>
Mysql学习总结(71)——MySQL 重复记录查询与删除总结
查看>>
Mysql学习总结(71)——数据库介绍(MySQL安装 体系结构、基本管理)再回顾
查看>>
Mysql学习总结(72)——MySQL 开发者开发,设计规范再总结
查看>>
Mysql学习总结(73)——MySQL 查询A表存在B表不存在的数据SQL总结
查看>>
Mysql学习总结(74)——慢SQL!压垮团队的最后一根稻草!
查看>>
Mysql学习总结(75)——并发量大、数据量大的互联网业务数据库设计军规
查看>>
Mysql学习总结(76)——MySQL执行计划(explain)结果含义总结
查看>>
Mysql学习总结(77)——温故Mysql数据库开发核心原则与规范
查看>>
Mysql学习总结(78)——MySQL各版本差异整理
查看>>
Mysql学习总结(79)——MySQL常用函数总结
查看>>
Mysql学习总结(7)——MySql索引原理与使用大全
查看>>
Mysql学习总结(80)——统计数据库的总记录数和库中各个表的数据量
查看>>
Mysql学习总结(81)——为什么MySQL不推荐使用uuid或者雪花id作为主键?
查看>>
Mysql学习总结(82)——MySQL逻辑删除与数据库唯一性约束如何解决?
查看>>
Mysql学习总结(83)——常用的几种分布式锁:ZK分布式锁、Redis分布式锁、数据库分布式锁、基于JDK的分布式锁方案对比总结
查看>>
Mysql学习总结(84)—— Mysql的主从复制延迟问题总结
查看>>
Mysql学习总结(85)——开发人员最应该明白的数据库设计原则
查看>>
Mysql学习总结(8)——MySql基本查询、连接查询、子查询、正则表达查询讲解
查看>>
Mysql学习总结(9)——MySql视图原理讲解与使用大全
查看>>