首页 > 其他分享 >关于Disruptor的实战应用

关于Disruptor的实战应用

时间:2023-02-14 20:47:02浏览次数:36  
标签:实战 Disruptor 20 sequence MyEventHandler 应用 test endOfBatch com

首先,需要引入 Disruptor 的依赖库,可以在 Maven 中添加如下依赖:

<dependency>
    <groupId>com.lmax</groupId>
    <artifactId>disruptor</artifactId>
    <version>3.4.2</version>
</dependency>

接下来,定义一个 Event 类,表示要处理的任务:

@Data
public class MyEvent { private String message; }

  

然后,定义一个 EventFactory 类,用于创建 Event 对象:

public class MyEventFactory implements EventFactory<MyEvent> {
    @Override
    public MyEvent newInstance() {
        return new MyEvent();
    }
}

 

接着,定义一个 EventHandler 类,用于处理 Event 对象:

@Slf4j
public class MyEventHandler implements EventHandler<MyEvent> { @Override public void onEvent(MyEvent event, long sequence, boolean endOfBatch) { // 处理 event 对象的逻辑,比如打印消息
     TimeUnit.SECONDS.sleep(3);
     log.info("received message:{}, sequence:{}, endOfBatch:{}", event.getMessage(), sequence,
endOfBatch);
 } }

 

最后,在主程序中创建 Disruptor 对象,并提交任务:

public class DisruptorMainTest {

    public static void main(String[] args) {
        Disruptor<MyEvent> disruptor = new Disruptor<>(new MyFactory(), 1024,
            Executors.defaultThreadFactory());
        disruptor.handleEventsWith(new MyEventHandler());
        disruptor.start();

        RingBuffer<MyEvent> ringBuffer = disruptor.getRingBuffer();
        for (int i = 0; i < 10; i++) {
            long sequence = ringBuffer.next();
            MyEvent event = ringBuffer.get(sequence);
            event.setMessage("Hello world " + i);
            ringBuffer.publish(sequence);
        }
        log.info("task publish finished");
        disruptor.shutdown();
        log.info("shut down");
    }
}

 

以上代码创建了一个 Disruptor 对象,并定义了一个 MyEventHandler 类来处理 Event 对象。在主程序中,首先通过调用 disruptor.start() 方法启动 Disruptor,然后通过 ringBuffer.next() 方法获取一个 Event 对象,并将要处理的数据存入该对象中。最后通过 ringBuffer.publish() 方法将 Event 对象发布到 Disruptor 中,等待处理。

注意,在程序结束时应该调用 disruptor.shutdown() 方法来关闭 Disruptor,释放资源。

Disruptor演示完毕... ...

运行结果:

20:20:26.096 [main] INFO com.hejinggang.test.first_test.service.DisruptorMainTest - task publish finished
20:20:29.099 [pool-1-thread-1] INFO com.hejinggang.test.first_test.service.handler.MyEventHandler - received message:Hello world 0, sequence:0, endOfBatch:false
20:20:32.104 [pool-1-thread-1] INFO com.hejinggang.test.first_test.service.handler.MyEventHandler - received message:Hello world 1, sequence:1, endOfBatch:false
20:20:35.105 [pool-1-thread-1] INFO com.hejinggang.test.first_test.service.handler.MyEventHandler - received message:Hello world 2, sequence:2, endOfBatch:false
20:20:38.110 [pool-1-thread-1] INFO com.hejinggang.test.first_test.service.handler.MyEventHandler - received message:Hello world 3, sequence:3, endOfBatch:false
20:20:41.114 [pool-1-thread-1] INFO com.hejinggang.test.first_test.service.handler.MyEventHandler - received message:Hello world 4, sequence:4, endOfBatch:false
20:20:44.119 [pool-1-thread-1] INFO com.hejinggang.test.first_test.service.handler.MyEventHandler - received message:Hello world 5, sequence:5, endOfBatch:false
20:20:47.120 [pool-1-thread-1] INFO com.hejinggang.test.first_test.service.handler.MyEventHandler - received message:Hello world 6, sequence:6, endOfBatch:false
20:20:50.122 [pool-1-thread-1] INFO com.hejinggang.test.first_test.service.handler.MyEventHandler - received message:Hello world 7, sequence:7, endOfBatch:false
20:20:53.126 [pool-1-thread-1] INFO com.hejinggang.test.first_test.service.handler.MyEventHandler - received message:Hello world 8, sequence:8, endOfBatch:false
20:20:56.132 [pool-1-thread-1] INFO com.hejinggang.test.first_test.service.handler.MyEventHandler - received message:Hello world 9, sequence:9, endOfBatch:true
20:20:56.132 [main] INFO com.hejinggang.test.first_test.service.DisruptorMainTest - shut down

使用到的技术:

Exectors

Disruptor

TimeUnit

标签:实战,Disruptor,20,sequence,MyEventHandler,应用,test,endOfBatch,com
From: https://www.cnblogs.com/kongqueting/p/17120817.html

相关文章

  • 【Azure 应用服务】在App Service for Windows中实现反向代理
    问题描述如何在AppServiceforWindows(.NETStack)中,如何实现反向代理呢?正向代理:客户端想要访问一个服务器,但是它可能无法直接访问这台服务器,这时候这可找一台可以访......
  • 关于使用 交叉编译器编译 linux 应用程序
    1之前犯过的错误:我使用的是6818核心板,系统是android5.1。我觉得既然这个SDK开发包能够编译出可以烧写的镜像那么我就直接用这个交叉编译器编写在我编译的linux......
  • AWS Glue 在数据湖仓中的应用
    在AWS环境,一般会使用S3作为数据湖,在S3上存储组织中的结构化,半结构化及非结构化的数据,这里使用了一个网上比较典型的AWS数据湖仓的架构   GlueCatalog数据目录的......
  • 智慧医院系统开发应用有哪些优势?
     “智慧医院”是一个信息化的概念,不是技术堆积,也不是功能的代名词。智慧医院是具备信息化、互联网化、智能化特征的医院。是运用互联网,物联网、大数据等信息化的技术,让电......
  • 浅析云边端协同架构的应用意义与EasyCVR视频融合能力升级
    随着5G时代的到来,万物互联产生了海量数据,据IDC预测,到2025年全球设备连接总数将达到1000亿,集中式处理模型下核心网络无法承载如此大的数据量传输,数据也无法在云中心存储计算,......
  • Linux系统安全及应用
    1.账号安全基本措施1.1系统账号清理(1)将非登录用户的shell设为/sbin/nologin:命令行格式:usermod-s/sbin/nologin用户名   (2)锁定长期不使用......
  • 浅析EasyCVR安防视频能力在智慧小区建设场景中的应用及意
    一、行业需求城市的发展创造了大量工作机会,人口的聚集也推动了居民住宅建设率的增长。人民生活旨在安居乐业,能否住得“踏实”是很多劳动工作者最关心的问题。但目前随着住宅......
  • 多功能智慧杆如何实现智能红绿灯应用?
    多功能智慧杆的优势之一在于模块化的功能组合,能够根据场景需求自由搭载硬件外设,开发专项功能。比如在十字路口的多功能智慧杆,可以选择搭载AI摄像头、智能红绿灯、显示屏、......
  • 个性化推荐系统在互联网中的应用
    郑重声明:本文纯属Fans同学的个人见解,仅供参考,欢迎拍砖。软林至尊,Fans同盟。号令天下,莫敢不从。@Fans.Lei1.个性化推荐系统定义:个性化推荐是根据用户的兴趣特点和购......
  • 报表开发工具FastReport .Net 如何在移动端生成Web报表应用?
    FastReport.Net是一款全功能的WindowsForms、ASP.NET和MVC报表分析解决方案,使用FastReport.NET可以创建独立于应用程序的.NET报表,同时FastReport.Net支持中文、英语等14......