Search in sources :

Example 1 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project camel by apache.

the class CamelSourceAdapterTest method testSendingOneWayMessage.

@Test
public void testSendingOneWayMessage() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    DirectChannel channelA = getMandatoryBean(DirectChannel.class, "channelA");
    channelA.subscribe(new MessageHandler() {

        public void handleMessage(Message<?> message) {
            latch.countDown();
            assertEquals("We should get the message from channelA", message.getPayload(), "Willem");
        }
    });
    template.sendBody("direct:OneWay", "Willem");
    assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : MessageHandler(org.springframework.messaging.MessageHandler) DirectChannel(org.springframework.integration.channel.DirectChannel) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class DelayHandlerTests method errorChannelNameHeaderAndHandlerThrowsExceptionWithDelay.

@Test
public void errorChannelNameHeaderAndHandlerThrowsExceptionWithDelay() throws Exception {
    String errorChannelName = "customErrorChannel";
    StaticApplicationContext context = new StaticApplicationContext();
    context.registerSingleton(errorChannelName, DirectChannel.class);
    context.registerSingleton(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, DirectChannel.class);
    context.refresh();
    DirectChannel customErrorChannel = (DirectChannel) context.getBean(errorChannelName);
    MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler();
    errorHandler.setBeanFactory(context);
    taskScheduler.setErrorHandler(errorHandler);
    this.setDelayExpression();
    this.startDelayerHandler();
    output.unsubscribe(resultHandler);
    customErrorChannel.subscribe(resultHandler);
    output.subscribe(message -> {
        throw new UnsupportedOperationException("intentional test failure");
    });
    Message<?> message = MessageBuilder.withPayload("test").setHeader("delay", "10").setErrorChannelName(errorChannelName).build();
    input.send(message);
    waitForLatch(10000);
    Message<?> errorMessage = resultHandler.lastMessage;
    assertEquals(MessageDeliveryException.class, errorMessage.getPayload().getClass());
    MessageDeliveryException exceptionPayload = (MessageDeliveryException) errorMessage.getPayload();
    assertSame(message.getPayload(), exceptionPayload.getFailedMessage().getPayload());
    assertEquals(UnsupportedOperationException.class, exceptionPayload.getCause().getClass());
    assertNotSame(Thread.currentThread(), resultHandler.lastThread);
}
Also used : MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) DirectChannel(org.springframework.integration.channel.DirectChannel) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 3 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class DelayHandlerTests method defaultErrorChannelAndHandlerThrowsExceptionWithDelay.

@Test
public void defaultErrorChannelAndHandlerThrowsExceptionWithDelay() throws Exception {
    StaticApplicationContext context = new StaticApplicationContext();
    context.registerSingleton(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, DirectChannel.class);
    context.refresh();
    DirectChannel defaultErrorChannel = (DirectChannel) context.getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME);
    MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler();
    errorHandler.setBeanFactory(context);
    taskScheduler.setErrorHandler(errorHandler);
    this.setDelayExpression();
    this.startDelayerHandler();
    output.unsubscribe(resultHandler);
    defaultErrorChannel.subscribe(resultHandler);
    output.subscribe(message -> {
        throw new UnsupportedOperationException("intentional test failure");
    });
    Message<?> message = MessageBuilder.withPayload("test").setHeader("delay", "10").build();
    input.send(message);
    waitForLatch(10000);
    Message<?> errorMessage = resultHandler.lastMessage;
    assertEquals(MessageDeliveryException.class, errorMessage.getPayload().getClass());
    MessageDeliveryException exceptionPayload = (MessageDeliveryException) errorMessage.getPayload();
    assertSame(message.getPayload(), exceptionPayload.getFailedMessage().getPayload());
    assertEquals(UnsupportedOperationException.class, exceptionPayload.getCause().getClass());
    assertNotSame(Thread.currentThread(), resultHandler.lastThread);
}
Also used : MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) DirectChannel(org.springframework.integration.channel.DirectChannel) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 4 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class MessagingGatewayTests method validateErrorChannelWithSuccessfulReply.

// should not fail but it does now
@Test
public void validateErrorChannelWithSuccessfulReply() {
    TestUtils.TestApplicationContext testApplicationContext = TestUtils.createTestApplicationContext();
    testApplicationContext.refresh();
    DirectChannel reqChannel = new DirectChannel();
    reqChannel.subscribe(message -> {
        throw new RuntimeException("ooops");
    });
    PublishSubscribeChannel errorChannel = new PublishSubscribeChannel();
    ServiceActivatingHandler handler = new ServiceActivatingHandler(new MyOneWayErrorService());
    handler.setBeanFactory(testApplicationContext);
    handler.afterPropertiesSet();
    errorChannel.subscribe(handler);
    this.messagingGateway = new MessagingGatewaySupport() {
    };
    this.messagingGateway.setRequestChannel(reqChannel);
    this.messagingGateway.setErrorChannel(errorChannel);
    this.messagingGateway.setReplyChannel(null);
    this.messagingGateway.setBeanFactory(mock(BeanFactory.class));
    this.messagingGateway.afterPropertiesSet();
    this.messagingGateway.start();
    this.messagingGateway.send("hello");
    testApplicationContext.close();
}
Also used : TestUtils(org.springframework.integration.test.util.TestUtils) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) PublishSubscribeChannel(org.springframework.integration.channel.PublishSubscribeChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Test(org.junit.Test)

Example 5 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class MessageHistoryIntegrationTests method testMessageHistoryWithHistoryPerformance.

@Test
@Ignore
public void testMessageHistoryWithHistoryPerformance() {
    ConfigurableApplicationContext acWithHistory = new ClassPathXmlApplicationContext("perfWithMessageHistory.xml", MessageHistoryIntegrationTests.class);
    ConfigurableApplicationContext acWithoutHistory = new ClassPathXmlApplicationContext("perfWithoutMessageHistory.xml", MessageHistoryIntegrationTests.class);
    SampleGateway gatewayHistory = acWithHistory.getBean("sampleGateway", SampleGateway.class);
    DirectChannel endOfThePipeChannelHistory = acWithHistory.getBean("endOfThePipeChannel", DirectChannel.class);
    endOfThePipeChannelHistory.subscribe(message -> {
        MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
        replyChannel.send(message);
    });
    SampleGateway gateway = acWithoutHistory.getBean("sampleGateway", SampleGateway.class);
    DirectChannel endOfThePipeChannel = acWithoutHistory.getBean("endOfThePipeChannel", DirectChannel.class);
    endOfThePipeChannel.subscribe(message -> {
        MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
        replyChannel.send(message);
    });
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    for (int i = 0; i < 10000; i++) {
        gatewayHistory.echo("hello");
    }
    stopWatch.stop();
    logger.info("Elapsed time with history 10000 calls: " + stopWatch.getTotalTimeSeconds());
    stopWatch = new StopWatch();
    stopWatch.start();
    for (int i = 0; i < 10000; i++) {
        gateway.echo("hello");
    }
    stopWatch.stop();
    logger.info("Elapsed time without history 10000 calls: " + stopWatch.getTotalTimeSeconds());
    acWithHistory.close();
    acWithoutHistory.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) DirectChannel(org.springframework.integration.channel.DirectChannel) StopWatch(org.springframework.util.StopWatch) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

DirectChannel (org.springframework.integration.channel.DirectChannel)207 Test (org.junit.Test)179 Message (org.springframework.messaging.Message)62 MessageChannel (org.springframework.messaging.MessageChannel)60 QueueChannel (org.springframework.integration.channel.QueueChannel)58 BeanFactory (org.springframework.beans.factory.BeanFactory)45 GenericMessage (org.springframework.messaging.support.GenericMessage)37 MessageHandler (org.springframework.messaging.MessageHandler)32 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)28 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)26 CountDownLatch (java.util.concurrent.CountDownLatch)25 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)23 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)22 HashMap (java.util.HashMap)20 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)19 MessagingException (org.springframework.messaging.MessagingException)18 SubscribableChannel (org.springframework.messaging.SubscribableChannel)16 Properties (java.util.Properties)15 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)15 Expression (org.springframework.expression.Expression)14