Search in sources :

Example 1 with ReactiveStreamsConsumer

use of org.springframework.integration.endpoint.ReactiveStreamsConsumer in project spring-integration by spring-projects.

the class MessagingGatewaySupport method registerReplyMessageCorrelatorIfNecessary.

protected void registerReplyMessageCorrelatorIfNecessary() {
    MessageChannel replyChannel = getReplyChannel();
    if (replyChannel != null && this.replyMessageCorrelator == null) {
        boolean shouldStartCorrelator;
        synchronized (this.replyMessageCorrelatorMonitor) {
            if (this.replyMessageCorrelator != null) {
                return;
            }
            AbstractEndpoint correlator;
            BridgeHandler handler = new BridgeHandler();
            if (getBeanFactory() != null) {
                handler.setBeanFactory(getBeanFactory());
            }
            handler.afterPropertiesSet();
            if (replyChannel instanceof SubscribableChannel) {
                correlator = new EventDrivenConsumer((SubscribableChannel) replyChannel, handler);
            } else if (replyChannel instanceof PollableChannel) {
                PollingConsumer endpoint = new PollingConsumer((PollableChannel) replyChannel, handler);
                endpoint.setBeanFactory(getBeanFactory());
                endpoint.setReceiveTimeout(this.replyTimeout);
                endpoint.afterPropertiesSet();
                correlator = endpoint;
            } else if (replyChannel instanceof ReactiveStreamsSubscribableChannel) {
                ReactiveStreamsConsumer endpoint = new ReactiveStreamsConsumer(replyChannel, (Subscriber<Message<?>>) handler);
                endpoint.afterPropertiesSet();
                correlator = endpoint;
            } else {
                throw new MessagingException("Unsupported 'replyChannel' type [" + replyChannel.getClass() + "]." + "SubscribableChannel or PollableChannel type are supported.");
            }
            this.replyMessageCorrelator = correlator;
            shouldStartCorrelator = true;
        }
        if (shouldStartCorrelator && isRunning()) {
            if (isRunning()) {
                this.replyMessageCorrelator.start();
            }
        }
    }
}
Also used : AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) MessageChannel(org.springframework.messaging.MessageChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) BridgeHandler(org.springframework.integration.handler.BridgeHandler) MessagingException(org.springframework.messaging.MessagingException) PollableChannel(org.springframework.messaging.PollableChannel) ReactiveStreamsSubscribableChannel(org.springframework.integration.channel.ReactiveStreamsSubscribableChannel) SubscribableChannel(org.springframework.messaging.SubscribableChannel) ReactiveStreamsSubscribableChannel(org.springframework.integration.channel.ReactiveStreamsSubscribableChannel)

Example 2 with ReactiveStreamsConsumer

use of org.springframework.integration.endpoint.ReactiveStreamsConsumer in project spring-integration by spring-projects.

the class AbstractMethodAnnotationPostProcessor method doCreateEndpoint.

protected AbstractEndpoint doCreateEndpoint(MessageHandler handler, MessageChannel inputChannel, List<Annotation> annotations) {
    AbstractEndpoint endpoint;
    if (inputChannel instanceof PollableChannel) {
        PollingConsumer pollingConsumer = new PollingConsumer((PollableChannel) inputChannel, handler);
        configurePollingEndpoint(pollingConsumer, annotations);
        endpoint = pollingConsumer;
    } else {
        Poller[] pollers = MessagingAnnotationUtils.resolveAttribute(annotations, "poller", Poller[].class);
        Assert.state(ObjectUtils.isEmpty(pollers), "A '@Poller' should not be specified for Annotation-based " + "endpoint, since '" + inputChannel + "' is a SubscribableChannel (not pollable).");
        if (inputChannel instanceof Publisher) {
            endpoint = new ReactiveStreamsConsumer(inputChannel, handler);
        } else {
            endpoint = new EventDrivenConsumer((SubscribableChannel) inputChannel, handler);
        }
    }
    return endpoint;
}
Also used : AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) PollableChannel(org.springframework.messaging.PollableChannel) Publisher(org.reactivestreams.Publisher) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Poller(org.springframework.integration.annotation.Poller)

Example 3 with ReactiveStreamsConsumer

use of org.springframework.integration.endpoint.ReactiveStreamsConsumer in project spring-integration by spring-projects.

the class ConsumerEndpointFactoryBean method initializeEndpoint.

@SuppressWarnings("unchecked")
private void initializeEndpoint() throws Exception {
    synchronized (this.initializationMonitor) {
        if (this.initialized) {
            return;
        }
        MessageChannel channel = null;
        if (StringUtils.hasText(this.inputChannelName)) {
            channel = this.channelResolver.resolveDestination(this.inputChannelName);
        }
        if (this.inputChannel != null) {
            channel = this.inputChannel;
        }
        Assert.state(channel != null, "one of inputChannelName or inputChannel is required");
        if (channel instanceof SubscribableChannel) {
            Assert.isNull(this.pollerMetadata, "A poller should not be specified for endpoint '" + this.beanName + "', since '" + channel + "' is a SubscribableChannel (not pollable).");
            this.endpoint = new EventDrivenConsumer((SubscribableChannel) channel, this.handler);
            if (logger.isWarnEnabled() && Boolean.FALSE.equals(this.autoStartup) && channel instanceof FixedSubscriberChannel) {
                logger.warn("'autoStartup=\"false\"' has no effect when using a FixedSubscriberChannel");
            }
        } else if (channel instanceof PollableChannel) {
            PollingConsumer pollingConsumer = new PollingConsumer((PollableChannel) channel, this.handler);
            if (this.pollerMetadata == null) {
                this.pollerMetadata = PollerMetadata.getDefaultPollerMetadata(this.beanFactory);
                Assert.notNull(this.pollerMetadata, "No poller has been defined for endpoint '" + this.beanName + "', and no default poller is available within the context.");
            }
            pollingConsumer.setTaskExecutor(this.pollerMetadata.getTaskExecutor());
            pollingConsumer.setTrigger(this.pollerMetadata.getTrigger());
            pollingConsumer.setAdviceChain(this.pollerMetadata.getAdviceChain());
            pollingConsumer.setMaxMessagesPerPoll(this.pollerMetadata.getMaxMessagesPerPoll());
            pollingConsumer.setErrorHandler(this.pollerMetadata.getErrorHandler());
            pollingConsumer.setReceiveTimeout(this.pollerMetadata.getReceiveTimeout());
            pollingConsumer.setTransactionSynchronizationFactory(this.pollerMetadata.getTransactionSynchronizationFactory());
            pollingConsumer.setBeanClassLoader(this.beanClassLoader);
            pollingConsumer.setBeanFactory(this.beanFactory);
            this.endpoint = pollingConsumer;
        } else {
            this.endpoint = new ReactiveStreamsConsumer(channel, this.handler);
        }
        this.endpoint.setBeanName(this.beanName);
        this.endpoint.setBeanFactory(this.beanFactory);
        if (this.autoStartup != null) {
            this.endpoint.setAutoStartup(this.autoStartup);
        }
        int phase = this.phase;
        if (!this.isPhaseSet && this.endpoint instanceof PollingConsumer) {
            phase = Integer.MAX_VALUE / 2;
        }
        this.endpoint.setPhase(phase);
        this.endpoint.setRole(this.role);
        if (this.taskScheduler != null) {
            this.endpoint.setTaskScheduler(this.taskScheduler);
        }
        this.endpoint.afterPropertiesSet();
        this.initialized = true;
    }
}
Also used : EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) MessageChannel(org.springframework.messaging.MessageChannel) PollableChannel(org.springframework.messaging.PollableChannel) SubscribableChannel(org.springframework.messaging.SubscribableChannel) AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel)

Example 4 with ReactiveStreamsConsumer

use of org.springframework.integration.endpoint.ReactiveStreamsConsumer 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 5 with ReactiveStreamsConsumer

use of org.springframework.integration.endpoint.ReactiveStreamsConsumer in project spring-integration by spring-projects.

the class ReactiveStreamsConsumerTests method testReactiveStreamsConsumerPollableChannel.

@Test
@SuppressWarnings("unchecked")
public void testReactiveStreamsConsumerPollableChannel() throws InterruptedException {
    QueueChannel testChannel = new QueueChannel();
    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();
    testChannel.send(testMessage);
    reactiveConsumer.start();
    verify(testSubscriber, times(2)).onSubscribe(subscriptionArgumentCaptor.capture());
    subscription = subscriptionArgumentCaptor.getValue();
    subscription.request(2);
    Message<?> testMessage2 = new GenericMessage<>("test2");
    testChannel.send(testMessage2);
    message = messages.poll(10, TimeUnit.SECONDS);
    assertSame(testMessage, message);
    message = messages.poll(10, TimeUnit.SECONDS);
    assertSame(testMessage2, message);
    verify(testSubscriber, never()).onError(any(Throwable.class));
    verify(testSubscriber, never()).onComplete();
    assertTrue(messages.isEmpty());
}
Also used : ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) 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)

Aggregations

ReactiveStreamsConsumer (org.springframework.integration.endpoint.ReactiveStreamsConsumer)6 Message (org.springframework.messaging.Message)4 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 Test (org.junit.Test)3 Subscriber (org.reactivestreams.Subscriber)3 Subscription (org.reactivestreams.Subscription)3 BeanFactory (org.springframework.beans.factory.BeanFactory)3 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)3 AbstractEndpoint (org.springframework.integration.endpoint.AbstractEndpoint)3 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)3 PollingConsumer (org.springframework.integration.endpoint.PollingConsumer)3 PollableChannel (org.springframework.messaging.PollableChannel)3 SubscribableChannel (org.springframework.messaging.SubscribableChannel)3 GenericMessage (org.springframework.messaging.support.GenericMessage)3 DirectChannel (org.springframework.integration.channel.DirectChannel)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 MessageChannel (org.springframework.messaging.MessageChannel)2 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1