Disruptor基本使用

5 篇文章 0 订阅

1 Disruptor是一个高性能的异步处理框架,一个轻量级的JMS,和JDK中的BlockingQueue有相似处,但是它的处理速度非常快,获得2011年程序框架创新大奖,号称“一个线程一秒钟可以处理600W个订单”,并且Disruptor不仅仅只有buffer,它提供的功能非常强大。
disruptor开发步骤:

  1. 定义event队列中需要处理的元素

  2. 定义event工厂,用于填充处理队列

  3. 定义消费者
    引入依赖:

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

定义event队列中需要处理的元素

package com.example.disruptor.Event;

public class StringEvent {

    private String stringEvent;


    public void setStringEvent(String stringEvent) {
        this.stringEvent = stringEvent;
    }

    public String getStringEvent() {
        return stringEvent;
    }

}

定义event工厂,用于填充处理队列

package com.cyc.mystudy.disruptor;
 
import com.lmax.disruptor.EventFactory;
 
package com.example.disruptor.Factory;

import com.example.disruptor.Event.StringEvent;
import com.lmax.disruptor.EventFactory;

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

定义消费者

package com.example.disruptor.EventHander;

import com.example.disruptor.Event.StringEvent;
import com.lmax.disruptor.EventHandler;

public class StringEventHander implements EventHandler<StringEvent> {
    @Override
    public void onEvent(StringEvent stringEvent, long l, boolean b) throws Exception {
        System.out.println(Thread.currentThread().getName()+":"+stringEvent.getStringEvent());
    }
}

创建Test类

package com.example.disruptor;

import com.example.disruptor.Event.StringEvent;
import com.example.disruptor.EventHander.StringEventHander;
import com.example.disruptor.Factory.StringEventFactory;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;

import java.util.concurrent.Executors;

public class mian {
    public static void main(String[] args) {
        //生产者
        StringEventFactory factory=new StringEventFactory();
        //大小
        int bufferSize=1024;
        //定义
        Disruptor<StringEvent> disruptor = new Disruptor<>(factory, bufferSize,  Executors.newCachedThreadPool());
        //连接消费端方法
        disruptor.handleEventsWith(new StringEventHander());
        //启动
        disruptor.start();
        //拿到实际存储队列
        RingBuffer<StringEvent> ringBuffer = disruptor.getRingBuffer();

        for (int i=0;i<102400;i++){
            long next = ringBuffer.next();
            StringEvent stringEvent = ringBuffer.get(next);
            stringEvent.setStringEvent(String.valueOf(i));
            //提交发布操作
            ringBuffer.publish(next);
        }

    }
}

disruptor的几种等待策略:
在这里插入图片描述
想要使用等待策略上面创建disruptor可以使用如下方法创建

Disruptor<StringEvent> disruptor =
new Disruptor<StringEvent>(factory, bufferSize, Executors.newCachedThreadPool(), ProducerType.SINGLE, new YieldingWaitStrategy());

如果有多个消费者怎么指定:

StringEventHander stringEventHander = new StringEventHander();
StringEventHander stringEventHander1 = new StringEventHander();
//连接多个消费端方法
disruptor.handleEventsWith(stringEventHander,stringEventHander1);

Disruptor也可以捕捉异常如下:

StringEventHander stringEventHander = new StringEventHander();
//连接多个消费端方法
disruptor.handleEventsWith(stringEventHander);
disruptor.handleExceptionsFor(stringEventHander).with(new ExceptionHandler<StringEvent>() {
    @Override
    public void handleEventException(Throwable throwable, long l, StringEvent stringEvent) {
        System.out.println(throwable.getMessage());
    }
    @Override
    public void handleOnStartException(Throwable throwable) {
    }
    @Override
    public void handleOnShutdownException(Throwable throwable) {
    }
});

这里重写3个方法分别对应

运行时出现异常

启动时异常

关闭时异常的处理方案

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值