我正在玩
Disruptor框架,并且发现我的事件处理程序没有被调用.
这是我的设置代码:
private static final int BUFFER_SIZE = 1024 * 8; private final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor(); private void initializeDisruptor() { if (disruptor != null) return; disruptor = new Disruptor<TwitterStatusReceivedEvent>(TwitterStatusReceivedEvent.EVENT_FACTORY,EXECUTOR,new SingleThreadedClaimStrategy(BUFFER_SIZE),new SleepingWaitStrategy()); disruptor.handleEventsWith(searchTermMatchingHandler) .then(appendStatusHandler,updatePriceHandler).then(persistUpdatesHandler); this.ringBuffer = disruptor.start(); }
在其他地方,我发布了一些事件.我尝试过以下两种方法:
事件发布方法A:
private void handleStatus(final Status status) { long sequence = ringBuffer.next(); TwitterStatusReceivedEvent event = ringBuffer.get(sequence); event.setStatus(status); event.setSearchInstruments(searchInstruments); ringBuffer.publish(sequence); }
在这种情况下,我发现第一个EventHandler被调用,但除此之外从未发生任何事情.
事件发布方法B:
private void handleStatus(final Status status) { disruptor.publishEvent(new EventTranslator<TwitterStatusReceivedEvent>() { @Override public TwitterStatusReceivedEvent translateTo( TwitterStatusReceivedEvent event,long sequence) { event.setStatus(status); event.setSearchInstruments(searchInstruments); return event; } }); }
在这种情况下,我发现根本没有调用任何事件处理程序.
我究竟做错了什么?
更新
这是我的EventHandler.我应该如何表示处理已完成?
public class SearchTermMatchingEventHandler implements EventHandler<TwitterStatusReceivedEvent> { @Override public void onEvent(TwitterStatusReceivedEvent event,long sequence,boolean endOfBatch) throws Exception { String statusText = event.getStatus().getText(); for (Instrument instrument : event.getSearchInstruments()) { if (statusText.contains(instrument.getSearchTerm())) { event.setMatchedInstrument(instrument); break; } } } }