Search in sources :

Example 1 with MessageHistory

use of org.springframework.integration.history.MessageHistory 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 2 with MessageHistory

use of org.springframework.integration.history.MessageHistory in project spring-integration by spring-projects.

the class MessageHistoryTests method testCorrectErrorMessageAfterWrite.

@Test
public void testCorrectErrorMessageAfterWrite() {
    RuntimeException payload = new RuntimeException();
    ErrorMessage original = new ErrorMessage(payload);
    assertNull(MessageHistory.read(original));
    Message<Throwable> result1 = MessageHistory.write(original, new TestComponent(1));
    assertThat(result1, instanceOf(ErrorMessage.class));
    assertNotSame(original, result1);
    assertSame(original.getPayload(), result1.getPayload());
    MessageHistory history1 = MessageHistory.read(result1);
    assertNotNull(history1);
    assertEquals("testComponent-1", history1.toString());
    Message<Throwable> result2 = MessageHistory.write(result1, new TestComponent(2));
    assertThat(result2, instanceOf(ErrorMessage.class));
    assertNotSame(original, result2);
    assertNotSame(result1, result2);
    assertSame(original.getPayload(), result2.getPayload());
    MessageHistory history2 = MessageHistory.read(result2);
    assertNotNull(history2);
    assertEquals("testComponent-1,testComponent-2", history2.toString());
}
Also used : MessageHistory(org.springframework.integration.history.MessageHistory) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Test(org.junit.Test)

Example 3 with MessageHistory

use of org.springframework.integration.history.MessageHistory in project spring-integration by spring-projects.

the class MessageHistoryTests method testCorrectAdviceMessageAfterWrite.

@Test
public void testCorrectAdviceMessageAfterWrite() {
    Message<?> inputMessage = new GenericMessage<>("input");
    AdviceMessage<String> original = new AdviceMessage<>("foo", inputMessage);
    assertNull(MessageHistory.read(original));
    Message<String> result1 = MessageHistory.write(original, new TestComponent(1));
    assertThat(result1, instanceOf(AdviceMessage.class));
    assertNotSame(original, result1);
    assertSame(original.getPayload(), result1.getPayload());
    assertSame(original.getInputMessage(), ((AdviceMessage<?>) result1).getInputMessage());
    MessageHistory history1 = MessageHistory.read(result1);
    assertNotNull(history1);
    assertEquals("testComponent-1", history1.toString());
    Message<String> result2 = MessageHistory.write(result1, new TestComponent(2));
    assertThat(result2, instanceOf(AdviceMessage.class));
    assertNotSame(original, result2);
    assertSame(original.getPayload(), result2.getPayload());
    assertSame(original.getInputMessage(), ((AdviceMessage<?>) result2).getInputMessage());
    assertNotSame(result1, result2);
    MessageHistory history2 = MessageHistory.read(result2);
    assertNotNull(history2);
    assertEquals("testComponent-1,testComponent-2", history2.toString());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHistory(org.springframework.integration.history.MessageHistory) AdviceMessage(org.springframework.integration.message.AdviceMessage) Test(org.junit.Test)

Example 4 with MessageHistory

use of org.springframework.integration.history.MessageHistory in project spring-integration by spring-projects.

the class BeanFactoryTypeConverterTests method testMessageHistoryNotConverted.

@Test
public void testMessageHistoryNotConverted() {
    BeanFactoryTypeConverter typeConverter = new BeanFactoryTypeConverter();
    typeConverter.setBeanFactory(new DefaultListableBeanFactory());
    Message<String> message = new GenericMessage<String>("foo");
    message = MessageHistory.write(message, new NamedComponent() {

        @Override
        public String getComponentName() {
            return "bar";
        }

        @Override
        public String getComponentType() {
            return "baz";
        }
    });
    MessageHistory history = MessageHistory.read(message);
    assertSame(history, typeConverter.convertValue(history, TypeDescriptor.valueOf(MessageHistory.class), TypeDescriptor.valueOf(MessageHistory.class)));
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHistory(org.springframework.integration.history.MessageHistory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) NamedComponent(org.springframework.integration.support.context.NamedComponent) Test(org.junit.Test)

Example 5 with MessageHistory

use of org.springframework.integration.history.MessageHistory in project spring-integration by spring-projects.

the class JmsChannelHistoryTests method testFullConfig.

@Test
public void testFullConfig() throws Exception {
    ActiveMqTestUtils.prepare();
    ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("JmsChannelHistoryTests-context.xml", this.getClass());
    SubscribableChannel channel = ac.getBean("jmsChannel", SubscribableChannel.class);
    PollableChannel resultChannel = ac.getBean("resultChannel", PollableChannel.class);
    channel.send(new GenericMessage<String>("hello"));
    Message<?> resultMessage = resultChannel.receive(10000);
    MessageHistory history = MessageHistory.read(resultMessage);
    assertTrue(history.get(0).contains("jmsChannel"));
    ac.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) MessageHistory(org.springframework.integration.history.MessageHistory) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollableChannel(org.springframework.messaging.PollableChannel) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Test(org.junit.Test)

Aggregations

MessageHistory (org.springframework.integration.history.MessageHistory)37 Test (org.junit.Test)34 Properties (java.util.Properties)26 GenericMessage (org.springframework.messaging.support.GenericMessage)13 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)8 DirectChannel (org.springframework.integration.channel.DirectChannel)8 PollableChannel (org.springframework.messaging.PollableChannel)8 Message (org.springframework.messaging.Message)5 Matchers.containsString (org.hamcrest.Matchers.containsString)4 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)3 QueueChannel (org.springframework.integration.channel.QueueChannel)3 EnableMessageHistory (org.springframework.integration.config.EnableMessageHistory)3 MongoClient (com.mongodb.MongoClient)2 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)2 SimpleMongoDbFactory (org.springframework.data.mongodb.core.SimpleMongoDbFactory)2 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)2 TcpConnection (org.springframework.integration.ip.tcp.connection.TcpConnection)2 JmsMessageDrivenEndpoint (org.springframework.integration.jms.JmsMessageDrivenEndpoint)2 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)2 MessageChannel (org.springframework.messaging.MessageChannel)2