Search in sources :

Example 1 with MethodInvokingMessageHandler

use of org.springframework.integration.handler.MethodInvokingMessageHandler in project spring-integration by spring-projects.

the class MethodInvokingMessageHandlerTests method subscription.

@Test
public void subscription() throws Exception {
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    SynchronousQueue<String> queue = new SynchronousQueue<String>();
    TestBean testBean = new TestBean(queue);
    QueueChannel channel = new QueueChannel();
    context.registerChannel("channel", channel);
    Message<String> message = new GenericMessage<String>("testing");
    channel.send(message);
    assertNull(queue.poll());
    MethodInvokingMessageHandler handler = new MethodInvokingMessageHandler(testBean, "foo");
    PollingConsumer endpoint = new PollingConsumer(channel, handler);
    endpoint.setTrigger(new PeriodicTrigger(10));
    context.registerEndpoint("testEndpoint", endpoint);
    context.refresh();
    String result = queue.poll(2000, TimeUnit.MILLISECONDS);
    assertNotNull(result);
    assertEquals("testing", result);
    context.stop();
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) QueueChannel(org.springframework.integration.channel.QueueChannel) SynchronousQueue(java.util.concurrent.SynchronousQueue) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Test(org.junit.Test)

Example 2 with MethodInvokingMessageHandler

use of org.springframework.integration.handler.MethodInvokingMessageHandler in project spring-integration by spring-projects.

the class ReactiveStreamsConsumerTests method testReactiveStreamsConsumerFluxMessageChannel.

@Test
public void testReactiveStreamsConsumerFluxMessageChannel() throws InterruptedException {
    FluxMessageChannel testChannel = new FluxMessageChannel();
    List<Message<?>> result = new LinkedList<>();
    CountDownLatch stopLatch = new CountDownLatch(2);
    MessageHandler messageHandler = m -> {
        result.add(m);
        stopLatch.countDown();
    };
    MessageHandler testSubscriber = new MethodInvokingMessageHandler(messageHandler, (String) null);
    ReactiveStreamsConsumer reactiveConsumer = new ReactiveStreamsConsumer(testChannel, testSubscriber);
    reactiveConsumer.setBeanFactory(mock(BeanFactory.class));
    reactiveConsumer.afterPropertiesSet();
    reactiveConsumer.start();
    Message<?> testMessage = new GenericMessage<>("test");
    testChannel.send(testMessage);
    reactiveConsumer.stop();
    try {
        testChannel.send(testMessage);
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
        assertThat(e.getCause(), instanceOf(IllegalStateException.class));
        assertThat(e.getMessage(), containsString("doesn't have subscribers to accept messages"));
    }
    reactiveConsumer.start();
    Message<?> testMessage2 = new GenericMessage<>("test2");
    testChannel.send(testMessage2);
    assertTrue(stopLatch.await(10, TimeUnit.SECONDS));
    assertThat(result, Matchers.<Message<?>>contains(testMessage, testMessage2));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) QueueChannel(org.springframework.integration.channel.QueueChannel) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) Assert.fail(org.junit.Assert.fail) Message(org.springframework.messaging.Message) LinkedList(java.util.LinkedList) Subscriber(org.reactivestreams.Subscriber) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Mockito.times(org.mockito.Mockito.times) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) ConsumerEndpointFactoryBean(org.springframework.integration.config.ConsumerEndpointFactoryBean) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Mockito.verify(org.mockito.Mockito.verify) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Mockito.never(org.mockito.Mockito.never) List(java.util.List) MessageHandler(org.springframework.messaging.MessageHandler) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Subscription(org.reactivestreams.Subscription) BeanFactory(org.springframework.beans.factory.BeanFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) DirectChannel(org.springframework.integration.channel.DirectChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) GenericMessage(org.springframework.messaging.support.GenericMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) Test(org.junit.Test)

Example 3 with MethodInvokingMessageHandler

use of org.springframework.integration.handler.MethodInvokingMessageHandler in project spring-integration by spring-projects.

the class MethodInvokingMessageHandlerTests method validMethod.

@Test
public void validMethod() {
    MethodInvokingMessageHandler handler = new MethodInvokingMessageHandler(new TestSink(), "validMethod");
    handler.handleMessage(new GenericMessage<String>("test"));
}
Also used : MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) Test(org.junit.Test)

Example 4 with MethodInvokingMessageHandler

use of org.springframework.integration.handler.MethodInvokingMessageHandler in project spring-integration by spring-projects.

the class MethodInvokingMessageHandlerTests method methodWithReturnValue.

@Test(expected = MessagingException.class)
public void methodWithReturnValue() {
    Message<?> message = new GenericMessage<String>("test");
    try {
        MethodInvokingMessageHandler handler = new MethodInvokingMessageHandler(new TestSink(), "methodWithReturnValue");
        handler.handleMessage(message);
    } catch (MessagingException e) {
        assertEquals(e.getFailedMessage(), message);
        throw e;
    }
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) MessagingException(org.springframework.messaging.MessagingException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 MethodInvokingMessageHandler (org.springframework.integration.handler.MethodInvokingMessageHandler)4 GenericMessage (org.springframework.messaging.support.GenericMessage)3 QueueChannel (org.springframework.integration.channel.QueueChannel)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 BlockingQueue (java.util.concurrent.BlockingQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 SynchronousQueue (java.util.concurrent.SynchronousQueue)1 TimeUnit (java.util.concurrent.TimeUnit)1 Matchers (org.hamcrest.Matchers)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)1 Assert.assertSame (org.junit.Assert.assertSame)1 Assert.assertThat (org.junit.Assert.assertThat)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Assert.fail (org.junit.Assert.fail)1 ArgumentCaptor (org.mockito.ArgumentCaptor)1