Search in sources :

Example 11 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.

the class AbstractCorrelatingMessageHandlerTests method testReapWithChangeInSameMillisecond.

@Test
public void testReapWithChangeInSameMillisecond() throws Exception {
    MessageGroupProcessor mgp = new DefaultAggregatingMessageGroupProcessor();
    AggregatingMessageHandler handler = new AggregatingMessageHandler(mgp);
    handler.setReleaseStrategy(group -> true);
    QueueChannel outputChannel = new QueueChannel();
    handler.setOutputChannel(outputChannel);
    MessageGroupStore mgs = TestUtils.getPropertyValue(handler, "messageStore", MessageGroupStore.class);
    Method forceComplete = AbstractCorrelatingMessageHandler.class.getDeclaredMethod("forceComplete", MessageGroup.class);
    forceComplete.setAccessible(true);
    GenericMessage<String> secondMessage = new GenericMessage<String>("bar");
    mgs.addMessagesToGroup("foo", new GenericMessage<String>("foo"), secondMessage);
    MessageGroup group = mgs.getMessageGroup("foo");
    // remove a message
    mgs.removeMessagesFromGroup("foo", secondMessage);
    // force lastModified to be the same
    MessageGroup groupNow = mgs.getMessageGroup("foo");
    new DirectFieldAccessor(group).setPropertyValue("lastModified", groupNow.getLastModified());
    forceComplete.invoke(handler, group);
    Message<?> message = outputChannel.receive(0);
    assertNotNull(message);
    Collection<?> payload = (Collection<?>) message.getPayload();
    assertEquals(1, payload.size());
}
Also used : MessageGroupStore(org.springframework.integration.store.MessageGroupStore) QueueChannel(org.springframework.integration.channel.QueueChannel) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) Method(java.lang.reflect.Method) GenericMessage(org.springframework.messaging.support.GenericMessage) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Collection(java.util.Collection) Test(org.junit.Test)

Example 12 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.

the class BarrierMessageHandlerTests method testReplyBeforeRequest.

@Test
public void testReplyBeforeRequest() throws Exception {
    final BarrierMessageHandler handler = new BarrierMessageHandler(10000);
    QueueChannel outputChannel = new QueueChannel();
    handler.setOutputChannel(outputChannel);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    ExecutorService exec = Executors.newSingleThreadExecutor();
    exec.execute(() -> handler.trigger(MessageBuilder.withPayload("bar").setCorrelationId("foo").build()));
    Map<?, ?> suspensions = TestUtils.getPropertyValue(handler, "suspensions", Map.class);
    int n = 0;
    while (n++ < 100 && suspensions.size() == 0) {
        Thread.sleep(100);
    }
    assertTrue("suspension did not appear in time", n < 100);
    handler.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("foo").build());
    Message<?> received = outputChannel.receive(10000);
    assertNotNull(received);
    List<?> result = (ArrayList<?>) received.getPayload();
    assertEquals("foo", result.get(0));
    assertEquals("bar", result.get(1));
    assertEquals(0, suspensions.size());
    exec.shutdownNow();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 13 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.

the class BarrierMessageHandlerTests method testLateReply.

@Test
public void testLateReply() throws Exception {
    final BarrierMessageHandler handler = new BarrierMessageHandler(0);
    QueueChannel outputChannel = new QueueChannel();
    QueueChannel discardChannel = new QueueChannel();
    handler.setOutputChannel(outputChannel);
    handler.setDiscardChannelName("discards");
    handler.setChannelResolver(s -> discardChannel);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    final CountDownLatch latch = new CountDownLatch(1);
    ExecutorService exec = Executors.newSingleThreadExecutor();
    exec.execute(() -> {
        handler.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("foo").build());
        latch.countDown();
    });
    Map<?, ?> suspensions = TestUtils.getPropertyValue(handler, "suspensions", Map.class);
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals("suspension not removed", 0, suspensions.size());
    Log logger = spy(TestUtils.getPropertyValue(handler, "logger", Log.class));
    new DirectFieldAccessor(handler).setPropertyValue("logger", logger);
    final Message<String> triggerMessage = MessageBuilder.withPayload("bar").setCorrelationId("foo").build();
    handler.trigger(triggerMessage);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    verify(logger).error(captor.capture());
    assertThat(captor.getValue(), allOf(containsString("Suspending thread timed out or did not arrive within timeout for:"), containsString("payload=bar")));
    assertEquals(0, suspensions.size());
    Message<?> discard = discardChannel.receive(0);
    assertSame(discard, triggerMessage);
    handler.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("foo").build());
    assertEquals(0, suspensions.size());
    exec.shutdownNow();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) Log(org.apache.commons.logging.Log) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) ExecutorService(java.util.concurrent.ExecutorService) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 14 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.

the class ExpressionEvaluatingCorrelationStrategyTests method testCorrelationStrategyWithAtBeanExpression.

@Test
public void testCorrelationStrategyWithAtBeanExpression() throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("expression-evaluating-correlation-with-bf.xml", this.getClass());
    MessageChannel inputChannel = context.getBean("inputChannel", MessageChannel.class);
    QueueChannel outputChannel = context.getBean("outputChannel", QueueChannel.class);
    Message<?> message = MessageBuilder.withPayload("foo").setSequenceNumber(1).setSequenceSize(1).build();
    inputChannel.send(message);
    Message<?> reply = outputChannel.receive(0);
    assertNotNull(reply);
    context.close();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) QueueChannel(org.springframework.integration.channel.QueueChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Test(org.junit.Test)

Example 15 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.

the class AsyncHandlerTests method testMessagingException.

@Test
public void testMessagingException() {
    QueueChannel errorChannel = new QueueChannel();
    Message<String> message = MessageBuilder.withPayload("foo").setErrorChannel(errorChannel).build();
    this.handler.handleMessage(message);
    assertNull(this.output.receive(0));
    this.whichTest = 2;
    this.latch.countDown();
    Message<?> received = errorChannel.receive(10000);
    assertNotNull(received);
    assertThat(received.getPayload(), instanceOf(MessagingException.class));
    assertSame(message, ((MessagingException) received.getPayload()).getFailedMessage());
    assertNull(this.failedCallbackException);
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

QueueChannel (org.springframework.integration.channel.QueueChannel)709 Test (org.junit.Test)669 GenericMessage (org.springframework.messaging.support.GenericMessage)186 Message (org.springframework.messaging.Message)173 BeanFactory (org.springframework.beans.factory.BeanFactory)162 MessageChannel (org.springframework.messaging.MessageChannel)100 Matchers.containsString (org.hamcrest.Matchers.containsString)66 CountDownLatch (java.util.concurrent.CountDownLatch)59 DirectChannel (org.springframework.integration.channel.DirectChannel)57 ArrayList (java.util.ArrayList)55 PollableChannel (org.springframework.messaging.PollableChannel)55 MessagingException (org.springframework.messaging.MessagingException)53 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)51 AtomicReference (java.util.concurrent.atomic.AtomicReference)47 Socket (java.net.Socket)44 ErrorMessage (org.springframework.messaging.support.ErrorMessage)42 ServerSocket (java.net.ServerSocket)41 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)39 IOException (java.io.IOException)35 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)35