Add ReactiveMessageSourceProducer (#3254)
* Add `ReactiveMessageSourceProducer` The `ReactiveMessageSourceProducer` wraps a provided `MessageSource` into a `Flux` for subscription in the `subscribeToPublisher(Publisher<? extends Message<?>>)` to make a source polling feature fully based on a reactive, on demand solution * Introduce a `IntegrationReactiveUtils` replacing existing `MessageChannelReactiveUtils` with more functionality * Replace a deprecated `MessageChannelReactiveUtils` with a new `IntegrationReactiveUtils` * Test and document the feature * * Fix Docs typos * * Remove unused imports from `MessageChannelReactiveUtils` * * Fix JavaDoc copy/paste artifact
This commit is contained in:
@@ -23,6 +23,7 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.integration.util.IntegrationReactiveUtils;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
import reactor.core.Disposable;
|
||||
@@ -45,7 +46,7 @@ class MessageChannelReactiveUtilsTests {
|
||||
try {
|
||||
DirectChannel channel = new DirectChannel();
|
||||
int initialRequest = 10;
|
||||
StepVerifier.create(MessageChannelReactiveUtils.toPublisher(channel), initialRequest)
|
||||
StepVerifier.create(IntegrationReactiveUtils.messageChannelToFlux(channel), initialRequest)
|
||||
.expectSubscription()
|
||||
.then(() -> {
|
||||
compositeDisposable.add(
|
||||
@@ -77,7 +78,7 @@ class MessageChannelReactiveUtilsTests {
|
||||
AtomicInteger sendCount = new AtomicInteger();
|
||||
try {
|
||||
int initialRequest = 10;
|
||||
StepVerifier.create(MessageChannelReactiveUtils.toPublisher(channel), initialRequest)
|
||||
StepVerifier.create(IntegrationReactiveUtils.messageChannelToFlux(channel), initialRequest)
|
||||
.expectSubscription()
|
||||
.then(() ->
|
||||
compositeDisposable.add(
|
||||
|
||||
@@ -33,13 +33,13 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.integration.annotation.BridgeFrom;
|
||||
import org.springframework.integration.annotation.ServiceActivator;
|
||||
import org.springframework.integration.channel.FluxMessageChannel;
|
||||
import org.springframework.integration.channel.MessageChannelReactiveUtils;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.config.EnableIntegration;
|
||||
import org.springframework.integration.dsl.IntegrationFlow;
|
||||
import org.springframework.integration.dsl.MessageChannels;
|
||||
import org.springframework.integration.dsl.context.IntegrationFlowContext;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.util.IntegrationReactiveUtils;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
@@ -104,7 +104,7 @@ public class FluxMessageChannelTests {
|
||||
List<String> results = new ArrayList<>();
|
||||
|
||||
Disposable disposable =
|
||||
Flux.from(MessageChannelReactiveUtils.<String>toPublisher(this.queueChannel))
|
||||
IntegrationReactiveUtils.<String>messageChannelToFlux(this.queueChannel)
|
||||
.map(Message::getPayload)
|
||||
.map(String::toUpperCase)
|
||||
.doOnNext(results::add)
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.endpoint;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.integration.IntegrationMessageHeaderAccessor;
|
||||
import org.springframework.integration.acks.AcknowledgmentCallback;
|
||||
import org.springframework.integration.channel.FluxMessageChannel;
|
||||
import org.springframework.integration.core.MessageSource;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
/**
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 5.3
|
||||
*/
|
||||
public class ReactiveMessageSourceProducerTests {
|
||||
|
||||
@Test
|
||||
void testReactiveMessageSourceProducing() {
|
||||
LinkedBlockingQueue<Integer> queue =
|
||||
IntStream.range(0, 10)
|
||||
.boxed()
|
||||
.collect(Collectors.toCollection(LinkedBlockingQueue::new));
|
||||
|
||||
AtomicBoolean ackState = new AtomicBoolean();
|
||||
|
||||
MessageSource<Integer> messageSource =
|
||||
() -> {
|
||||
Integer integer = queue.poll();
|
||||
if (integer == null) {
|
||||
try {
|
||||
Thread.sleep(200);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
integer = 100;
|
||||
}
|
||||
return MessageBuilder.withPayload(integer)
|
||||
.setHeader(IntegrationMessageHeaderAccessor.ACKNOWLEDGMENT_CALLBACK,
|
||||
(AcknowledgmentCallback) status -> ackState.set(true))
|
||||
.build();
|
||||
};
|
||||
|
||||
FluxMessageChannel outputChannel = new FluxMessageChannel();
|
||||
|
||||
ReactiveMessageSourceProducer reactiveMessageSourceProducer = new ReactiveMessageSourceProducer(messageSource);
|
||||
reactiveMessageSourceProducer.setDelayWhenEmpty(Duration.ofMillis(10));
|
||||
reactiveMessageSourceProducer.setOutputChannel(outputChannel);
|
||||
reactiveMessageSourceProducer.setBeanFactory(mock(BeanFactory.class));
|
||||
reactiveMessageSourceProducer.afterPropertiesSet();
|
||||
|
||||
StepVerifier stepVerifier =
|
||||
StepVerifier.create(
|
||||
Flux.from(outputChannel)
|
||||
.map(Message::getPayload)
|
||||
.cast(Integer.class))
|
||||
.expectNextSequence(
|
||||
IntStream.range(0, 10)
|
||||
.boxed()
|
||||
.collect(Collectors.toList()))
|
||||
.expectNoEvent(Duration.ofMillis(100))
|
||||
.expectNext(100)
|
||||
.thenCancel()
|
||||
.verifyLater();
|
||||
|
||||
reactiveMessageSourceProducer.start();
|
||||
|
||||
stepVerifier.verify();
|
||||
|
||||
reactiveMessageSourceProducer.stop();
|
||||
|
||||
assertThat(ackState.get()).isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user