Search in sources :

Example 1 with MessagingTemplate

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

the class ManualFlowTests method testRoleControl.

@Test
public void testRoleControl() {
    String testRole = "bridge";
    PollableChannel resultChannel = new QueueChannel();
    IntegrationFlowRegistration flowRegistration = this.integrationFlowContext.registration(flow -> flow.bridge(e -> e.role(testRole)).channel(resultChannel)).register();
    MessagingTemplate messagingTemplate = this.integrationFlowContext.messagingTemplateFor(flowRegistration.getId());
    messagingTemplate.send(new GenericMessage<>("test"));
    Message<?> receive = resultChannel.receive(1000);
    assertNotNull(receive);
    assertEquals("test", receive.getPayload());
    this.roleController.stopLifecyclesInRole(testRole);
    try {
        messagingTemplate.send(new GenericMessage<>("test2"));
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
        assertThat(e.getMessage(), containsString("Dispatcher has no subscribers for channel"));
    }
    this.roleController.startLifecyclesInRole(testRole);
    messagingTemplate.send(new GenericMessage<>("test2"));
    receive = resultChannel.receive(1000);
    assertNotNull(receive);
    assertEquals("test2", receive.getPayload());
    flowRegistration.destroy();
    assertTrue(this.roleController.getEndpointsRunningStatus(testRole).isEmpty());
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) Arrays(java.util.Arrays) Date(java.util.Date) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) Autowired(org.springframework.beans.factory.annotation.Autowired) IntegrationFlowAdapter(org.springframework.integration.dsl.IntegrationFlowAdapter) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) Assert.assertThat(org.junit.Assert.assertThat) MessageProducerSupport(org.springframework.integration.endpoint.MessageProducerSupport) Assert.fail(org.junit.Assert.fail) PollableChannel(org.springframework.messaging.PollableChannel) SpringRunner(org.springframework.test.context.junit4.SpringRunner) SmartLifecycleRoleController(org.springframework.integration.support.SmartLifecycleRoleController) MessageProducerSpec(org.springframework.integration.dsl.MessageProducerSpec) EnableIntegration(org.springframework.integration.config.EnableIntegration) MessageProducer(org.springframework.integration.core.MessageProducer) MessageChannel(org.springframework.messaging.MessageChannel) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Objects(java.util.Objects) Configuration(org.springframework.context.annotation.Configuration) IntegrationFlowDefinition(org.springframework.integration.dsl.IntegrationFlowDefinition) MessageHandler(org.springframework.messaging.MessageHandler) Assert.assertFalse(org.junit.Assert.assertFalse) DisposableBean(org.springframework.beans.factory.DisposableBean) Matchers.containsString(org.hamcrest.Matchers.containsString) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) BeanCreationNotAllowedException(org.springframework.beans.factory.BeanCreationNotAllowedException) RunWith(org.junit.runner.RunWith) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IntegrationFlowContext(org.springframework.integration.dsl.context.IntegrationFlowContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) MessageChannels(org.springframework.integration.dsl.MessageChannels) Scope(org.springframework.context.annotation.Scope) Assert.assertSame(org.junit.Assert.assertSame) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Message(org.springframework.messaging.Message) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Flux(reactor.core.publisher.Flux) Assert.assertNull(org.junit.Assert.assertNull) BeanFactory(org.springframework.beans.factory.BeanFactory) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Bean(org.springframework.context.annotation.Bean) GenericMessage(org.springframework.messaging.support.GenericMessage) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) Assert.assertEquals(org.junit.Assert.assertEquals) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) QueueChannel(org.springframework.integration.channel.QueueChannel) PollableChannel(org.springframework.messaging.PollableChannel) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) Matchers.containsString(org.hamcrest.Matchers.containsString) MessagingException(org.springframework.messaging.MessagingException) BeanCreationNotAllowedException(org.springframework.beans.factory.BeanCreationNotAllowedException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 2 with MessagingTemplate

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

the class EnableIntegrationTests method testAnnotatedServiceActivator.

@Test
public void testAnnotatedServiceActivator() throws Exception {
    this.serviceActivatorEndpoint.setReceiveTimeout(10000);
    this.serviceActivatorEndpoint.start();
    assertTrue(this.inputReceiveLatch.await(10, TimeUnit.SECONDS));
    assertEquals(10L, TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "maxMessagesPerPoll"));
    Trigger trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(100L, TestUtils.getPropertyValue(trigger, "period"));
    assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));
    assertTrue(this.annotationTestService.isRunning());
    Log logger = spy(TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "logger", Log.class));
    when(logger.isDebugEnabled()).thenReturn(true);
    final CountDownLatch pollerInterruptedLatch = new CountDownLatch(1);
    doAnswer(invocation -> {
        pollerInterruptedLatch.countDown();
        invocation.callRealMethod();
        return null;
    }).when(logger).debug("Received no Message during the poll, returning 'false'");
    new DirectFieldAccessor(this.serviceActivatorEndpoint).setPropertyValue("logger", logger);
    this.serviceActivatorEndpoint.stop();
    assertFalse(this.annotationTestService.isRunning());
    // wait until the service activator's poller is interrupted.
    assertTrue(pollerInterruptedLatch.await(10, TimeUnit.SECONDS));
    this.serviceActivatorEndpoint.start();
    assertTrue(this.annotationTestService.isRunning());
    trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint1, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(100L, TestUtils.getPropertyValue(trigger, "period"));
    assertTrue(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));
    trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint2, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(CronTrigger.class));
    assertEquals("0 5 7 * * *", TestUtils.getPropertyValue(trigger, "sequenceGenerator.expression"));
    trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint3, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(11L, TestUtils.getPropertyValue(trigger, "period"));
    assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));
    trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint4, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(1000L, TestUtils.getPropertyValue(trigger, "period"));
    assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));
    assertSame(this.myTrigger, trigger);
    trigger = TestUtils.getPropertyValue(this.transformer, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(10L, TestUtils.getPropertyValue(trigger, "period"));
    assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));
    this.input.send(MessageBuilder.withPayload("Foo").build());
    Message<?> interceptedMessage = this.wireTapChannel.receive(10000);
    assertNotNull(interceptedMessage);
    assertEquals("Foo", interceptedMessage.getPayload());
    Message<?> receive = this.output.receive(10000);
    assertNotNull(receive);
    assertEquals("FOO", receive.getPayload());
    receive = this.wireTapFromOutput.receive(10000);
    assertNotNull(receive);
    assertEquals("FOO", receive.getPayload());
    MessageHistory messageHistory = receive.getHeaders().get(MessageHistory.HEADER_NAME, MessageHistory.class);
    assertNotNull(messageHistory);
    String messageHistoryString = messageHistory.toString();
    assertThat(messageHistoryString, Matchers.containsString("input"));
    assertThat(messageHistoryString, Matchers.containsString("annotationTestService.handle.serviceActivator.handler"));
    assertThat(messageHistoryString, Matchers.not(Matchers.containsString("output")));
    receive = this.publishedChannel.receive(10000);
    assertNotNull(receive);
    assertEquals("foo", receive.getPayload());
    messageHistory = receive.getHeaders().get(MessageHistory.HEADER_NAME, MessageHistory.class);
    assertNotNull(messageHistory);
    messageHistoryString = messageHistory.toString();
    assertThat(messageHistoryString, Matchers.not(Matchers.containsString("input")));
    assertThat(messageHistoryString, Matchers.not(Matchers.containsString("output")));
    assertThat(messageHistoryString, Matchers.containsString("publishedChannel"));
    assertNull(this.wireTapChannel.receive(0));
    assertThat(this.testChannelInterceptor.getInvoked(), Matchers.greaterThan(0));
    assertThat(this.fbInterceptorCounter.get(), Matchers.greaterThan(0));
    assertTrue(this.context.containsBean("annotationTestService.count.inboundChannelAdapter.source"));
    Object messageSource = this.context.getBean("annotationTestService.count.inboundChannelAdapter.source");
    assertThat(messageSource, Matchers.instanceOf(MethodInvokingMessageSource.class));
    assertNull(this.counterChannel.receive(10));
    SmartLifecycle countSA = this.context.getBean("annotationTestService.count.inboundChannelAdapter", SmartLifecycle.class);
    assertFalse(countSA.isAutoStartup());
    assertEquals(23, countSA.getPhase());
    countSA.start();
    for (int i = 0; i < 10; i++) {
        Message<?> message = this.counterChannel.receive(1000);
        assertNotNull(message);
        assertEquals(i + 1, message.getPayload());
    }
    Message<?> message = this.fooChannel.receive(1000);
    assertNotNull(message);
    assertEquals("foo", message.getPayload());
    message = this.fooChannel.receive(1000);
    assertNotNull(message);
    assertEquals("foo", message.getPayload());
    assertNull(this.fooChannel.receive(10));
    message = this.messageChannel.receive(1000);
    assertNotNull(message);
    assertEquals("bar", message.getPayload());
    assertTrue(message.getHeaders().containsKey("foo"));
    assertEquals("FOO", message.getHeaders().get("foo"));
    MessagingTemplate messagingTemplate = new MessagingTemplate(this.controlBusChannel);
    assertFalse(messagingTemplate.convertSendAndReceive("@lifecycle.isRunning()", Boolean.class));
    this.controlBusChannel.send(new GenericMessage<String>("@lifecycle.start()"));
    assertTrue(messagingTemplate.convertSendAndReceive("@lifecycle.isRunning()", Boolean.class));
    this.controlBusChannel.send(new GenericMessage<String>("@lifecycle.stop()"));
    assertFalse(messagingTemplate.convertSendAndReceive("@lifecycle.isRunning()", Boolean.class));
}
Also used : CronTrigger(org.springframework.scheduling.support.CronTrigger) Log(org.apache.commons.logging.Log) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) SmartLifecycle(org.springframework.context.SmartLifecycle) AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) MessageHistory(org.springframework.integration.history.MessageHistory) EnableMessageHistory(org.springframework.integration.config.EnableMessageHistory) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) Trigger(org.springframework.scheduling.Trigger) CronTrigger(org.springframework.scheduling.support.CronTrigger) OnlyOnceTrigger(org.springframework.integration.test.util.OnlyOnceTrigger) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) MethodInvokingMessageSource(org.springframework.integration.endpoint.MethodInvokingMessageSource) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Test(org.junit.Test)

Example 3 with MessagingTemplate

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

the class HeaderEnricherTests method expressionWithLongType.

@Test
public void expressionWithLongType() {
    MessagingTemplate template = new MessagingTemplate();
    MessageChannel channel = context.getBean("expressionWithLongTypeInput", MessageChannel.class);
    Message<?> result = template.sendAndReceive(channel, new GenericMessage<String>("test"));
    assertNotNull(result);
    assertEquals(Long.class, result.getHeaders().get("number").getClass());
    assertEquals(12345L, result.getHeaders().get("number"));
}
Also used : MessagingTemplate(org.springframework.integration.core.MessagingTemplate) MessageChannel(org.springframework.messaging.MessageChannel) Test(org.junit.Test)

Example 4 with MessagingTemplate

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

the class HeaderEnricherTests method expressionUsingHeader.

@Test
public void expressionUsingHeader() {
    MessagingTemplate template = new MessagingTemplate();
    MessageChannel channel = context.getBean("headerExpressionInput", MessageChannel.class);
    Message<?> message = MessageBuilder.withPayload("test").setHeader("testHeader1", "foo").build();
    Message<?> result = template.sendAndReceive(channel, message);
    assertNotNull(result);
    assertEquals("foobar", result.getHeaders().get("testHeader2"));
}
Also used : MessagingTemplate(org.springframework.integration.core.MessagingTemplate) MessageChannel(org.springframework.messaging.MessageChannel) Test(org.junit.Test)

Example 5 with MessagingTemplate

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

the class HeaderEnricherTests method correlationIdValue.

@Test
public void correlationIdValue() {
    MessagingTemplate template = new MessagingTemplate();
    MessageChannel channel = context.getBean("correlationIdValueInput", MessageChannel.class);
    Message<?> result = template.sendAndReceive(channel, new GenericMessage<String>("test"));
    assertNotNull(result);
    assertEquals("ABC", new IntegrationMessageHeaderAccessor(result).getCorrelationId());
}
Also used : MessagingTemplate(org.springframework.integration.core.MessagingTemplate) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) MessageChannel(org.springframework.messaging.MessageChannel) 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