Example usage for org.springframework.integration.channel QueueChannel receive

List of usage examples for org.springframework.integration.channel QueueChannel receive

Introduction

In this page you can find the example usage for org.springframework.integration.channel QueueChannel receive.

Prototype

@Override
@Nullable
public Message<?> receive() 

Source Link

Document

Receive the first available message from this channel.

Usage

From source file:com.apress.prospringintegration.springbatch.integration.IntegrationMain.java

public static void main(String[] args) throws Throwable {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("integration.xml");
    context.start();/* w  w  w  . j av  a 2s  . co  m*/

    MessageChannel launchChannel = context.getBean("launchChannel", MessageChannel.class);
    QueueChannel statusChannel = context.getBean("statusChannel", QueueChannel.class);

    Job job = (Job) context.getBean("importData");

    JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
    jobParametersBuilder.addDate("date", new Date());
    jobParametersBuilder.addString("input.file", "registrations");
    JobParameters jobParameters = jobParametersBuilder.toJobParameters();

    JobLaunchRequest jobLaunchRequest = new JobLaunchRequest(job, jobParameters);
    launchChannel.send(MessageBuilder.withPayload(jobLaunchRequest).build());

    Message<JobExecution> statusMessage = (Message<JobExecution>) statusChannel.receive();
    JobExecution jobExecution = statusMessage.getPayload();

    System.out.println(jobExecution);

    System.out.println("Exit status: " + jobExecution.getExitStatus().getExitCode());
    JobInstance jobInstance = jobExecution.getJobInstance();
    System.out.println("job instance Id: " + jobInstance.getId());
}

From source file:com.apress.prospringintegration.springbatch.partition.IntegrationPartitionMain.java

public static void main(String[] args) throws Throwable {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("message-partition.xml");
    context.start();//  ww  w . j a v  a2  s.  co m

    MessageChannel launchChannel = context.getBean("launchChannel", MessageChannel.class);
    QueueChannel statusChannel = context.getBean("statusChannel", QueueChannel.class);

    Job job = (Job) context.getBean("importData");
    JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
    jobParametersBuilder.addDate("date", new Date());
    jobParametersBuilder.addString("input.file", "registrations");
    JobParameters jobParameters = jobParametersBuilder.toJobParameters();

    JobLaunchRequest jobLaunchRequest = new JobLaunchRequest(job, jobParameters);

    launchChannel.send(MessageBuilder.withPayload(jobLaunchRequest).build());

    Message<JobExecution> statusMessage = (Message<JobExecution>) statusChannel.receive();
    JobExecution jobExecution = statusMessage.getPayload();

    System.out.println(jobExecution);
    System.out.println("Exit status: " + jobExecution.getExitStatus().getExitCode());

    JobInstance jobInstance = jobExecution.getJobInstance();
    System.out.println("job instance Id: " + jobInstance.getId());
}

From source file:org.springframework.integration.channel.QueueChannelTests.java

@Test
public void testSimpleSendAndReceive() throws Exception {
    final AtomicBoolean messageReceived = new AtomicBoolean(false);
    final CountDownLatch latch = new CountDownLatch(1);
    final QueueChannel channel = new QueueChannel();
    Log logger = spy(TestUtils.getPropertyValue(channel, "logger", Log.class));
    when(logger.isDebugEnabled()).thenReturn(true);
    when(logger.isTraceEnabled()).thenReturn(true);
    new DirectFieldAccessor(channel).setPropertyValue("logger", logger);
    new Thread(new Runnable() {
        @Override/*from  w w w .j  a  va2  s.c o m*/
        public void run() {
            Message<?> message = channel.receive();
            if (message != null) {
                messageReceived.set(true);
                latch.countDown();
            }
        }
    }).start();
    assertFalse(messageReceived.get());
    channel.send(new GenericMessage<String>("testing"));
    latch.await(1000, TimeUnit.MILLISECONDS);
    assertTrue(messageReceived.get());
    ArgumentCaptor<String> preCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> postCaptor = ArgumentCaptor.forClass(String.class);
    verify(logger).trace(preCaptor.capture());
    verify(logger, times(3)).debug(postCaptor.capture());
    assertThat(preCaptor.getValue(), startsWith("preReceive"));
    assertThat(postCaptor.getValue(), startsWith("postReceive"));
}

From source file:org.springframework.integration.channel.QueueChannelTests.java

@Test
public void testBlockingReceiveWithNoTimeout() throws Exception {
    final QueueChannel channel = new QueueChannel();
    final AtomicBoolean receiveInterrupted = new AtomicBoolean(false);
    final CountDownLatch latch = new CountDownLatch(1);
    Thread t = new Thread(new Runnable() {
        @Override/*w w  w.  j  av  a2s  .c  o m*/
        public void run() {
            Message<?> message = channel.receive();
            receiveInterrupted.set(true);
            assertTrue(message == null);
            latch.countDown();
        }
    });
    t.start();
    assertFalse(receiveInterrupted.get());
    t.interrupt();
    latch.await();
    assertTrue(receiveInterrupted.get());
}

From source file:org.springframework.integration.channel.QueueChannelTests.java

@Test
public void testReactorPersistentQueue() throws InterruptedException, IOException {
    final AtomicBoolean messageReceived = new AtomicBoolean(false);
    final CountDownLatch latch = new CountDownLatch(1);
    PersistentQueue<Message<?>> queue = new PersistentQueueSpec<Message<?>>()
            .codec(new JavaSerializationCodec<Message<?>>())
            .basePath(this.tempFolder.getRoot().getAbsolutePath()).get();

    final QueueChannel channel = new QueueChannel(queue);
    new Thread(new Runnable() {
        @Override//ww  w  .j  a  v  a2  s. co m
        public void run() {
            Message<?> message = channel.receive();
            if (message != null) {
                messageReceived.set(true);
                latch.countDown();
            }
        }
    }).start();
    assertFalse(messageReceived.get());
    channel.send(new GenericMessage<String>("testing"));
    latch.await(1000, TimeUnit.MILLISECONDS);
    assertTrue(messageReceived.get());

    final CountDownLatch latch1 = new CountDownLatch(2);

    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                Message<?> message = channel.receive(100);
                if (message != null) {
                    latch1.countDown();
                    if (latch1.getCount() == 0) {
                        break;
                    }
                }
            }
        }
    });
    thread.start();

    Thread.sleep(200);
    channel.send(new GenericMessage<String>("testing"));
    channel.send(new GenericMessage<String>("testing"));
    assertTrue(latch1.await(1000, TimeUnit.MILLISECONDS));

    final AtomicBoolean receiveInterrupted = new AtomicBoolean(false);
    final CountDownLatch latch2 = new CountDownLatch(1);
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            Message<?> message = channel.receive(10000);
            receiveInterrupted.set(true);
            assertTrue(message == null);
            latch2.countDown();
        }
    });
    t.start();
    assertFalse(receiveInterrupted.get());
    t.interrupt();
    latch2.await();
    assertTrue(receiveInterrupted.get());

    receiveInterrupted.set(false);
    final CountDownLatch latch3 = new CountDownLatch(1);
    t = new Thread(new Runnable() {
        @Override
        public void run() {
            Message<?> message = channel.receive();
            receiveInterrupted.set(true);
            assertTrue(message == null);
            latch3.countDown();
        }
    });
    t.start();
    assertFalse(receiveInterrupted.get());
    t.interrupt();
    latch3.await();
    assertTrue(receiveInterrupted.get());

    GenericMessage<String> message1 = new GenericMessage<String>("test1");
    GenericMessage<String> message2 = new GenericMessage<String>("test2");
    assertTrue(channel.send(message1));
    assertTrue(channel.send(message2));
    List<Message<?>> clearedMessages = channel.clear();
    assertNotNull(clearedMessages);
    assertEquals(2, clearedMessages.size());

    clearedMessages = channel.clear();
    assertNotNull(clearedMessages);
    assertEquals(0, clearedMessages.size());

    // Test on artificial infinite wait
    // channel.receive();

    // Distributed scenario
    final CountDownLatch latch4 = new CountDownLatch(1);
    new Thread(new Runnable() {
        @Override
        public void run() {
            Message<?> message = channel.receive();
            if (message != null) {
                latch4.countDown();
            }
        }
    }).start();
    queue.add(new GenericMessage<String>("foo"));
    assertTrue(latch4.await(1000, TimeUnit.MILLISECONDS));
}