例子一: 一个生产者多消费者 共同消费产品
package com.h3c.disruptor02;
public class TestEvent {
private String line;
public String getLine() {
return line;
}
public void setLine(String line) {
this.line = line;
}
}
package com.h3c.disruptor02;
import com.lmax.disruptor.EventFactory;
/**
* 事件生产工厂
* @author gaohaicheng
*
*/
public class TestEventFactory implements EventFactory<TestEvent>
{
@Override
public TestEvent newInstance() {
return new TestEvent();
}
}
package com.h3c.disruptor02;
import org.apache.log4j.Logger;
import com.lmax.disruptor.ExceptionHandler;
public class IntEventExceptionHandler implements ExceptionHandler {
private static final Logger logger = Logger.getLogger(IntEventExceptionHandler.class);
public void handleEventException(Throwable ex, long sequence, Object event) {
logger.error("handleEventException", ex);
}
public void handleOnStartException(Throwable ex) {
logger.error("handleOnStartException", ex);
}
public void handleOnShutdownException(Throwable ex) {
logger.error("handleOnShutdownException", ex);
}
}
package com.h3c.disruptor02;
import com.lmax.disruptor.WorkHandler;
public class TestEventHandler implements WorkHandler<TestEvent> {
@Override
public void onEvent(TestEvent event) throws Exception {
System.out.println(Thread.currentThread().getName()+" 处理了一行数据:" + event.getLine());
}
}
package com.h3c.disruptor02;
import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.RingBuffer;
public class TestEventProducer {
private RingBuffer<TestEvent> ringBuffer;
public TestEventProducer (RingBuffer<TestEvent> ringBuffer) {
this.ringBuffer = ringBuffer;
}
/**
* 转换器,向队列里放值的时候调用(队列先设置固定长度的对象,然后通过set方法生产值)
*/
private static EventTranslatorOneArg<TestEvent, String> eventTranslatorOneArg = new EventTranslatorOneArg<TestEvent, String>() {
@Override
public void translateTo(TestEvent event, long sequence, String arg0) {
event.setLine(arg0);
}
};
/**
* 生产者向队列里放入数据
* @param line
*/
public void onData (String line) {
this.ringBuffer.publishEvent(eventTranslatorOneArg, line);
}
/**
* 处理数据
*/
public void handler () {
long begin = System.currentTimeMillis();
for (int i = 0; i < 20; i++) {
System.out.println("生产者生产了一行数据: "+"wozaizhe" + i);
this.onData("wozaizhe" + i);
}
System.out.println("生产 者生产耗时 : "+ (System.currentTimeMillis() - begin));
}
}
package com.h3c.disruptor02;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
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;
public class TestEventMain {
public static final int BUFFER_SIZE = 2;
public static void main (String[] args) {
testDisruptor();
}
public static void testDisruptor () {
ExecutorService executor = Executors.newFixedThreadPool(8);
EventFactory<TestEvent> eventFactory = new TestEventFactory();
//创建disruptor,设置单生产者模式
Disruptor disruptor = new Disruptor(eventFactory, BUFFER_SIZE, executor, ProducerType.SINGLE,
new YieldingWaitStrategy ());
//设置消费者程序
disruptor.handleEventsWithWorkerPool(new TestEventHandler(), new TestEventHandler(),
new TestEventHandler(), new TestEventHandler());
//设置异常处理
disruptor.handleExceptionsWith(new IntEventExceptionHandler());
//启动disruptor并返回RingBuffer
RingBuffer<TestEvent> ringBuffer = disruptor.start();
//创建生产者线程,并生产
TestEventProducer producer = new TestEventProducer(ringBuffer);
producer.handler();
disruptor.shutdown();
executor.shutdown();
}
}
运行结果:
生产者生产了一行数据: wozaizhe0
生产者生产了一行数据: wozaizhe1
生产者生产了一行数据: wozaizhe2
pool-1-thread-4 处理了一行数据:wozaizhe1
pool-1-thread-1 处理了一行数据:wozaizhe0
pool-1-thread-3 处理了一行数据:wozaizhe2
生产者生产了一行数据: wozaizhe3
生产者生产了一行数据: wozaizhe4
生产者生产了一行数据: wozaizhe5
pool-1-thread-4 处理了一行数据:wozaizhe4
pool-1-thread-2 处理了一行数据:wozaizhe3
生产者生产了一行数据: wozaizhe6
生产者生产了一行数据: wozaizhe7
pool-1-thread-1 处理了一行数据:wozaizhe5
pool-1-thread-3 处理了一行数据:wozaizhe6
生产者生产了一行数据: wozaizhe8
生产者生产了一行数据: wozaizhe9
pool-1-thread-4 处理了一行数据:wozaizhe7
pool-1-thread-2 处理了一行数据:wozaizhe8
pool-1-thread-1 处理了一行数据:wozaizhe9
生产者生产了一行数据: wozaizhe10
生产者生产了一行数据: wozaizhe11
生产者生产了一行数据: wozaizhe12
pool-1-thread-3 处理了一行数据:wozaizhe10
pool-1-thread-4 处理了一行数据:wozaizhe11
生产者生产了一行数据: wozaizhe13
生产者生产了一行数据: wozaizhe14
pool-1-thread-2 处理了一行数据:wozaizhe12
pool-1-thread-1 处理了一行数据:wozaizhe13
生产者生产了一行数据: wozaizhe15
生产者生产了一行数据: wozaizhe16
pool-1-thread-4 处理了一行数据:wozaizhe15
pool-1-thread-3 处理了一行数据:wozaizhe14
生产者生产了一行数据: wozaizhe17
生产者生产了一行数据: wozaizhe18
pool-1-thread-2 处理了一行数据:wozaizhe16
pool-1-thread-1 处理了一行数据:wozaizhe17
生产者生产了一行数据: wozaizhe19
pool-1-thread-4 处理了一行数据:wozaizhe18
pool-1-thread-3 处理了一行数据:wozaizhe19
生产 者生产耗时 : 125
例子二:一生产者多消费者分别消费产品