private void publishBuffer(List<Object> batch, int batchIndex, long bufferBegin, long bufferEnd) { long at = bufferBegin; while (at <= bufferEnd) { final MutableObject m = _buffer.get(at); m.setObject(batch.get(batchIndex)); at++; batchIndex++; } _buffer.publish(bufferBegin, bufferEnd); }
private synchronized List<Object> getConsumeBatch() throws AlertException, InterruptedException, TimeoutException { long endCursor = getAvailableConsumeCursor(); long currCursor = _consumer.get(); long eventNumber = endCursor - currCursor; List<Object> batch = new ArrayList<>((int) eventNumber); for (long curr = currCursor + 1; curr <= endCursor; curr++) { try { MutableObject mo = _buffer.get(curr); Object o = mo.o; mo.setObject(null); batch.add(o); } catch (Exception e) { LOG.error(e.getMessage(), e); throw new RuntimeException(e); } } _consumer.set(endCursor); return batch; }
public void consumeBatchToCursor(EventHandler<Object> handler) throws AlertException, InterruptedException, TimeoutException { long endCursor = getAvailableConsumeCursor(); long curr = _consumer.get() + 1; for (; curr <= endCursor; curr++) { try { MutableObject mo = _buffer.get(curr); Object o = mo.o; mo.setObject(null); handler.onEvent(o, curr, curr == endCursor); } catch (InterruptedException e) { LOG.error(e.getMessage(), e); return; } catch (Exception e) { LOG.error(e.getMessage(), e); throw new RuntimeException(e); } } // TODO: only set this if the consumer cursor has changed? _consumer.set(endCursor); }
protected void publishDirect(Object obj, boolean block) throws InsufficientCapacityException { final long id; if (block) { id = _buffer.next(); } else { id = _buffer.tryNext(1); } final MutableObject m = _buffer.get(id); m.setObject(obj); _buffer.publish(id); }
_consumer.set(nextSequence); ret = mo.o; mo.setObject(null);
private void publishBuffer(List<Object> batch, int batchIndex, long bufferBegin, long bufferEnd) { long at = bufferBegin; while (at <= bufferEnd) { final MutableObject m = _buffer.get(at); m.setObject(batch.get(batchIndex)); at++; batchIndex++; } _buffer.publish(bufferBegin, bufferEnd); }
public void consumeBatchToCursor(long cursor, EventHandler<Object> handler) { for (long curr = _consumer.get() + 1; curr <= cursor; curr++) { try { MutableObject mo = _buffer.get(curr); Object o = mo.o; mo.setObject(null); handler.onEvent(o, curr, curr == cursor); } catch (InterruptedException e) { LOG.error(e.getMessage(), e); return; } catch (Exception e) { LOG.error(e.getMessage(), e); throw new RuntimeException(e); } } // TODO: only set this if the consumer cursor has changed? _consumer.set(cursor); }
synchronized public List<Object> retreiveAvailableBatch() throws AlertException, InterruptedException, TimeoutException { final long nextSequence = _consumer.get() + 1; final long availableSequence = _barrier.waitFor(nextSequence); final Long availableNum = availableSequence - nextSequence + 1; List<Object> ret = new ArrayList<Object>(availableNum.intValue()); if (availableSequence >= nextSequence) { for (long curr = _consumer.get() + 1; curr <= availableSequence; curr++) { try { MutableObject mo = _buffer.get(curr); Object o = mo.o; mo.setObject(null); ret.add(o); } catch (Exception e) { LOG.error(e.getMessage(), e); throw new RuntimeException(e); } } _consumer.set(availableSequence); } return ret; }
public void publish(Object obj, boolean block) throws InsufficientCapacityException { if(consumerStartedFlag) { final long id; if(block) { id = _buffer.next(); } else { id = _buffer.tryNext(1); } final MutableObject m = _buffer.get(id); m.setObject(obj); _buffer.publish(id); } else { _cache.add(obj); if(consumerStartedFlag) flushCache(); } }
private void consumeBatchToCursor(long cursor, EventHandler<Object> handler) { for(long curr = _consumer.get() + 1; curr <= cursor; curr++) { try { MutableObject mo = _buffer.get(curr); Object o = mo.o; mo.setObject(null); if(o==FLUSH_CACHE) { Object c = null; while(true) { c = _cache.poll(); if(c==null) break; else handler.onEvent(c, curr, true); } } else if(o==INTERRUPT) { throw new InterruptedException("Disruptor processing interrupted"); } else { handler.onEvent(o, curr, curr == cursor); } } catch (Exception e) { throw new RuntimeException(e); } } //TODO: only set this if the consumer cursor has changed? _consumer.set(cursor); }
protected void publishDirect(Object obj, boolean block) throws InsufficientCapacityException { final long id; if (block) { id = _buffer.next(); } else { id = _buffer.tryNext(1); } final MutableObject m = _buffer.get(id); m.setObject(obj); _buffer.publish(id); }
public Object take() { Object ret = null; if (cacheSize() > 0) { synchronized (_cacheLock) { ret = _cache.remove(0); } } else { final long nextSequence = _consumer.get() + 1; // final long availableSequence; try { _barrier.waitFor(nextSequence); } catch (AlertException e) { LOG.error(e.getMessage(), e); throw new RuntimeException(e); } catch (InterruptedException e) { LOG.error("InterruptedException " + e.getCause()); // throw new RuntimeException(e); return null; } catch (TimeoutException e) { // LOG.error(e.getCause(), e); return null; } MutableObject mo = _buffer.get(nextSequence); _consumer.set(nextSequence); ret = mo.o; mo.setObject(null); } handlerCallback(); return ret; }
public Object poll() { Object ret = null; if (cacheSize() > 0) { synchronized (_cacheLock) { ret = _cache.remove(0); } } else { final long nextSequence = _consumer.get() + 1; if (nextSequence <= _barrier.getCursor()) { MutableObject mo = _buffer.get(nextSequence); _consumer.set(nextSequence); ret = mo.o; mo.setObject(null); } } handlerCallback(); return ret; }