Search in sources :

Example 76 with Message

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

the class TestAnnotatedEndpointWithReleaseStrategy method aggregatingMethod.

@Aggregator(inputChannel = "inputChannel")
public Message<?> aggregatingMethod(List<Message<?>> messages) {
    List<Message<?>> sortableList = new ArrayList<>(messages);
    Collections.sort(sortableList, new MessageSequenceComparator());
    StringBuffer buffer = new StringBuffer();
    Object correlationId = null;
    for (Message<?> message : sortableList) {
        buffer.append(message.getPayload().toString());
        if (null == correlationId) {
            correlationId = new IntegrationMessageHeaderAccessor(message).getCorrelationId();
        }
    }
    Message<?> returnedMessage = new GenericMessage<>(buffer.toString());
    this.aggregatedMessages.put(correlationId, returnedMessage);
    return returnedMessage;
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) GenericMessage(org.springframework.messaging.support.GenericMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageSequenceComparator(org.springframework.integration.aggregator.MessageSequenceComparator) ArrayList(java.util.ArrayList) Aggregator(org.springframework.integration.annotation.Aggregator)

Example 77 with Message

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

the class EnricherParserTests method integrationTest.

@Test
public void integrationTest() {
    QueueChannel output = context.getBean("output", QueueChannel.class);
    output.purge(null);
    SubscribableChannel requests = context.getBean("requests", SubscribableChannel.class);
    class Foo extends AbstractReplyProducingMessageHandler {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return new Source("foo");
        }
    }
    Foo foo = new Foo();
    foo.setOutputChannel(context.getBean("replies", MessageChannel.class));
    requests.subscribe(foo);
    Target original = new Target();
    Message<?> request = MessageBuilder.withPayload(original).setHeader("sourceName", "test").setHeader("notOverwrite", "test").build();
    context.getBean("input", MessageChannel.class).send(request);
    Message<?> reply = output.receive(0);
    Target enriched = (Target) reply.getPayload();
    assertEquals("foo", enriched.getName());
    assertEquals(42, enriched.getAge());
    assertEquals(Gender.MALE, enriched.getGender());
    assertTrue(enriched.isMarried());
    assertNotSame(original, enriched);
    assertEquals(1, adviceCalled);
    MessageHeaders headers = reply.getHeaders();
    assertEquals("bar", headers.get("foo"));
    assertEquals(Gender.MALE, headers.get("testBean"));
    assertEquals("foo", headers.get("sourceName"));
    assertEquals("test", headers.get("notOverwrite"));
    requests.unsubscribe(foo);
    adviceCalled--;
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageChannel(org.springframework.messaging.MessageChannel) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MessageHeaders(org.springframework.messaging.MessageHeaders) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Test(org.junit.Test)

Example 78 with Message

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

the class EnricherParserTests3 method testSourceBeanResolver.

@Test
public void testSourceBeanResolver() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-context.xml", this.getClass());
    MessageChannel beanResolveIn = context.getBean("beanResolveIn", MessageChannel.class);
    PollableChannel beanResolveOut = context.getBean("beanResolveOut", PollableChannel.class);
    SomeBean payload = new SomeBean("foo");
    assertEquals("foo", payload.getNested().getValue());
    beanResolveIn.send(new GenericMessage<SomeBean>(payload));
    @SuppressWarnings("unchecked") Message<SomeBean> out = (Message<SomeBean>) beanResolveOut.receive();
    assertSame(payload, out.getPayload());
    assertEquals("bar", out.getPayload().getNested().getValue());
    context.close();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollableChannel(org.springframework.messaging.PollableChannel) Test(org.junit.Test)

Example 79 with Message

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

the class SourcePollingChannelAdapterFactoryBeanTests method testTransactionalAdviceChain.

@Test
public void testTransactionalAdviceChain() throws Throwable {
    SourcePollingChannelAdapterFactoryBean factoryBean = new SourcePollingChannelAdapterFactoryBean();
    QueueChannel outputChannel = new QueueChannel();
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    factoryBean.setBeanFactory(context.getBeanFactory());
    factoryBean.setBeanClassLoader(ClassUtils.getDefaultClassLoader());
    factoryBean.setOutputChannel(outputChannel);
    factoryBean.setSource(() -> new GenericMessage<>("test"));
    PollerMetadata pollerMetadata = new PollerMetadata();
    List<Advice> adviceChain = new ArrayList<Advice>();
    final AtomicBoolean adviceApplied = new AtomicBoolean(false);
    adviceChain.add((MethodInterceptor) invocation -> {
        adviceApplied.set(true);
        return invocation.proceed();
    });
    pollerMetadata.setTrigger(new PeriodicTrigger(5000));
    pollerMetadata.setMaxMessagesPerPoll(1);
    final AtomicInteger count = new AtomicInteger();
    final MethodInterceptor txAdvice = mock(MethodInterceptor.class);
    adviceChain.add((MethodInterceptor) invocation -> {
        count.incrementAndGet();
        return invocation.proceed();
    });
    when(txAdvice.invoke(any(MethodInvocation.class))).thenAnswer(invocation -> {
        count.incrementAndGet();
        return ((MethodInvocation) invocation.getArgument(0)).proceed();
    });
    pollerMetadata.setAdviceChain(adviceChain);
    factoryBean.setPollerMetadata(pollerMetadata);
    factoryBean.setAutoStartup(true);
    factoryBean.afterPropertiesSet();
    context.registerEndpoint("testPollingEndpoint", factoryBean.getObject());
    context.refresh();
    Message<?> message = outputChannel.receive(5000);
    assertEquals("test", message.getPayload());
    assertEquals(1, count.get());
    assertTrue("adviceChain was not applied", adviceApplied.get());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ArgumentMatchers.contains(org.mockito.ArgumentMatchers.contains) Mockito.spy(org.mockito.Mockito.spy) TestUtils(org.springframework.integration.test.util.TestUtils) MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) MessageSource(org.springframework.integration.core.MessageSource) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) ArrayList(java.util.ArrayList) MethodInvocation(org.aopalliance.intercept.MethodInvocation) NullChannel(org.springframework.integration.channel.NullChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Advice(org.aopalliance.aop.Advice) Message(org.springframework.messaging.Message) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) ClassUtils(org.springframework.util.ClassUtils) Trigger(org.springframework.scheduling.Trigger) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) TaskScheduler(org.springframework.scheduling.TaskScheduler) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Mockito.verify(org.mockito.Mockito.verify) Lifecycle(org.springframework.context.Lifecycle) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) List(java.util.List) BeanFactory(org.springframework.beans.factory.BeanFactory) Log(org.apache.commons.logging.Log) GenericMessage(org.springframework.messaging.support.GenericMessage) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) QueueChannel(org.springframework.integration.channel.QueueChannel) ArrayList(java.util.ArrayList) MethodInvocation(org.aopalliance.intercept.MethodInvocation) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Advice(org.aopalliance.aop.Advice) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) Test(org.junit.Test)

Example 80 with Message

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

the class SourcePollingChannelAdapterFactoryBeanTests method testAdviceChain.

@Test
public void testAdviceChain() throws Exception {
    SourcePollingChannelAdapterFactoryBean factoryBean = new SourcePollingChannelAdapterFactoryBean();
    QueueChannel outputChannel = new QueueChannel();
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    factoryBean.setBeanFactory(context.getBeanFactory());
    factoryBean.setBeanClassLoader(ClassUtils.getDefaultClassLoader());
    factoryBean.setOutputChannel(outputChannel);
    factoryBean.setSource(() -> new GenericMessage<>("test"));
    PollerMetadata pollerMetadata = new PollerMetadata();
    List<Advice> adviceChain = new ArrayList<Advice>();
    final AtomicBoolean adviceApplied = new AtomicBoolean(false);
    adviceChain.add((MethodInterceptor) invocation -> {
        adviceApplied.set(true);
        return invocation.proceed();
    });
    pollerMetadata.setTrigger(new PeriodicTrigger(5000));
    pollerMetadata.setMaxMessagesPerPoll(1);
    pollerMetadata.setAdviceChain(adviceChain);
    factoryBean.setPollerMetadata(pollerMetadata);
    factoryBean.setAutoStartup(true);
    factoryBean.afterPropertiesSet();
    context.registerEndpoint("testPollingEndpoint", factoryBean.getObject());
    context.refresh();
    Message<?> message = outputChannel.receive(5000);
    assertEquals("test", message.getPayload());
    assertTrue("adviceChain was not applied", adviceApplied.get());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ArgumentMatchers.contains(org.mockito.ArgumentMatchers.contains) Mockito.spy(org.mockito.Mockito.spy) TestUtils(org.springframework.integration.test.util.TestUtils) MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) MessageSource(org.springframework.integration.core.MessageSource) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) ArrayList(java.util.ArrayList) MethodInvocation(org.aopalliance.intercept.MethodInvocation) NullChannel(org.springframework.integration.channel.NullChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Advice(org.aopalliance.aop.Advice) Message(org.springframework.messaging.Message) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) ClassUtils(org.springframework.util.ClassUtils) Trigger(org.springframework.scheduling.Trigger) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) TaskScheduler(org.springframework.scheduling.TaskScheduler) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Mockito.verify(org.mockito.Mockito.verify) Lifecycle(org.springframework.context.Lifecycle) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) List(java.util.List) BeanFactory(org.springframework.beans.factory.BeanFactory) Log(org.apache.commons.logging.Log) GenericMessage(org.springframework.messaging.support.GenericMessage) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QueueChannel(org.springframework.integration.channel.QueueChannel) ArrayList(java.util.ArrayList) Advice(org.aopalliance.aop.Advice) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) 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