Search in sources :

Example 31 with DirectChannel

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

the class EndpointSpec method obtainInputChannelFromFlow.

/**
 * Try to get a {@link MessageChannel} as an input for the provided {@link IntegrationFlow}
 * or create one and wrap the provided flow to a new one.
 * @param subFlow the {@link IntegrationFlow} to extract input channel.
 * @param evaluateInternalBuilder true if an internal {@link IntegrationFlowDefinition} should be
 * evaluated to an {@link IntegrationFlow} component or left as a builder in the {@link #componentsToRegister}
 * for future use-case. For example the builder is used for router configurations to retain beans
 * registration order for parent-child dependencies.
 * @return the input channel of the flow of create one
 * @since 5.0.4
 */
protected MessageChannel obtainInputChannelFromFlow(IntegrationFlow subFlow, boolean evaluateInternalBuilder) {
    Assert.notNull(subFlow, "'subFlow' must not be null");
    MessageChannel messageChannel = subFlow.getInputChannel();
    if (messageChannel == null) {
        messageChannel = new DirectChannel();
        IntegrationFlowDefinition<?> flowBuilder = IntegrationFlows.from(messageChannel);
        subFlow.configure(flowBuilder);
        this.componentsToRegister.put(evaluateInternalBuilder ? flowBuilder.get() : flowBuilder, null);
    } else {
        this.componentsToRegister.put(subFlow, null);
    }
    return messageChannel;
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) DirectChannel(org.springframework.integration.channel.DirectChannel)

Example 32 with DirectChannel

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

the class IntegrationFlows method from.

/**
 * Populate the {@link MessageChannel} to the new {@link IntegrationFlowBuilder}
 * chain, which becomes as a {@code requestChannel} for the Messaging Gateway(s) built
 * on the provided service interface.
 * <p>A gateway proxy bean for provided service interface is registered under a name of
 * the provided {@code beanName} if not null, or from the
 * {@link org.springframework.integration.annotation.MessagingGateway#name()} if present
 * or as a fallback to the {@link IntegrationFlow} bean name plus {@code .gateway} suffix.
 * @param serviceInterface the service interface class with an optional
 * {@link org.springframework.integration.annotation.MessagingGateway} annotation.
 * @param beanName the bean name to be used for registering bean for the gateway proxy
 * @return new {@link IntegrationFlowBuilder}.
 */
public static IntegrationFlowBuilder from(Class<?> serviceInterface, String beanName) {
    final DirectChannel gatewayRequestChannel = new DirectChannel();
    GatewayProxyFactoryBean gatewayProxyFactoryBean = new AnnotationGatewayProxyFactoryBean(serviceInterface);
    gatewayProxyFactoryBean.setDefaultRequestChannel(gatewayRequestChannel);
    gatewayProxyFactoryBean.setBeanName(beanName);
    return from(gatewayRequestChannel).addComponent(gatewayProxyFactoryBean);
}
Also used : GatewayProxyFactoryBean(org.springframework.integration.gateway.GatewayProxyFactoryBean) AnnotationGatewayProxyFactoryBean(org.springframework.integration.gateway.AnnotationGatewayProxyFactoryBean) DirectChannel(org.springframework.integration.channel.DirectChannel) AnnotationGatewayProxyFactoryBean(org.springframework.integration.gateway.AnnotationGatewayProxyFactoryBean)

Example 33 with DirectChannel

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

the class IntegrationFlows method from.

private static IntegrationFlowBuilder from(MessagingGatewaySupport inboundGateway, IntegrationFlowBuilder integrationFlowBuilder) {
    MessageChannel outputChannel = inboundGateway.getRequestChannel();
    if (outputChannel == null) {
        outputChannel = new DirectChannel();
        inboundGateway.setRequestChannel(outputChannel);
    }
    if (integrationFlowBuilder == null) {
        integrationFlowBuilder = from(outputChannel);
    } else {
        integrationFlowBuilder.channel(outputChannel);
    }
    return integrationFlowBuilder.addComponent(inboundGateway);
}
Also used : FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) MessageChannel(org.springframework.messaging.MessageChannel) DirectChannel(org.springframework.integration.channel.DirectChannel)

Example 34 with DirectChannel

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

the class ReactiveStreamsConsumerTests method testReactiveStreamsConsumerDirectChannel.

@Test
@SuppressWarnings("unchecked")
public void testReactiveStreamsConsumerDirectChannel() throws InterruptedException {
    DirectChannel testChannel = new DirectChannel();
    Subscriber<Message<?>> testSubscriber = (Subscriber<Message<?>>) Mockito.mock(Subscriber.class);
    BlockingQueue<Message<?>> messages = new LinkedBlockingQueue<>();
    willAnswer(i -> {
        messages.put(i.getArgument(0));
        return null;
    }).given(testSubscriber).onNext(any(Message.class));
    ReactiveStreamsConsumer reactiveConsumer = new ReactiveStreamsConsumer(testChannel, testSubscriber);
    reactiveConsumer.setBeanFactory(mock(BeanFactory.class));
    reactiveConsumer.afterPropertiesSet();
    reactiveConsumer.start();
    Message<?> testMessage = new GenericMessage<>("test");
    testChannel.send(testMessage);
    ArgumentCaptor<Subscription> subscriptionArgumentCaptor = ArgumentCaptor.forClass(Subscription.class);
    verify(testSubscriber).onSubscribe(subscriptionArgumentCaptor.capture());
    Subscription subscription = subscriptionArgumentCaptor.getValue();
    subscription.request(1);
    Message<?> message = messages.poll(10, TimeUnit.SECONDS);
    assertSame(testMessage, message);
    reactiveConsumer.stop();
    try {
        testChannel.send(testMessage);
        fail("MessageDeliveryException");
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
    }
    reactiveConsumer.start();
    subscription.request(1);
    testMessage = new GenericMessage<>("test2");
    testChannel.send(testMessage);
    message = messages.poll(10, TimeUnit.SECONDS);
    assertSame(testMessage, message);
    verify(testSubscriber, never()).onError(any(Throwable.class));
    verify(testSubscriber, never()).onComplete();
    assertTrue(messages.isEmpty());
}
Also used : ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) DirectChannel(org.springframework.integration.channel.DirectChannel) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) GenericMessage(org.springframework.messaging.support.GenericMessage) Subscriber(org.reactivestreams.Subscriber) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 35 with DirectChannel

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

the class AggregatorTests method testAggPerfDefaultPartial.

@Test
public void testAggPerfDefaultPartial() throws InterruptedException, ExecutionException, TimeoutException {
    AggregatingMessageHandler handler = new AggregatingMessageHandler(new DefaultAggregatingMessageGroupProcessor());
    handler.setCorrelationStrategy(message -> "foo");
    handler.setReleasePartialSequences(true);
    DirectChannel outputChannel = new DirectChannel();
    handler.setOutputChannel(outputChannel);
    final CompletableFuture<Collection<?>> resultFuture = new CompletableFuture<>();
    outputChannel.subscribe(message -> {
        Collection<?> payload = (Collection<?>) message.getPayload();
        logger.warn("Received " + payload.size());
        resultFuture.complete(payload);
    });
    SimpleMessageStore store = new SimpleMessageStore();
    SimpleMessageGroupFactory messageGroupFactory = new SimpleMessageGroupFactory(SimpleMessageGroupFactory.GroupType.BLOCKING_QUEUE);
    store.setMessageGroupFactory(messageGroupFactory);
    handler.setMessageStore(store);
    StopWatch stopwatch = new StopWatch();
    stopwatch.start();
    for (int i = 0; i < 120000; i++) {
        if (i % 10000 == 0) {
            stopwatch.stop();
            logger.warn("Sent " + i + " in " + stopwatch.getTotalTimeSeconds() + " (10k in " + stopwatch.getLastTaskTimeMillis() + "ms)");
            stopwatch.start();
        }
        handler.handleMessage(MessageBuilder.withPayload("foo").setSequenceSize(120000).setSequenceNumber(i + 1).build());
    }
    stopwatch.stop();
    logger.warn("Sent " + 120000 + " in " + stopwatch.getTotalTimeSeconds() + " (10k in " + stopwatch.getLastTaskTimeMillis() + "ms)");
    Collection<?> result = resultFuture.get(10, TimeUnit.SECONDS);
    assertNotNull(result);
    assertEquals(120000, result.size());
    // actually < 2.0, was many minutes
    assertThat(stopwatch.getTotalTimeSeconds(), lessThan(60.0));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) SimpleMessageStore(org.springframework.integration.store.SimpleMessageStore) SimpleMessageGroupFactory(org.springframework.integration.store.SimpleMessageGroupFactory) DirectChannel(org.springframework.integration.channel.DirectChannel) Collection(java.util.Collection) StopWatch(org.springframework.util.StopWatch) 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