/** * The same functionality as {@link RingBuffer#tryNext()}, but allows the caller to attempt * to claim the next n sequences. * * @param n number of slots to claim * @return sequence number of the highest slot claimed * @throws InsufficientCapacityException if the necessary space in the ring buffer is not available */ @Override public long tryNext(int n) throws InsufficientCapacityException { return sequencer.tryNext(n); }
/** * The same functionality as {@link RingBuffer#tryNext()}, but allows the caller to attempt to claim the next n sequences. * * @param n number of slots to claim * @return sequence number of the highest slot claimed * @throws InsufficientCapacityException if the necessary space in the ring buffer is not available */ public long tryNext(int n) throws InsufficientCapacityException { return sequencer.tryNext(n); }
/** * <p>Increment and return the next sequence for the ring buffer. Calls of this * method should ensure that they always publish the sequence afterward. E.g.</p> * <pre> * long sequence = ringBuffer.next(); * try { * Event e = ringBuffer.get(sequence); * // Do some work with the event. * } finally { * ringBuffer.publish(sequence); * } * </pre> * <p>This method will not block if there is not space available in the ring * buffer, instead it will throw an {@link InsufficientCapacityException}.</p> * * @return The next sequence to publish to. * @throws InsufficientCapacityException if the necessary space in the ring buffer is not available * @see RingBuffer#publish(long) * @see RingBuffer#get(long) */ @Override public long tryNext() throws InsufficientCapacityException { return sequencer.tryNext(); }
/** * @see com.lmax.disruptor.EventSink#tryPublishEvent(com.lmax.disruptor.EventTranslatorThreeArg, Object, Object, Object) * com.lmax.disruptor.EventSink#tryPublishEvent(com.lmax.disruptor.EventTranslatorThreeArg, A, B, C) */ @Override public <A, B, C> boolean tryPublishEvent(EventTranslatorThreeArg<E, A, B, C> translator, A arg0, B arg1, C arg2) { try { final long sequence = sequencer.tryNext(); translateAndPublish(translator, sequence, arg0, arg1, arg2); return true; } catch (InsufficientCapacityException e) { return false; } }
/** * @see com.lmax.disruptor.EventSink#tryPublishEvent(com.lmax.disruptor.EventTranslator) */ @Override public boolean tryPublishEvent(EventTranslator<E> translator) { try { final long sequence = sequencer.tryNext(); translateAndPublish(translator, sequence); return true; } catch (InsufficientCapacityException e) { return false; } }
/** * @see com.lmax.disruptor.EventSink#tryPublishEvent(com.lmax.disruptor.EventTranslatorVararg, java.lang.Object...) */ @Override public boolean tryPublishEvent(EventTranslatorVararg<E> translator, Object... args) { try { final long sequence = sequencer.tryNext(); translateAndPublish(translator, sequence, args); return true; } catch (InsufficientCapacityException e) { return false; } }
/** * @see com.lmax.disruptor.EventSink#tryPublishEvent(com.lmax.disruptor.EventTranslatorTwoArg, Object, Object) * com.lmax.disruptor.EventSink#tryPublishEvent(com.lmax.disruptor.EventTranslatorTwoArg, A, B) */ @Override public <A, B> boolean tryPublishEvent(EventTranslatorTwoArg<E, A, B> translator, A arg0, B arg1) { try { final long sequence = sequencer.tryNext(); translateAndPublish(translator, sequence, arg0, arg1); return true; } catch (InsufficientCapacityException e) { return false; } }
/** * @see com.lmax.disruptor.EventSink#tryPublishEvent(com.lmax.disruptor.EventTranslatorOneArg, Object) * com.lmax.disruptor.EventSink#tryPublishEvent(com.lmax.disruptor.EventTranslatorOneArg, A) */ @Override public <A> boolean tryPublishEvent(EventTranslatorOneArg<E, A> translator, A arg0) { try { final long sequence = sequencer.tryNext(); translateAndPublish(translator, sequence, arg0); return true; } catch (InsufficientCapacityException e) { return false; } }
/** * Allows one user supplied argument. * * @see #tryPublishEvent(EventTranslator) * @param translator The user specified translation for the event * @param arg0 A user supplied argument. * @return true if the value was published, false if there was insufficient capacity. */ public <A> boolean tryPublishEvent(EventTranslatorOneArg<E, A> translator, A arg0) { try { final long sequence = sequencer.tryNext(); translateAndPublish(translator, sequence, arg0); return true; } catch (InsufficientCapacityException e) { return false; } }
/** * Allows a variable number of user supplied arguments * * @see #publishEvent(EventTranslator) * @param translator The user specified translation for the event * @param args User supplied arguments. * @return true if the value was published, false if there was insufficient capacity. */ public boolean tryPublishEvent(EventTranslatorVararg<E> translator, Object... args) { try { final long sequence = sequencer.tryNext(); translateAndPublish(translator, sequence, args); return true; } catch (InsufficientCapacityException e) { return false; } }
/** * Attempts to publish an event to the ring buffer. It handles claiming the next sequence, getting the current (uninitialised) event from the ring buffer * and publishing the claimed sequence after translation. Will return false if specified capacity was not available. * * @param translator The user specified translation for the event * @return true if the value was published, false if there was insufficient capacity. */ public boolean tryPublishEvent(EventTranslator<E> translator) { try { final long sequence = sequencer.tryNext(); translateAndPublish(translator, sequence); return true; } catch (InsufficientCapacityException e) { return false; } }
/** * @see com.lmax.disruptor.EventSink#tryPublishEvents(com.lmax.disruptor.EventTranslatorVararg, int, int, java.lang.Object[][]) */ @Override public boolean tryPublishEvents( EventTranslatorVararg<E> translator, int batchStartsAt, int batchSize, Object[]... args) { checkBounds(args, batchStartsAt, batchSize); try { final long finalSequence = sequencer.tryNext(batchSize); translateAndPublishBatch(translator, batchStartsAt, batchSize, finalSequence, args); return true; } catch (InsufficientCapacityException e) { return false; } }
/** * @see com.lmax.disruptor.EventSink#tryPublishEvents(com.lmax.disruptor.EventTranslator[], int, int) */ @Override public boolean tryPublishEvents(EventTranslator<E>[] translators, int batchStartsAt, int batchSize) { checkBounds(translators, batchStartsAt, batchSize); try { final long finalSequence = sequencer.tryNext(batchSize); translateAndPublishBatch(translators, batchStartsAt, batchSize, finalSequence); return true; } catch (InsufficientCapacityException e) { return false; } }
/** * @see com.lmax.disruptor.EventSink#tryPublishEvents(com.lmax.disruptor.EventTranslatorOneArg, int, int, Object[]) * com.lmax.disruptor.EventSink#tryPublishEvents(com.lmax.disruptor.EventTranslatorOneArg, int, int, A[]) */ @Override public <A> boolean tryPublishEvents( EventTranslatorOneArg<E, A> translator, int batchStartsAt, int batchSize, A[] arg0) { checkBounds(arg0, batchStartsAt, batchSize); try { final long finalSequence = sequencer.tryNext(batchSize); translateAndPublishBatch(translator, arg0, batchStartsAt, batchSize, finalSequence); return true; } catch (InsufficientCapacityException e) { return false; } }
/** * @see com.lmax.disruptor.EventSink#tryPublishEvents(com.lmax.disruptor.EventTranslatorTwoArg, int, int, Object[], Object[]) * com.lmax.disruptor.EventSink#tryPublishEvents(com.lmax.disruptor.EventTranslatorTwoArg, int, int, A[], B[]) */ @Override public <A, B> boolean tryPublishEvents( EventTranslatorTwoArg<E, A, B> translator, int batchStartsAt, int batchSize, A[] arg0, B[] arg1) { checkBounds(arg0, arg1, batchStartsAt, batchSize); try { final long finalSequence = sequencer.tryNext(batchSize); translateAndPublishBatch(translator, arg0, arg1, batchStartsAt, batchSize, finalSequence); return true; } catch (InsufficientCapacityException e) { return false; } }
/** * @see com.lmax.disruptor.EventSink#tryPublishEvents(com.lmax.disruptor.EventTranslatorThreeArg, int, int, Object[], Object[], Object[]) * com.lmax.disruptor.EventSink#tryPublishEvents(com.lmax.disruptor.EventTranslatorThreeArg, int, int, A[], B[], C[]) */ @Override public <A, B, C> boolean tryPublishEvents( EventTranslatorThreeArg<E, A, B, C> translator, int batchStartsAt, int batchSize, A[] arg0, B[] arg1, C[] arg2) { checkBounds(arg0, arg1, arg2, batchStartsAt, batchSize); try { final long finalSequence = sequencer.tryNext(batchSize); translateAndPublishBatch(translator, arg0, arg1, arg2, batchStartsAt, batchSize, finalSequence); return true; } catch (InsufficientCapacityException e) { return false; } }
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowBulkTryNextLessThanZero() throws Exception { sequencer.tryNext(-1); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowBulkTryNextOfZero() throws Exception { sequencer.tryNext(0); }
@Test public void shouldTryNext() throws Exception { sequencer.addGatingSequences(gatingSequence); for (int i = 0; i < BUFFER_SIZE; i++) { sequencer.publish(sequencer.tryNext()); } try { sequencer.tryNext(); fail("Should of thrown: " + InsufficientCapacityException.class.getSimpleName()); } catch (InsufficientCapacityException e) { // No-op } }
@Test(expected = InsufficientCapacityException.class) public void shouldThrowInsufficientCapacityExceptionWhenSequencerIsFull() throws Exception { sequencer.addGatingSequences(gatingSequence); for (int i = 0; i < BUFFER_SIZE; i++) { sequencer.next(); } sequencer.tryNext(); }