/** * Add the request to this channel for processing, but wait to return until the request has been processed. * * @param request the request to be submitted; may not be null * @throws InterruptedException if the current thread is interrupted while waiting */ public void addAndAwait( Request request ) throws InterruptedException { // Add the request with a latch, then block until the request has completed ... add(request, new CountDownLatch(1)).await(); }
/** * Asynchronously submit a request to the supplied source. This is typically called when the forked requests are not needed * before continuing. * * @param request the request to be submitted; may not be null * @param sourceName the name of the source; may not be null * @see #submitAndAwait(Request, String) * @see #submit(Request, String, CountDownLatch) */ protected void submit( Request request, String sourceName ) { assert request != null; CompositeRequestChannel channel = channelBySourceName.get(sourceName); if (channel == null) { channel = new CompositeRequestChannel(sourceName); channelBySourceName.put(sourceName, channel); channel.start(executor, getExecutionContext(), connectionFactory); } channel.add(request); }
/** * Submit a request to the supplied source, and have the supplied {@link CountDownLatch latch} be * {@link CountDownLatch#countDown() decremented} when the request has been completed. Note that the same latch can be used * multiple times. * <p> * This method is typically used when a federated request is forked into multiple source-specific requests, but the output of * a source-specific request is required before forking other source-specific requests. This pattern is common in requests * that update one source and any information not stored by that source needs to be stored in another source. * </p> * * @param request the request to be submitted; may not be null * @param sourceName the name of the source; may not be null * @param latch the count-down latch; may not be null * @see #submit(Request, String) * @see #submitAndAwait(Request, String) */ protected void submit( Request request, String sourceName, CountDownLatch latch ) { assert request != null; CompositeRequestChannel channel = channelBySourceName.get(sourceName); if (channel == null) { channel = new CompositeRequestChannel(sourceName); channelBySourceName.put(sourceName, channel); channel.start(executor, getExecutionContext(), connectionFactory); } channel.add(request, latch); }
public void run() { while (requests.hasNext()) { try { Thread.sleep(intervalInMillis); } catch (InterruptedException err) { fail(err.getMessage()); } channel.add(requests.next()); } // Call done ... channel.close(); } }
@Test public void shouldCreateIteratorOverRequestsAddedToChannelAfterObtainingIterator() { Iterator<Request> iter = channel.createIterator(); // Add the requests ... for (Request request : requests) { channel.add(request); } // Call done ... channel.close(); // Start iterating ... for (Request expected : requests) { assertThat(iter.hasNext(), is(true)); assertThat(iter.next(), is(sameInstance(expected))); } assertThat(iter.hasNext(), is(false)); }
@Test public void shouldSubmitBlockedRequestsToConnection() throws Exception { // Start the channel ... channel.start(executor, context, connectionFactory); // Submit the requests to the channel ... List<CountDownLatch> latches = new ArrayList<CountDownLatch>(); for (Request request : requests) { CountDownLatch latch = new CountDownLatch(1); latches.add(latch); channel.add(request, latch); } // Mark the channel as done ... channel.close(); // Wait until the channel has completed ... channel.await(); // Verify that all of the latches were decremented ... for (CountDownLatch latch : latches) { latch.await(); assertThat(latch.getCount(), is(0L)); } // Verify that all the requests to the channel were processed ... Iterator<Request> iter = executedRequests.iterator(); for (Request expected : requests) { assertThat(iter.hasNext(), is(true)); assertThat(iter.next(), is(sameInstance(expected))); } assertThat(iter.hasNext(), is(false)); }
@Test public void shouldSubmitRequestsToConnection() throws Exception { // Start the channel ... channel.start(executor, context, connectionFactory); // Submit the requests to the channel ... for (Request request : requests) { channel.add(request); } // Mark the channel as done ... channel.close(); // Wait until the channel has completed ... channel.await(); // Verify that all the requests to the channel were processed ... Iterator<Request> iter = executedRequests.iterator(); for (Request expected : requests) { assertThat(iter.hasNext(), is(true)); assertThat(iter.next(), is(sameInstance(expected))); } assertThat(iter.hasNext(), is(false)); }
channel.start(executorService, context, connectionFactory); channel.add(request);