Search in sources :

Example 71 with QueueChannel

use of org.springframework.integration.channel.QueueChannel 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 72 with QueueChannel

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

the class ReactiveStreamsTests method testFromPublisher.

@Test
public void testFromPublisher() {
    Flux<Message<?>> messageFlux = Flux.just("1,2,3,4").map(v -> v.split(",")).flatMapIterable(Arrays::asList).map(Integer::parseInt).log("org.springframework.integration.flux").map(GenericMessage<Integer>::new);
    QueueChannel resultChannel = new QueueChannel();
    IntegrationFlow integrationFlow = IntegrationFlows.from(messageFlux).log("org.springframework.integration.flux2").<Integer, Integer>transform(p -> p * 2).channel(resultChannel).get();
    this.integrationFlowContext.registration(integrationFlow).register();
    for (int i = 0; i < 4; i++) {
        Message<?> receive = resultChannel.receive(10000);
        assertNotNull(receive);
        assertEquals((i + 1) * 2, receive.getPayload());
    }
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) Test(org.junit.Test)

Example 73 with QueueChannel

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

the class TransformerTests method transformWithHeader.

@Test
public void transformWithHeader() {
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message = MessageBuilder.withPayload("Foo").setReplyChannel(replyChannel).build();
    this.pojoTransformFlowInput.send(message);
    Message<?> receive = replyChannel.receive(10000);
    assertNotNull(receive);
    assertEquals("FooBar", receive.getPayload());
    try {
        this.pojoTransformFlowInput.send(message);
        fail("MessageRejectedException expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageRejectedException.class));
        assertThat(e.getMessage(), containsString("IdempotentReceiver"));
        assertThat(e.getMessage(), containsString("rejected duplicate Message"));
    }
    assertNotNull(this.idempotentDiscardChannel.receive(10000));
    assertNotNull(this.adviceChannel.receive(10000));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) IOException(java.io.IOException) MessageRejectedException(org.springframework.integration.MessageRejectedException) Test(org.junit.Test)

Example 74 with QueueChannel

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

the class ExpressionEvaluatingMessageSourceIntegrationTests method test.

@Test
public void test() throws Exception {
    QueueChannel channel = new QueueChannel();
    String payloadExpression = "'test-' + T(org.springframework.integration.endpoint.ExpressionEvaluatingMessageSourceIntegrationTests).next()";
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.afterPropertiesSet();
    Map<String, Expression> headerExpressions = new HashMap<String, Expression>();
    headerExpressions.put("foo", new LiteralExpression("x"));
    headerExpressions.put("bar", new SpelExpressionParser().parseExpression("7 * 6"));
    ExpressionFactoryBean factoryBean = new ExpressionFactoryBean(payloadExpression);
    factoryBean.afterPropertiesSet();
    Expression expression = factoryBean.getObject();
    ExpressionEvaluatingMessageSource<Object> source = new ExpressionEvaluatingMessageSource<Object>(expression, Object.class);
    source.setBeanFactory(mock(BeanFactory.class));
    source.setHeaderExpressions(headerExpressions);
    SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
    adapter.setSource(source);
    adapter.setTaskScheduler(scheduler);
    adapter.setMaxMessagesPerPoll(3);
    adapter.setTrigger(new PeriodicTrigger(60000));
    adapter.setOutputChannel(channel);
    adapter.setErrorHandler(t -> {
        throw new IllegalStateException("unexpected exception in test", t);
    });
    adapter.start();
    List<Message<?>> messages = new ArrayList<Message<?>>();
    for (int i = 0; i < 3; i++) {
        messages.add(channel.receive(1000));
    }
    scheduler.destroy();
    Message<?> message1 = messages.get(0);
    assertEquals("test-1", message1.getPayload());
    assertEquals("x", message1.getHeaders().get("foo"));
    assertEquals(42, message1.getHeaders().get("bar"));
    Message<?> message2 = messages.get(1);
    assertEquals("test-2", message2.getPayload());
    assertEquals("x", message2.getHeaders().get("foo"));
    assertEquals(42, message2.getHeaders().get("bar"));
    Message<?> message3 = messages.get(2);
    assertEquals("test-3", message3.getPayload());
    assertEquals("x", message3.getHeaders().get("foo"));
    assertEquals(42, message3.getHeaders().get("bar"));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) HashMap(java.util.HashMap) LiteralExpression(org.springframework.expression.common.LiteralExpression) ArrayList(java.util.ArrayList) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) ExpressionFactoryBean(org.springframework.integration.config.ExpressionFactoryBean) LiteralExpression(org.springframework.expression.common.LiteralExpression) Expression(org.springframework.expression.Expression) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 75 with QueueChannel

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

the class PollingLifecycleTests method ensurePollerTaskStops.

@Test
public void ensurePollerTaskStops() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    QueueChannel channel = new QueueChannel();
    channel.send(new GenericMessage<String>("foo"));
    MessageHandler handler = Mockito.spy(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            latch.countDown();
        }
    });
    PollingConsumer consumer = new PollingConsumer(channel, handler);
    consumer.setTrigger(new PeriodicTrigger(0));
    consumer.setErrorHandler(errorHandler);
    consumer.setTaskScheduler(taskScheduler);
    consumer.setBeanFactory(mock(BeanFactory.class));
    consumer.afterPropertiesSet();
    consumer.start();
    assertTrue(latch.await(2, TimeUnit.SECONDS));
    Mockito.verify(handler, times(1)).handleMessage(Mockito.any(Message.class));
    consumer.stop();
    for (int i = 0; i < 10; i++) {
        channel.send(new GenericMessage<String>("foo"));
    }
    // give enough time for poller to kick in if it didn't stop properly
    Thread.sleep(2000);
    // we'll still have a natural race condition between call to stop() and poller polling
    // so what we really have to assert is that it doesn't poll for more then once after stop() was called
    Mockito.reset(handler);
    Mockito.verify(handler, atMost(1)).handleMessage(Mockito.any(Message.class));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) MessageHandler(org.springframework.messaging.MessageHandler) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessagingException(org.springframework.messaging.MessagingException) CountDownLatch(java.util.concurrent.CountDownLatch) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) Test(org.junit.Test)

Aggregations

QueueChannel (org.springframework.integration.channel.QueueChannel)709 Test (org.junit.Test)669 GenericMessage (org.springframework.messaging.support.GenericMessage)186 Message (org.springframework.messaging.Message)173 BeanFactory (org.springframework.beans.factory.BeanFactory)162 MessageChannel (org.springframework.messaging.MessageChannel)100 Matchers.containsString (org.hamcrest.Matchers.containsString)66 CountDownLatch (java.util.concurrent.CountDownLatch)59 DirectChannel (org.springframework.integration.channel.DirectChannel)57 ArrayList (java.util.ArrayList)55 PollableChannel (org.springframework.messaging.PollableChannel)55 MessagingException (org.springframework.messaging.MessagingException)53 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)51 AtomicReference (java.util.concurrent.atomic.AtomicReference)47 Socket (java.net.Socket)44 ErrorMessage (org.springframework.messaging.support.ErrorMessage)42 ServerSocket (java.net.ServerSocket)41 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)39 IOException (java.io.IOException)35 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)35