Disruptor并发框架:开源的并发框架,核心是一个业务逻辑处理器,能够在一个线程里每秒处理600w订单,能够在无锁的条件下实现网络Queue并发操作,高性能的异步处理并发框架.
package com.neo.study004.radio01;
/**
* @author liyy
* @date 2020/6/6 14:06
* @Fun Eventlei
*/
public class LongEvent {
private long value;
public long getValue() {
return value;
}
public void setValue(long value) {
this.value = value;
}
}
package com.neo.study004.radio01;
import com.lmax.disruptor.EventFactory;
/**
* @author liyy
* @date 2020/6/6 14:07
* @Fun 让Disruptor为我们创建事件,我们同时还声明了一个EventFactory来实例化Event对象
*/
public class LongEventFactory implements EventFactory {
@Override
public Object newInstance() {
return new LongEvent();
}
}
package com.neo.study004.radio01;
import com.lmax.disruptor.EventHandler;
/**
* @author liyy
* @date 2020/6/6 14:21
* @Fun 一个事件处理器。这个事件处理器简单地把事件中存储的数据打印到终端:
*/
public class LongEventHandler implements EventHandler<LongEvent> {
@Override
public void onEvent(LongEvent longEvent, long l, boolean b) throws Exception {
System.out.println(longEvent.getValue());
}
}
package com.neo.study004.radio01;
import com.lmax.disruptor.RingBuffer;
import java.nio.ByteBuffer;
/**
* @author liyy
* @date 2020/6/6 14:23
* @Fun 生成事件的源 事件源会在IO读取到一部分数据的时候触发事件
*/
public class LongEventProducer {
private final RingBuffer<LongEvent> ringBuffer;
public LongEventProducer(RingBuffer<LongEvent> ringBuffer) {
this.ringBuffer = ringBuffer;
}
/**
* onData用来发布事件,每调用一次就发布一次事件事件
* 它的参数会通过事件传递给消费者
*
* @param bb
*/
public void onData(ByteBuffer bb) {
//可以把ringBuffer看做一个事件队列,那么next就是得到下面一个事件槽
long sequence = ringBuffer.next();
try {
//用上面的索引取出一个空的事件用于填充
LongEvent event = ringBuffer.get(sequence);// for the sequence
event.setValue(bb.getLong(0));
} finally {
//发布事件
ringBuffer.publish(sequence);
}
}
}
package com.neo.study004.radio01;
import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.RingBuffer;
import java.nio.ByteBuffer;
/**
* @author liyy
* @date 2020/6/6 14:27
* @Fun 事件初始化器
*/
public class LongEventProducerWithTranslator { //一个translator可以看做一个事件初始化器,publicEvent方法会调用它
//填充Event
private static final EventTranslatorOneArg<LongEvent, ByteBuffer> TRANSLATOR =
new EventTranslatorOneArg<LongEvent, ByteBuffer>() {
public void translateTo(LongEvent event, long sequence, ByteBuffer bb) {
event.setValue(bb.getLong(0));
}
};
private final RingBuffer<LongEvent> ringBuffer;
public LongEventProducerWithTranslator(RingBuffer<LongEvent> ringBuffer) {
this.ringBuffer = ringBuffer;
}
public void onData(ByteBuffer bb) {
ringBuffer.publishEvent(TRANSLATOR, bb);
}
}
package com.neo.study004.radio01;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import java.nio.ByteBuffer;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
/**
* @author liyy
* @date 2020/6/6 14:29
* @Fun 把所有的代码组合起来完成一个完整的事件处理系统
*/
public class LongEventMain {
public static void main(String[] args) throws InterruptedException {
// 创建缓冲池,完成事件内部接收调度
Executor executor = Executors.newCachedThreadPool();
// 实例化事件工厂对象
LongEventFactory factory = new LongEventFactory();
// 设置缓冲池大小
int bufferSize = 1024;
// 创建disruptor
Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(factory, bufferSize, executor);
// 连接消费事件处理器
disruptor.handleEventsWith(new LongEventHandler());
// 启动disruptor
disruptor.start();
// 获取ringBuffer,用来发布事件
RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
LongEventProducer producer = new LongEventProducer(ringBuffer);
ByteBuffer bb = ByteBuffer.allocate(8);
for (long l = 0; l<=200; l++) {
bb.putLong(0, l);
producer.onData(bb);
//Thread.sleep(1000);
}
}
}
Disruptor重要参数说明
public Disruptor(EventFactory<T> eventFactory, int ringBufferSize, Executor executor, ProducerType producerType, WaitStrategy waitStrategy) {
this(RingBuffer.create(producerType, eventFactory, ringBufferSize, waitStrategy), executor);
}
RingBuffer: 最主要的组件,负责存储和更新disruptor中流通的数据.
Sequence: 一个特殊组件处理的序号,每一个消费者都维护一个序号
Sequencer: Disruptor真正的核心。实现了这个接口的两种生产者(单生产者和多生产者)均实现了所有的并发算法,为了在生产者和消费者之间进行准确快速的数据传递。
SequenceBarrier: 由Sequencer生成,并且包含了已经发布的Sequence的引用,这些的Sequence源于Sequencer和一些独立的消费者的Sequence。它包含了决定是否有供消费者来消费的Event的逻辑
WaitStrategy: 决定一个消费者将如何等待生产者将Event置入Disruptor
Event: 从生产者到消费者过程中所处理的数据单元。Disruptor中没有代码表示Event,因为它完全是由用户定义的
EventHandler: 由用户实现并且代表了Disruptor中的一个消费者的接口。
Producer: 由用户实现,它调用RingBuffer来插入事件(Event),在Disruptor中没有相应的实现代码,由用户实现。