Search in sources :

Example 61 with MessagingTemplate

use of org.springframework.integration.core.MessagingTemplate in project spring-integration by spring-projects.

the class ExtractRequestReplyPayloadTests method testOutboundInboundDefaultIsTx.

@Test
public void testOutboundInboundDefaultIsTx() {
    this.outboundGateway.setExtractRequestPayload(true);
    this.outboundGateway.setExtractReplyPayload(true);
    this.inboundGateway.setExtractReplyPayload(true);
    this.inboundGateway.setExtractRequestPayload(true);
    final AtomicBoolean failOnce = new AtomicBoolean();
    MessageHandler handler = message -> {
        assertTrue(message.getPayload() instanceof String);
        if (failOnce.compareAndSet(false, true)) {
            throw new RuntimeException("test tx");
        }
        MessagingTemplate template = new MessagingTemplate();
        template.setDefaultDestination((MessageChannel) message.getHeaders().getReplyChannel());
        template.send(message);
    };
    this.jmsInputChannel.subscribe(handler);
    this.outboundChannel.send(new GenericMessage<String>("Hello " + this.testName.getMethodName()));
    Message<?> replyMessage = this.replyChannel.receive(10000);
    assertTrue(replyMessage.getPayload() instanceof String);
    this.jmsInputChannel.unsubscribe(handler);
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) RunWith(org.junit.runner.RunWith) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Autowired(org.springframework.beans.factory.annotation.Autowired) Test(org.junit.Test) ChannelPublishingJmsMessageListener(org.springframework.integration.jms.ChannelPublishingJmsMessageListener) MessageChannel(org.springframework.messaging.MessageChannel) JMSException(javax.jms.JMSException) ApplicationContext(org.springframework.context.ApplicationContext) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) Rule(org.junit.Rule) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) MessageHandler(org.springframework.messaging.MessageHandler) TestName(org.junit.rules.TestName) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Assert.fail(org.junit.Assert.fail) Message(org.springframework.messaging.Message) PollableChannel(org.springframework.messaging.PollableChannel) JmsOutboundGateway(org.springframework.integration.jms.JmsOutboundGateway) GenericMessage(org.springframework.messaging.support.GenericMessage) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) MessageHandler(org.springframework.messaging.MessageHandler) MessageChannel(org.springframework.messaging.MessageChannel) Test(org.junit.Test)

Example 62 with MessagingTemplate

use of org.springframework.integration.core.MessagingTemplate in project spring-integration by spring-projects.

the class JmsOutboundGatewayParserTests method gatewayMaintainsReplyChannelAndInboundHistory.

@Test
public void gatewayMaintainsReplyChannelAndInboundHistory() {
    ActiveMqTestUtils.prepare();
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("gatewayMaintainsReplyChannel.xml", this.getClass());
    SampleGateway gateway = context.getBean("gateway", SampleGateway.class);
    SubscribableChannel jmsInput = context.getBean("jmsInput", SubscribableChannel.class);
    MessageHandler handler = message -> {
        MessageHistory history = MessageHistory.read(message);
        assertNotNull(history);
        Properties componentHistoryRecord = TestUtils.locateComponentInHistory(history, "inboundGateway", 0);
        assertNotNull(componentHistoryRecord);
        assertEquals("jms:inbound-gateway", componentHistoryRecord.get("type"));
        MessagingTemplate messagingTemplate = new MessagingTemplate();
        messagingTemplate.setDefaultDestination((MessageChannel) message.getHeaders().getReplyChannel());
        messagingTemplate.send(message);
    };
    jmsInput.subscribe(handler);
    String result = gateway.echo("hello");
    assertEquals("hello", result);
    JmsOutboundGateway gw1 = context.getBean("chain1$child.gateway.handler", JmsOutboundGateway.class);
    MessageChannel out = TestUtils.getPropertyValue(gw1, "outputChannel", MessageChannel.class);
    assertThat(out.getClass().getSimpleName(), equalTo("ReplyForwardingMessageChannel"));
    JmsOutboundGateway gw2 = context.getBean("chain2$child.gateway.handler", JmsOutboundGateway.class);
    out = TestUtils.getPropertyValue(gw2, "outputChannel", MessageChannel.class);
    assertThat(out.getClass().getName(), containsString("MessageHandlerChain$"));
    context.close();
}
Also used : DefaultMessageListenerContainer(org.springframework.jms.listener.DefaultMessageListenerContainer) AbstractRequestHandlerAdvice(org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice) SubscribableChannel(org.springframework.messaging.SubscribableChannel) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) TestUtils(org.springframework.integration.test.util.TestUtils) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) MessageBuilder(org.springframework.integration.support.MessageBuilder) Session(javax.jms.Session) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) MessageHistory(org.springframework.integration.history.MessageHistory) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Assert.fail(org.junit.Assert.fail) Message(org.springframework.messaging.Message) JmsOutboundGateway(org.springframework.integration.jms.JmsOutboundGateway) Method(java.lang.reflect.Method) DeliveryMode(javax.jms.DeliveryMode) Properties(java.util.Properties) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) MessageChannel(org.springframework.messaging.MessageChannel) ExpressionEvaluatingMessageProcessor(org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor) Queue(javax.jms.Queue) BeanDefinitionParsingException(org.springframework.beans.factory.parsing.BeanDefinitionParsingException) Destination(javax.jms.Destination) MessageHandler(org.springframework.messaging.MessageHandler) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Expression(org.springframework.expression.Expression) MessageConverter(org.springframework.jms.support.converter.MessageConverter) GenericMessage(org.springframework.messaging.support.GenericMessage) Matchers.containsString(org.hamcrest.Matchers.containsString) NotReadablePropertyException(org.springframework.beans.NotReadablePropertyException) Assert.assertEquals(org.junit.Assert.assertEquals) StubMessageConverter(org.springframework.integration.jms.StubMessageConverter) Mockito.mock(org.mockito.Mockito.mock) MessageHistory(org.springframework.integration.history.MessageHistory) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) MessageHandler(org.springframework.messaging.MessageHandler) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) JmsOutboundGateway(org.springframework.integration.jms.JmsOutboundGateway) Matchers.containsString(org.hamcrest.Matchers.containsString) Properties(java.util.Properties) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Test(org.junit.Test)

Example 63 with MessagingTemplate

use of org.springframework.integration.core.MessagingTemplate in project spring-integration by spring-projects.

the class XmppHeaderEnricherParserTests method to.

@SuppressWarnings("rawtypes")
@Test
public void to() {
    MessagingTemplate messagingTemplate = new MessagingTemplate();
    MessageHandler handler = mock(MessageHandler.class);
    doAnswer(invocation -> {
        Message message = invocation.getArgument(0);
        String chatToUser = (String) message.getHeaders().get(XmppHeaders.TO);
        assertNotNull(chatToUser);
        assertEquals("test1@example.org", chatToUser);
        return null;
    }).when(handler).handleMessage(Mockito.any(Message.class));
    output.subscribe(handler);
    messagingTemplate.send(input, MessageBuilder.withPayload("foo").build());
    verify(handler, times(1)).handleMessage(Mockito.any(Message.class));
}
Also used : MessagingTemplate(org.springframework.integration.core.MessagingTemplate) MessageHandler(org.springframework.messaging.MessageHandler) Message(org.springframework.messaging.Message) Test(org.junit.Test)

Aggregations

MessagingTemplate (org.springframework.integration.core.MessagingTemplate)63 Test (org.junit.Test)58 MessageChannel (org.springframework.messaging.MessageChannel)22 PollableChannel (org.springframework.messaging.PollableChannel)12 GenericMessage (org.springframework.messaging.support.GenericMessage)11 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)9 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)8 Message (org.springframework.messaging.Message)6 File (java.io.File)5 Map (java.util.Map)5 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Assert.assertThat (org.junit.Assert.assertThat)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 Assert.fail (org.junit.Assert.fail)4 MessageHandler (org.springframework.messaging.MessageHandler)4 Date (java.util.Date)3 HashMap (java.util.HashMap)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)3 Assert.assertEquals (org.junit.Assert.assertEquals)3