Search in sources :

Example 86 with Message

use of org.springframework.messaging.Message in project spring-integration by spring-projects.

the class ApplicationContextMessageBusTests method consumerSubscribedToErrorChannel.

@Test
public void consumerSubscribedToErrorChannel() throws InterruptedException {
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    QueueChannel errorChannel = new QueueChannel();
    context.registerChannel(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, errorChannel);
    final CountDownLatch latch = new CountDownLatch(1);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        public Object handleRequestMessage(Message<?> message) {
            latch.countDown();
            return null;
        }
    };
    PollingConsumer endpoint = new PollingConsumer(errorChannel, handler);
    endpoint.setBeanFactory(mock(BeanFactory.class));
    context.registerEndpoint("testEndpoint", endpoint);
    context.refresh();
    errorChannel.send(new ErrorMessage(new RuntimeException("test-exception")));
    latch.await(1000, TimeUnit.MILLISECONDS);
    assertEquals("handler should have received error message", 0, latch.getCount());
    context.stop();
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) QueueChannel(org.springframework.integration.channel.QueueChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) CountDownLatch(java.util.concurrent.CountDownLatch) ErrorMessage(org.springframework.messaging.support.ErrorMessage) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) Test(org.junit.Test)

Example 87 with Message

use of org.springframework.messaging.Message in project spring-integration by spring-projects.

the class CGLibProxyChannelTests method testProxyExecutor.

@Test
public void testProxyExecutor() throws Exception {
    assertTrue(AopUtils.isCglibProxy(this.executorChannel));
    final AtomicReference<Message<?>> message = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    this.executorChannel.subscribe(m -> {
        message.set(m);
        latch.countDown();
    });
    this.executorChannel.send(new GenericMessage<>("foo"));
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertNotNull(message.get());
}
Also used : Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 88 with Message

use of org.springframework.messaging.Message in project spring-integration by spring-projects.

the class CGLibProxyChannelTests method testProxyPubSubWithExec.

@Test
public void testProxyPubSubWithExec() throws Exception {
    assertTrue(AopUtils.isCglibProxy(this.publishSubscribeChannel));
    final AtomicReference<Message<?>> message = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    this.publishSubscribeChannel.subscribe(m -> {
        message.set(m);
        latch.countDown();
    });
    this.publishSubscribeChannel.send(new GenericMessage<>("foo"));
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertNotNull(message.get());
}
Also used : Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 89 with Message

use of org.springframework.messaging.Message in project spring-integration by spring-projects.

the class ReactiveStreamsConsumerTests method testReactiveStreamsConsumerDirectChannel.

@Test
@SuppressWarnings("unchecked")
public void testReactiveStreamsConsumerDirectChannel() throws InterruptedException {
    DirectChannel testChannel = new DirectChannel();
    Subscriber<Message<?>> testSubscriber = (Subscriber<Message<?>>) Mockito.mock(Subscriber.class);
    BlockingQueue<Message<?>> messages = new LinkedBlockingQueue<>();
    willAnswer(i -> {
        messages.put(i.getArgument(0));
        return null;
    }).given(testSubscriber).onNext(any(Message.class));
    ReactiveStreamsConsumer reactiveConsumer = new ReactiveStreamsConsumer(testChannel, testSubscriber);
    reactiveConsumer.setBeanFactory(mock(BeanFactory.class));
    reactiveConsumer.afterPropertiesSet();
    reactiveConsumer.start();
    Message<?> testMessage = new GenericMessage<>("test");
    testChannel.send(testMessage);
    ArgumentCaptor<Subscription> subscriptionArgumentCaptor = ArgumentCaptor.forClass(Subscription.class);
    verify(testSubscriber).onSubscribe(subscriptionArgumentCaptor.capture());
    Subscription subscription = subscriptionArgumentCaptor.getValue();
    subscription.request(1);
    Message<?> message = messages.poll(10, TimeUnit.SECONDS);
    assertSame(testMessage, message);
    reactiveConsumer.stop();
    try {
        testChannel.send(testMessage);
        fail("MessageDeliveryException");
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
    }
    reactiveConsumer.start();
    subscription.request(1);
    testMessage = new GenericMessage<>("test2");
    testChannel.send(testMessage);
    message = messages.poll(10, TimeUnit.SECONDS);
    assertSame(testMessage, message);
    verify(testSubscriber, never()).onError(any(Throwable.class));
    verify(testSubscriber, never()).onComplete();
    assertTrue(messages.isEmpty());
}
Also used : ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) DirectChannel(org.springframework.integration.channel.DirectChannel) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) GenericMessage(org.springframework.messaging.support.GenericMessage) Subscriber(org.reactivestreams.Subscriber) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 90 with Message

use of org.springframework.messaging.Message in project spring-integration by spring-projects.

the class ReactiveStreamsConsumerTests method testReactiveStreamsConsumerPollableChannel.

@Test
@SuppressWarnings("unchecked")
public void testReactiveStreamsConsumerPollableChannel() throws InterruptedException {
    QueueChannel testChannel = new QueueChannel();
    Subscriber<Message<?>> testSubscriber = (Subscriber<Message<?>>) Mockito.mock(Subscriber.class);
    BlockingQueue<Message<?>> messages = new LinkedBlockingQueue<>();
    willAnswer(i -> {
        messages.put(i.getArgument(0));
        return null;
    }).given(testSubscriber).onNext(any(Message.class));
    ReactiveStreamsConsumer reactiveConsumer = new ReactiveStreamsConsumer(testChannel, testSubscriber);
    reactiveConsumer.setBeanFactory(mock(BeanFactory.class));
    reactiveConsumer.afterPropertiesSet();
    reactiveConsumer.start();
    Message<?> testMessage = new GenericMessage<>("test");
    testChannel.send(testMessage);
    ArgumentCaptor<Subscription> subscriptionArgumentCaptor = ArgumentCaptor.forClass(Subscription.class);
    verify(testSubscriber).onSubscribe(subscriptionArgumentCaptor.capture());
    Subscription subscription = subscriptionArgumentCaptor.getValue();
    subscription.request(1);
    Message<?> message = messages.poll(10, TimeUnit.SECONDS);
    assertSame(testMessage, message);
    reactiveConsumer.stop();
    testChannel.send(testMessage);
    reactiveConsumer.start();
    verify(testSubscriber, times(2)).onSubscribe(subscriptionArgumentCaptor.capture());
    subscription = subscriptionArgumentCaptor.getValue();
    subscription.request(2);
    Message<?> testMessage2 = new GenericMessage<>("test2");
    testChannel.send(testMessage2);
    message = messages.poll(10, TimeUnit.SECONDS);
    assertSame(testMessage, message);
    message = messages.poll(10, TimeUnit.SECONDS);
    assertSame(testMessage2, message);
    verify(testSubscriber, never()).onError(any(Throwable.class));
    verify(testSubscriber, never()).onComplete();
    assertTrue(messages.isEmpty());
}
Also used : ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) GenericMessage(org.springframework.messaging.support.GenericMessage) Subscriber(org.reactivestreams.Subscriber) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Aggregations

Message (org.springframework.messaging.Message)597 Test (org.junit.Test)423 GenericMessage (org.springframework.messaging.support.GenericMessage)272 QueueChannel (org.springframework.integration.channel.QueueChannel)189 MessageChannel (org.springframework.messaging.MessageChannel)128 ArrayList (java.util.ArrayList)113 BeanFactory (org.springframework.beans.factory.BeanFactory)85 CountDownLatch (java.util.concurrent.CountDownLatch)80 DirectChannel (org.springframework.integration.channel.DirectChannel)69 AtomicReference (java.util.concurrent.atomic.AtomicReference)68 Test (org.junit.jupiter.api.Test)65 MessageHandler (org.springframework.messaging.MessageHandler)58 List (java.util.List)56 PollableChannel (org.springframework.messaging.PollableChannel)56 ErrorMessage (org.springframework.messaging.support.ErrorMessage)52 Matchers.containsString (org.hamcrest.Matchers.containsString)50 MessagingException (org.springframework.messaging.MessagingException)50 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)43 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)41 Assert.assertEquals (org.junit.Assert.assertEquals)40