Search in sources :

Example 56 with Message

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

the class PseudoTransactionalMessageSourceTests method testCommitWithManager.

@Test
public void testCommitWithManager() {
    final PollableChannel queueChannel = new QueueChannel();
    TransactionTemplate transactionTemplate = new TransactionTemplate(new PseudoTransactionManager());
    transactionTemplate.execute(status -> {
        SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
        ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor();
        syncProcessor.setBeanFactory(mock(BeanFactory.class));
        syncProcessor.setBeforeCommitExpression(new SpelExpressionParser().parseExpression("#bix"));
        syncProcessor.setBeforeCommitChannel(queueChannel);
        syncProcessor.setAfterCommitChannel(queueChannel);
        syncProcessor.setAfterCommitExpression(new SpelExpressionParser().parseExpression("#baz"));
        DefaultTransactionSynchronizationFactory syncFactory = new DefaultTransactionSynchronizationFactory(syncProcessor);
        adapter.setTransactionSynchronizationFactory(syncFactory);
        QueueChannel outputChannel = new QueueChannel();
        adapter.setOutputChannel(outputChannel);
        adapter.setSource(new MessageSource<String>() {

            @Override
            public Message<String> receive() {
                GenericMessage<String> message = new GenericMessage<String>("foo");
                IntegrationResourceHolder holder = (IntegrationResourceHolder) TransactionSynchronizationManager.getResource(this);
                holder.addAttribute("baz", "qux");
                holder.addAttribute("bix", "qox");
                return message;
            }
        });
        doPoll(adapter);
        return null;
    });
    Message<?> beforeCommitMessage = queueChannel.receive(1000);
    assertNotNull(beforeCommitMessage);
    assertEquals("qox", beforeCommitMessage.getPayload());
    Message<?> afterCommitMessage = queueChannel.receive(1000);
    assertNotNull(afterCommitMessage);
    assertEquals("qux", afterCommitMessage.getPayload());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) ExpressionEvaluatingTransactionSynchronizationProcessor(org.springframework.integration.transaction.ExpressionEvaluatingTransactionSynchronizationProcessor) DefaultTransactionSynchronizationFactory(org.springframework.integration.transaction.DefaultTransactionSynchronizationFactory) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) PseudoTransactionManager(org.springframework.integration.transaction.PseudoTransactionManager) GenericMessage(org.springframework.messaging.support.GenericMessage) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) IntegrationResourceHolder(org.springframework.integration.transaction.IntegrationResourceHolder) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 57 with Message

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

the class PseudoTransactionalMessageSourceTests method testRollback.

@Test
public void testRollback() {
    SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
    ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor();
    syncProcessor.setBeanFactory(mock(BeanFactory.class));
    PollableChannel queueChannel = new QueueChannel();
    syncProcessor.setAfterRollbackChannel(queueChannel);
    syncProcessor.setAfterRollbackExpression(new SpelExpressionParser().parseExpression("#baz"));
    DefaultTransactionSynchronizationFactory syncFactory = new DefaultTransactionSynchronizationFactory(syncProcessor);
    adapter.setTransactionSynchronizationFactory(syncFactory);
    QueueChannel outputChannel = new QueueChannel();
    adapter.setOutputChannel(outputChannel);
    final Message<?> testMessage = new GenericMessage<>("foo");
    adapter.setSource(new MessageSource<String>() {

        @Override
        public Message<String> receive() {
            GenericMessage<String> message = new GenericMessage<String>("foo");
            ((IntegrationResourceHolder) TransactionSynchronizationManager.getResource(this)).addAttribute("baz", testMessage);
            return message;
        }
    });
    TransactionSynchronizationManager.initSynchronization();
    TransactionSynchronizationManager.setActualTransactionActive(true);
    doPoll(adapter);
    TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
    Message<?> rollbackMessage = queueChannel.receive(1000);
    assertNotNull(rollbackMessage);
    assertSame(testMessage, rollbackMessage);
    TransactionSynchronizationManager.clearSynchronization();
    TransactionSynchronizationManager.setActualTransactionActive(false);
}
Also used : ExpressionEvaluatingTransactionSynchronizationProcessor(org.springframework.integration.transaction.ExpressionEvaluatingTransactionSynchronizationProcessor) QueueChannel(org.springframework.integration.channel.QueueChannel) DefaultTransactionSynchronizationFactory(org.springframework.integration.transaction.DefaultTransactionSynchronizationFactory) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) BeanFactory(org.springframework.beans.factory.BeanFactory) PollableChannel(org.springframework.messaging.PollableChannel) Test(org.junit.Test)

Example 58 with Message

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

the class ServiceActivatorEndpointTests method dynamicReplyChannel.

@Test
public void dynamicReplyChannel() throws Exception {
    TestUtils.TestApplicationContext testApplicationContext = TestUtils.createTestApplicationContext();
    testApplicationContext.refresh();
    final QueueChannel replyChannel1 = new QueueChannel();
    final QueueChannel replyChannel2 = new QueueChannel();
    replyChannel2.setBeanName("replyChannel2");
    Object handler = new Object() {

        @SuppressWarnings("unused")
        public Message<?> handle(Message<?> message) {
            return new GenericMessage<String>("foo" + message.getPayload());
        }
    };
    ServiceActivatingHandler endpoint = new ServiceActivatingHandler(handler, "handle");
    TestChannelResolver channelResolver = new TestChannelResolver();
    channelResolver.addChannel("replyChannel2", replyChannel2);
    endpoint.setChannelResolver(channelResolver);
    endpoint.setBeanFactory(testApplicationContext);
    endpoint.afterPropertiesSet();
    Message<String> testMessage1 = MessageBuilder.withPayload("bar").setReplyChannel(replyChannel1).build();
    endpoint.handleMessage(testMessage1);
    Message<?> reply1 = replyChannel1.receive(50);
    assertNotNull(reply1);
    assertEquals("foobar", reply1.getPayload());
    Message<?> reply2 = replyChannel2.receive(0);
    assertNull(reply2);
    Message<String> testMessage2 = MessageBuilder.fromMessage(testMessage1).setReplyChannelName("replyChannel2").build();
    endpoint.handleMessage(testMessage2);
    reply1 = replyChannel1.receive(0);
    assertNull(reply1);
    reply2 = replyChannel2.receive(0);
    assertNotNull(reply2);
    assertEquals("foobar", reply2.getPayload());
    testApplicationContext.close();
}
Also used : TestUtils(org.springframework.integration.test.util.TestUtils) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) TestChannelResolver(org.springframework.integration.channel.TestChannelResolver) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Test(org.junit.Test)

Example 59 with Message

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

the class MapToObjectTransformerParserTests method testMapToObjectTransformationWithRef.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testMapToObjectTransformationWithRef() {
    Map map = new HashMap();
    map.put("fname", "Justin");
    map.put("lname", "Case");
    Address address = new Address();
    address.setStreet("1123 Main st");
    map.put("address", address);
    Message message = MessageBuilder.withPayload(map).build();
    inputA.send(message);
    Message<?> newMessage = outputA.receive();
    Person person = (Person) newMessage.getPayload();
    assertNotNull(person);
    assertEquals("Justin", person.getFname());
    assertEquals("Case", person.getLname());
    assertNull(person.getSsn());
    assertNotNull(person.getAddress());
    assertEquals("1123 Main st", person.getAddress().getStreet());
}
Also used : Message(org.springframework.messaging.Message) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 60 with Message

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

the class AsyncMessagingTemplateTests method asyncReceiveWithTimeoutException.

@Test(expected = TimeoutException.class)
public void asyncReceiveWithTimeoutException() throws Exception {
    AsyncMessagingTemplate template = new AsyncMessagingTemplate();
    Future<Message<?>> result = template.asyncReceive(new QueueChannel());
    result.get(100, TimeUnit.MILLISECONDS);
}
Also used : Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) 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