public ReactiveStreamsConsumer(MessageChannel inputChannel, final Subscriber<Message<?>> subscriber) { this.inputChannel = inputChannel; Assert.notNull(inputChannel, "'inputChannel' must not be null"); Assert.notNull(subscriber, "'subscriber' must not be null"); if (inputChannel instanceof NullChannel && logger.isWarnEnabled()) { logger.warn("The consuming from the NullChannel does not have any effects: " + "it doesn't forward messages sent to it. A NullChannel is the end of the flow."); } this.publisher = MessageChannelReactiveUtils.toPublisher(inputChannel); this.subscriber = subscriber; this.lifecycleDelegate = subscriber instanceof Lifecycle ? (Lifecycle) subscriber : null; if (subscriber instanceof MessageHandlerSubscriber) { this.handler = ((MessageHandlerSubscriber) subscriber).messageHandler; } else if (subscriber instanceof MessageHandler) { this.handler = (MessageHandler) subscriber; } else { this.handler = this.subscriber::onNext; } }
@SuppressWarnings("unchecked") public static <T> Publisher<Message<T>> toPublisher(MessageChannel messageChannel) { if (messageChannel instanceof Publisher) { return (Publisher<Message<T>>) messageChannel; } else if (messageChannel instanceof SubscribableChannel) { return adaptSubscribableChannelToPublisher((SubscribableChannel) messageChannel); } else if (messageChannel instanceof PollableChannel) { return adaptPollableChannelToPublisher((PollableChannel) messageChannel); } else { throw new IllegalArgumentException("The 'messageChannel' must be an instance of Publisher, " + "SubscribableChannel or PollableChannel, not: " + messageChannel); } }
/** * Represent an Integration Flow as a Reactive Streams {@link Publisher} bean. * @param <T> the expected {@code payload} type * @return the Reactive Streams {@link Publisher} */ @SuppressWarnings("unchecked") public <T> Publisher<Message<T>> toReactivePublisher() { MessageChannel channelForPublisher = this.currentMessageChannel; Publisher<Message<T>> publisher; if (channelForPublisher instanceof Publisher) { publisher = (Publisher<Message<T>>) channelForPublisher; } else { if (channelForPublisher != null && this.integrationComponents.size() > 1 && !(channelForPublisher instanceof MessageChannelReference) && !(channelForPublisher instanceof FixedSubscriberChannelPrototype)) { publisher = MessageChannelReactiveUtils.toPublisher(channelForPublisher); } else { MessageChannel reactiveChannel = new FluxMessageChannel(); publisher = (Publisher<Message<T>>) reactiveChannel; channel(reactiveChannel); } } this.implicitChannel = false; get(); return new PublisherIntegrationFlow<>(this.integrationComponents, publisher); }
@SuppressWarnings("unchecked") public static <T> Publisher<Message<T>> toPublisher(MessageChannel messageChannel) { if (messageChannel instanceof Publisher) { return (Publisher<Message<T>>) messageChannel; } else if (messageChannel instanceof SubscribableChannel) { return adaptSubscribableChannelToPublisher((SubscribableChannel) messageChannel); } else if (messageChannel instanceof PollableChannel) { return adaptPollableChannelToPublisher((PollableChannel) messageChannel); } else { throw new IllegalArgumentException("The 'messageChannel' must be an instance of Publisher, " + "SubscribableChannel or PollableChannel, not: " + messageChannel); } }
@Test public void testMessageChannelReactiveAdaptation() throws InterruptedException { CountDownLatch done = new CountDownLatch(2); List<String> results = new ArrayList<>(); Flux.from(MessageChannelReactiveUtils.<String>toPublisher(this.queueChannel)) .map(Message::getPayload) .map(String::toUpperCase) .doOnNext(results::add) .subscribe(v -> done.countDown()); this.queueChannel.send(new GenericMessage<>("foo")); this.queueChannel.send(new GenericMessage<>("bar")); assertTrue(done.await(10, TimeUnit.SECONDS)); assertThat(results, contains("FOO", "BAR")); }
public ReactiveStreamsConsumer(MessageChannel inputChannel, final Subscriber<Message<?>> subscriber) { this.inputChannel = inputChannel; Assert.notNull(inputChannel, "'inputChannel' must not be null"); Assert.notNull(subscriber, "'subscriber' must not be null"); if (inputChannel instanceof NullChannel && logger.isWarnEnabled()) { logger.warn("The consuming from the NullChannel does not have any effects: " + "it doesn't forward messages sent to it. A NullChannel is the end of the flow."); } this.publisher = MessageChannelReactiveUtils.toPublisher(inputChannel); this.subscriber = subscriber; this.lifecycleDelegate = subscriber instanceof Lifecycle ? (Lifecycle) subscriber : null; if (subscriber instanceof MessageHandlerSubscriber) { this.handler = ((MessageHandlerSubscriber) subscriber).messageHandler; } else if (subscriber instanceof MessageHandler) { this.handler = (MessageHandler) subscriber; } else { this.handler = this.subscriber::onNext; } }
/** * Represent an Integration Flow as a Reactive Streams {@link Publisher} bean. * @param <T> the expected {@code payload} type * @return the Reactive Streams {@link Publisher} */ @SuppressWarnings("unchecked") public <T> Publisher<Message<T>> toReactivePublisher() { MessageChannel channelForPublisher = this.currentMessageChannel; Publisher<Message<T>> publisher; if (channelForPublisher instanceof Publisher) { publisher = (Publisher<Message<T>>) channelForPublisher; } else { if (channelForPublisher != null && this.integrationComponents.size() > 1 && !(channelForPublisher instanceof MessageChannelReference) && !(channelForPublisher instanceof FixedSubscriberChannelPrototype)) { publisher = MessageChannelReactiveUtils.toPublisher(channelForPublisher); } else { MessageChannel reactiveChannel = new FluxMessageChannel(); publisher = (Publisher<Message<T>>) reactiveChannel; channel(reactiveChannel); } } this.implicitChannel = false; get(); return new PublisherIntegrationFlow<>(this.integrationComponents, publisher); }
private SubscribableChannel postProcessInboundChannelForFunction(MessageChannel inputChannel, ConsumerProperties consumerProperties) { if (this.integrationFlowFunctionSupport != null && (this.integrationFlowFunctionSupport.containsFunction(Consumer.class) || this.integrationFlowFunctionSupport.containsFunction(Function.class))) { DirectChannel actualInputChannel = new DirectChannel(); if (inputChannel instanceof AbstractMessageChannel) { moveChannelInterceptors((AbstractMessageChannel) inputChannel, actualInputChannel); } this.integrationFlowFunctionSupport.andThenFunction(MessageChannelReactiveUtils.toPublisher(actualInputChannel), inputChannel, this.streamFunctionProperties); return actualInputChannel; } return (SubscribableChannel) inputChannel; }
private SubscribableChannel postProcessOutboundChannelForFunction(MessageChannel outputChannel, ProducerProperties producerProperties) { if (this.integrationFlowFunctionSupport != null) { Publisher<?> publisher = MessageChannelReactiveUtils.toPublisher(outputChannel); // If the app has an explicit Supplier bean defined, make that as the publisher if (this.integrationFlowFunctionSupport.containsFunction(Supplier.class)) { IntegrationFlowBuilder integrationFlowBuilder = IntegrationFlows.from(outputChannel).bridge(); publisher = integrationFlowBuilder.toReactivePublisher(); } if (this.integrationFlowFunctionSupport.containsFunction(Function.class, this.streamFunctionProperties.getDefinition())) { DirectChannel actualOutputChannel = new DirectChannel(); if (outputChannel instanceof AbstractMessageChannel) { moveChannelInterceptors((AbstractMessageChannel) outputChannel, actualOutputChannel); } this.integrationFlowFunctionSupport.andThenFunction(publisher, actualOutputChannel, this.streamFunctionProperties); return actualOutputChannel; } } return (SubscribableChannel) outputChannel; }