从功能上来看,Disruptor 是实现了“队列”的功能,并且是一个有界队列。那么它的应用场景天然就是“生产者-消费者”模型的应用场合了。
能够拿 JDK 的 BlockingQueue 作一个简单对比,以便更好地认识 Disruptor 是什么。
咱们知道 BlockingQueue 是一个 FIFO 队列,生产者(Producer)往队列里发布(publish)一项事件(或称之为“消息”也能够)时,消费者(Consumer)能得到通知;若是没有事件时,消费者被堵塞,直到生产者发布了新的事件。
这些都是 Disruptor 能作到的,与之不一样的是,Disruptor 能作更多:app
以上虽然简单地描述了 Disruptor 是什么,但对于它"能作什么",还不是那么明白。简而言之,当你须要在两个独立的处理过程之间交换数据时,就能够使用 Disruptor 。固然使用队列也能够,只不过 Disruptor 的性能更好。dom
本文先不具体去阐述Disruptor的工做具体原理,只是简单地将Spring与其整合。整合过程很简单,具体步骤以下:ide
<dependency> <groupId>com.lmax</groupId> <artifactId>disruptor</artifactId> <version>3.4.2</version> </dependency>
@Data public class NotifyEvent { private String message; }
public class NotifyEventFactory implements EventFactory { @Override public Object newInstance() { return new NotifyEvent(); } }
public class NotifyEventHandler implements EventHandler<NotifyEvent>,WorkHandler<NotifyEvent> { @Override public void onEvent(NotifyEvent notifyEvent, long l, boolean b) throws Exception { System.out.println("接收到消息"); this.onEvent(notifyEvent); } @Override public void onEvent(NotifyEvent notifyEvent) throws Exception { System.out.println(notifyEvent+">>>"+UUID.randomUUID().toString()); } }
@Log4j2 public class NotifyEventHandlerException implements ExceptionHandler { @Override public void handleEventException(Throwable throwable, long sequence, Object event) { throwable.fillInStackTrace(); log.error("process data error sequence ==[{}] event==[{}] ,ex ==[{}]", sequence, event.toString(), throwable.getMessage()); } @Override public void handleOnStartException(Throwable throwable) { log.error("start disruptor error ==[{}]!", throwable.getMessage()); } @Override public void handleOnShutdownException(Throwable throwable) { log.error("shutdown disruptor error ==[{}]!", throwable.getMessage()); } }
@Service public class NotifyServiceImpl implements INotifyService, DisposableBean,InitializingBean { private Disruptor<NotifyEvent> disruptor; private static final int RING_BUFFER_SIZE = 1024 * 1024; @Override public void destroy() throws Exception { disruptor.shutdown(); } @Override public void afterPropertiesSet() throws Exception { disruptor = new Disruptor<NotifyEvent>(new NotifyEventFactory(),RING_BUFFER_SIZE, Executors.defaultThreadFactory(), ProducerType.SINGLE,new BlockingWaitStrategy()); disruptor.setDefaultExceptionHandler(new NotifyEventHandlerException()); disruptor.handleEventsWith(new NotifyEventHandler()); disruptor.start(); } @Override public void sendNotify(String message) { RingBuffer<NotifyEvent> ringBuffer = disruptor.getRingBuffer(); // ringBuffer.publishEvent(new EventTranslatorOneArg<NotifyEvent, String>() { // @Override // public void translateTo(NotifyEvent event, long sequence, String data) { // event.setMessage(data); // } // }, message); ringBuffer.publishEvent((event, sequence, data) -> event.setMessage(data), message); //lambda式写法,若是是用jdk1.8如下版本使用以上注释的一段 } }
@GetMapping("test") @ResponseBody public String testLog() { log.info("============="); notifyService.sendNotify("Hello,World!"); return "hello,world"; }