定义 实体类
package cn.fnst.comm.disruptor.event;
/**
* @author fanghc.fnst
* @title: LongEvent
* @projectName DisruptorDemo
* @description: TODO Bean 类
* @date 2019/8/3012:03
*/
public class LongEvent {
private long value;
public long getValue() {
return value;
}
public void set(long value)
{
this.value = value;
}
public void clear() {
System.out.println("clear");
value = 0;
}
}
定义工厂类
package cn.fnst.comm.disruptor.event;
import com.lmax.disruptor.EventFactory;
/**
* @author fanghc.fnst
* @title: LongEventFactory
* @projectName DisruptorDemo
* @description: TODO 工厂类
* @date 2019/8/3012:08
*/
public class LongEventFactory implements EventFactory<LongEvent> {
public LongEvent newInstance()
{
return new LongEvent();
}
}
定义生产者
package cn.fnst.comm.disruptor.event;
import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.RingBuffer;
import java.nio.ByteBuffer;
/**
* @author fanghc.fnst
* @title: LongEventProducer
* @projectName DisruptorDemo
* @description: TODO 生产者
* @date 2019/8/3012:12
*/
public class LongEventProducer {
private final RingBuffer<LongEvent> ringBuffer;
public LongEventProducer(RingBuffer<LongEvent> ringBuffer)
{
this.ringBuffer = ringBuffer;
}
private static final EventTranslatorOneArg<LongEvent, ByteBuffer> TRANSLATOR =
new EventTranslatorOneArg<LongEvent, ByteBuffer>()
{
public void translateTo(LongEvent event, long sequence, ByteBuffer bb)
{
event.set(bb.getLong(0));
}
};
public void onData(ByteBuffer bb)
{
ringBuffer.publishEvent(TRANSLATOR, bb);
}
}
定义消费者
package cn.fnst.comm.disruptor.event;
import com.lmax.disruptor.EventHandler;
/**
* @author fanghc.fnst
* @title: LongEventHandler
* @projectName DisruptorDemo
* @description: TODO handler 处理类 实现 EventHandler 消费者
* @date 2019/8/3012:10
*/
public class LongEventHandler implements EventHandler<LongEvent>
{
public void onEvent(LongEvent event, long sequence, boolean endOfBatch) {
try {
if (event.getValue() > 50){
} else {
Thread.sleep(200);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Event: " + event.getValue());
}
}
定义Main 方法调用
package cn.fnst.comm.disruptor.event;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.util.DaemonThreadFactory;
import java.nio.ByteBuffer;
/**
* @author fanghc.fnst
* @title: LongEventMain
* @projectName DisruptorDemo
* @description: TODO
* @date 2019/8/3012:16
*/
public class LongEventMain {
public static void main(String[] args) throws Exception
{
// The factory for the event
LongEventFactory factory = new LongEventFactory();
// Specify the size of the ring buffer, must be power of 2.
int bufferSize = 1024;
// Construct the Disruptor
Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(factory, bufferSize, DaemonThreadFactory.INSTANCE);
// Connect the handler 增加消费者
disruptor.handleEventsWith(new LongEventHandler());
// .then(new ClearingEventHandler());
// Start the Disruptor, starts all threads running
disruptor.start();
// Get the ring buffer from the Disruptor to be used for publishing.
RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
LongEventProducer producer = new LongEventProducer(ringBuffer);
ByteBuffer bb = ByteBuffer.allocate(8);
for (long l = 0; l< 100; l++) {
System.out.println("================= " + l);
bb.putLong(0, l);
producer.onData(bb);
if (l> 20 && l < 50) {
Thread.sleep(100);
} else if (l>=50){
Thread.sleep(300);
}
}
while (true){
}
}
}
结果显示
================= 0
================= 1
================= 2
================= 3
================= 4
================= 5
================= 6
================= 7
================= 8
================= 9
================= 10
================= 11
================= 12
================= 13
================= 14
================= 15
================= 16
================= 17
================= 18
================= 19
================= 20
================= 21
================= 22
Event: 0
================= 23
================= 24
Event: 1
================= 25
================= 26
Event: 2
================= 27
================= 28
Event: 3
================= 29
================= 30
Event: 4
================= 31
================= 32
Event: 5
================= 33
================= 34
Event: 6
================= 35
================= 36
Event: 7
================= 37
================= 38
Event: 8
================= 39
================= 40
Event: 9
================= 41
================= 42
Event: 10
================= 43
================= 44
Event: 11
================= 45
================= 46
Event: 12
================= 47
================= 48
Event: 13
================= 49
================= 50
Event: 14
Event: 15
================= 51
Event: 16
================= 52
Event: 17
Event: 18
================= 53
Event: 19
================= 54
Event: 20
Event: 21
================= 55
Event: 22
================= 56
Event: 23
Event: 24
================= 57
Event: 25
================= 58
Event: 26
Event: 27
================= 59
Event: 28
================= 60
Event: 29
Event: 30
================= 61
Event: 31
================= 62
Event: 32
Event: 33
================= 63
Event: 34
================= 64
Event: 35
Event: 36
================= 65
Event: 37
================= 66
Event: 38
Event: 39
================= 67
Event: 40
================= 68
Event: 41
Event: 42
================= 69
Event: 43
================= 70
Event: 44
Event: 45
================= 71
Event: 46
================= 72
Event: 47
Event: 48
================= 73
Event: 49
================= 74
Event: 50
Event: 51
Event: 52
Event: 53
Event: 54
Event: 55
Event: 56
Event: 57
Event: 58
Event: 59
Event: 60
Event: 61
Event: 62
Event: 63
Event: 64
Event: 65
Event: 66
Event: 67
Event: 68
Event: 69
Event: 70
Event: 71
Event: 72
Event: 73
Event: 74
================= 75
Event: 75
================= 76
Event: 76
================= 77
Event: 77
================= 78
Event: 78
================= 79
Event: 79
================= 80
Event: 80
================= 81
Event: 81
================= 82
Event: 82
================= 83
Event: 83
================= 84
Event: 84
================= 85
Event: 85
================= 86
Event: 86
================= 87
Event: 87
================= 88
Event: 88
================= 89
Event: 89
================= 90
Event: 90
================= 91
Event: 91
================= 92
Event: 92
================= 93
Event: 93
================= 94
Event: 94
================= 95
Event: 95
================= 96
Event: 96
================= 97
Event: 97
================= 98
Event: 98
================= 99
Event: 99