Search in sources :

Example 26 with DirectChannel

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

the class MessagingAnnotationPostProcessorTests method testMessageEndpointAnnotationInheritedWithProxy.

@Test
public void testMessageEndpointAnnotationInheritedWithProxy() {
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    DirectChannel inputChannel = new DirectChannel();
    QueueChannel outputChannel = new QueueChannel();
    context.registerChannel("inputChannel", inputChannel);
    context.registerChannel("outputChannel", outputChannel);
    MessagingAnnotationPostProcessor postProcessor = new MessagingAnnotationPostProcessor();
    postProcessor.setBeanFactory(context.getBeanFactory());
    postProcessor.afterPropertiesSet();
    ProxyFactory proxyFactory = new ProxyFactory(new SimpleAnnotatedEndpointSubclass());
    Object proxy = proxyFactory.getProxy();
    postProcessor.postProcessAfterInitialization(proxy, "proxy");
    context.refresh();
    inputChannel.send(new GenericMessage<String>("world"));
    Message<?> message = outputChannel.receive(1000);
    assertEquals("hello world", message.getPayload());
    context.stop();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) ProxyFactory(org.springframework.aop.framework.ProxyFactory) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) Test(org.junit.Test)

Example 27 with DirectChannel

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

the class InnerDefinitionHandlerAwareEndpointParserTests method testRouterDefinitionSuccess.

private void testRouterDefinitionSuccess(String configProperty) {
    ApplicationContext ac = this.bootStrap(configProperty);
    EventDrivenConsumer splitter = (EventDrivenConsumer) ac.getBean("testRouter");
    Assert.assertNotNull(splitter);
    MessageBuilder<String> inChannelMessageBuilder = MessageBuilder.withPayload("1");
    Message<String> inMessage = inChannelMessageBuilder.build();
    DirectChannel inChannel = (DirectChannel) ac.getBean("inChannel");
    inChannel.send(inMessage);
    PollableChannel channel1 = (PollableChannel) ac.getBean("channel1");
    Assert.assertTrue(channel1.receive().getPayload().equals("1"));
    inChannelMessageBuilder = MessageBuilder.withPayload("2");
    inMessage = inChannelMessageBuilder.build();
    inChannel.send(inMessage);
    PollableChannel channel2 = (PollableChannel) ac.getBean("channel2");
    Assert.assertTrue(channel2.receive().getPayload().equals("2"));
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) DirectChannel(org.springframework.integration.channel.DirectChannel) PollableChannel(org.springframework.messaging.PollableChannel)

Example 28 with DirectChannel

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

the class ControlBusTests method testControlHeaderChannelReaper.

@Test
public void testControlHeaderChannelReaper() throws InterruptedException {
    MessagingTemplate messagingTemplate = new MessagingTemplate();
    messagingTemplate.convertAndSend(input, "@integrationHeaderChannelRegistry.size()");
    Message<?> result = this.output.receive(0);
    assertNotNull(result);
    // No channels in the registry
    assertEquals(0, result.getPayload());
    this.registry.channelToChannelName(new DirectChannel());
    // Sleep a bit to be sure that we aren't reaped by registry TTL as 60000
    Thread.sleep(100);
    messagingTemplate.convertAndSend(input, "@integrationHeaderChannelRegistry.size()");
    result = this.output.receive(0);
    assertNotNull(result);
    assertEquals(1, result.getPayload());
    // Some DirectFieldAccessor magic to modify 'expireAt' to the past to avoid timing issues on high-loaded build
    Object messageChannelWrapper = TestUtils.getPropertyValue(this.registry, "channels", Map.class).values().iterator().next();
    DirectFieldAccessor dfa = new DirectFieldAccessor(messageChannelWrapper);
    dfa.setPropertyValue("expireAt", System.currentTimeMillis() - 60000);
    messagingTemplate.convertAndSend(input, "@integrationHeaderChannelRegistry.runReaper()");
    messagingTemplate.convertAndSend(input, "@integrationHeaderChannelRegistry.size()");
    result = this.output.receive(0);
    assertNotNull(result);
    assertEquals(0, result.getPayload());
}
Also used : MessagingTemplate(org.springframework.integration.core.MessagingTemplate) DirectChannel(org.springframework.integration.channel.DirectChannel) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Map(java.util.Map) Test(org.junit.Test)

Example 29 with DirectChannel

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

the class IntegrationFlowDefinition method wireTap.

/**
 * Populate the {@code Wire Tap} EI Pattern specific
 * {@link org.springframework.messaging.support.ChannelInterceptor} implementation
 * to the current {@link #currentMessageChannel}.
 * <p> It is useful when an implicit {@link MessageChannel} is used between endpoints:
 * <pre class="code">
 * {@code
 *  .transform("payload")
 *  .wireTap(new WireTap(tapChannel().selector(m -> m.getPayload().equals("foo")))
 *  .channel("foo")
 * }
 * </pre>
 * This method can be used after any {@link #channel} for explicit {@link MessageChannel},
 * but with the caution do not impact existing {@link org.springframework.messaging.support.ChannelInterceptor}s.
 * @param wireTapSpec the {@link WireTapSpec} to use.
 * <p> When this EIP-method is used in the end of flow, it appends {@code nullChannel} to terminate flow properly,
 * Otherwise {@code Dispatcher has no subscribers} exception is thrown for implicit {@link DirectChannel}.
 * @return the current {@link IntegrationFlowDefinition}.
 */
public B wireTap(WireTapSpec wireTapSpec) {
    WireTap interceptor = wireTapSpec.get();
    if (this.currentMessageChannel == null || !(this.currentMessageChannel instanceof ChannelInterceptorAware)) {
        this.implicitChannel = true;
        channel(new DirectChannel());
    }
    addComponent(wireTapSpec);
    ((ChannelInterceptorAware) this.currentMessageChannel).addInterceptor(interceptor);
    return _this();
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) WireTap(org.springframework.integration.channel.interceptor.WireTap) ChannelInterceptorAware(org.springframework.integration.channel.ChannelInterceptorAware)

Example 30 with DirectChannel

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

the class IntegrationFlowDefinition method register.

@SuppressWarnings("unchecked")
private <S extends ConsumerEndpointSpec<S, ? extends MessageHandler>> B register(S endpointSpec, Consumer<S> endpointConfigurer) {
    if (endpointConfigurer != null) {
        endpointConfigurer.accept(endpointSpec);
    }
    MessageChannel inputChannel = this.currentMessageChannel;
    this.currentMessageChannel = null;
    if (inputChannel == null) {
        inputChannel = new DirectChannel();
        this.registerOutputChannelIfCan(inputChannel);
    }
    Tuple2<ConsumerEndpointFactoryBean, ? extends MessageHandler> factoryBeanTuple2 = endpointSpec.get();
    addComponents(endpointSpec.getComponentsToRegister());
    if (inputChannel instanceof MessageChannelReference) {
        factoryBeanTuple2.getT1().setInputChannelName(((MessageChannelReference) inputChannel).getName());
    } else {
        if (inputChannel instanceof FixedSubscriberChannelPrototype) {
            String beanName = ((FixedSubscriberChannelPrototype) inputChannel).getName();
            inputChannel = new FixedSubscriberChannel(factoryBeanTuple2.getT2());
            if (beanName != null) {
                ((FixedSubscriberChannel) inputChannel).setBeanName(beanName);
            }
            registerOutputChannelIfCan(inputChannel);
        }
        factoryBeanTuple2.getT1().setInputChannel(inputChannel);
    }
    return addComponent(endpointSpec).currentComponent(factoryBeanTuple2.getT2());
}
Also used : MessageChannelReference(org.springframework.integration.dsl.support.MessageChannelReference) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) MessageChannel(org.springframework.messaging.MessageChannel) ConsumerEndpointFactoryBean(org.springframework.integration.config.ConsumerEndpointFactoryBean) DirectChannel(org.springframework.integration.channel.DirectChannel) FixedSubscriberChannelPrototype(org.springframework.integration.dsl.support.FixedSubscriberChannelPrototype) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel)

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