Example usage for java.util.concurrent SynchronousQueue poll

List of usage examples for java.util.concurrent SynchronousQueue poll

Introduction

In this page you can find the example usage for java.util.concurrent SynchronousQueue poll.

Prototype

public E poll(long timeout, TimeUnit unit) throws InterruptedException 

Source Link

Document

Retrieves and removes the head of this queue, waiting if necessary up to the specified wait time, for another thread to insert it.

Usage

From source file:org.springframework.xd.shell.command.JobCommandTests.java

@Test
public void testLaunchJobTwiceWhereMakeUniqueIsFalse() throws Exception {
    logger.info("Launch batch job (makeUnique=false) twice");
    String jobName = generateJobName();
    // Batch 3.0 requires at least one parameter to reject duplicate executions of an instance
    String myJobParams = "{\"-param(long)\":\"12345\"}";
    JobParametersHolder.reset();// w ww .jav a2s .c o m
    final JobParametersHolder jobParametersHolder = new JobParametersHolder();

    executeJobCreate(jobName, JOB_WITH_PARAMETERS_DESCRIPTOR + " --makeUnique=false");
    checkForJobInList(jobName, JOB_WITH_PARAMETERS_DESCRIPTOR + " --makeUnique=false", true);
    executeJobLaunch(jobName, myJobParams);
    assertTrue("The countdown latch expired and did not count down.", jobParametersHolder.isDone());

    final SynchronousQueue<Message<?>> rendezvous = new SynchronousQueue<Message<?>>();
    MessageHandler handler = new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            rendezvous.add(message);
        }
    };
    getErrorChannel().subscribe(handler);
    executeCommand("job launch --name " + jobName + " --params " + myJobParams);
    Message<?> error = rendezvous.poll(5, TimeUnit.SECONDS);
    getErrorChannel().unsubscribe(handler);
    assertNotNull("expected an error message", error);
    Object payload = error.getPayload();
    assertTrue("payload should be a MessagingException", payload instanceof MessagingException);
    assertEquals(JobInstanceAlreadyCompleteException.class,
            ((MessagingException) payload).getCause().getClass());
}