GH-2788: Add MongoDbChangeStreamMessageProducer

Fixes https://github.com/spring-projects/spring-integration/issues/2788

* Introduce a `MessageProducerSupport.subscribeToPublisher(Publisher<Message<?>>)`
for components which produces `Flux` for data from their source
* Such a component is auto-stopped when subscription to that `Publisher` is canceled
* Implement a `MongoDbChangeStreamMessageProducer` based on the reactive support for
in Spring Data MongoDb
* Implement a Java DSL for `MongoDbChangeStreamMessageProducer`
* Disable a test for change stream since it requires server of version 4.x started with 'replSet' option
* Add `MongoHeaders` for change stream events

* Change `MessageProducerSupport` to use a `takeWhile((message) -> isRunning())`
instead of storing a `subscription` from a callback
* Document new features

* Remove trailing whitespaces

* Doc Polishing.
This commit is contained in:
Artem Bilan
2020-04-02 21:32:08 -04:00
committed by Gary Russell
parent 2d7e47355b
commit d8c378bd28
11 changed files with 745 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-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.
@@ -16,11 +16,14 @@
package org.springframework.integration.endpoint;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.core.AttributeAccessor;
import org.springframework.integration.IntegrationPattern;
import org.springframework.integration.IntegrationPatternType;
import org.springframework.integration.channel.ReactiveStreamsSubscribableChannel;
import org.springframework.integration.core.MessageProducer;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.history.MessageHistory;
@@ -36,6 +39,8 @@ import org.springframework.messaging.support.ErrorMessage;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Flux;
/**
* A support class for producer endpoints that provides a setter for the
* output channel and a convenience method for sending Messages.
@@ -176,7 +181,8 @@ public abstract class MessageProducerSupport extends AbstractEndpoint implements
}
/**
* Takes no action by default. Subclasses may override this if they
* Take no action by default.
* Subclasses may override this if they
* need lifecycle-managed behavior. Protected by 'lifecycleLock'.
*/
@Override
@@ -184,7 +190,8 @@ public abstract class MessageProducerSupport extends AbstractEndpoint implements
}
/**
* Takes no action by default. Subclasses may override this if they
* Take no action by default.
* Subclasses may override this if they
* need lifecycle-managed behavior.
*/
@Override
@@ -196,13 +203,10 @@ public abstract class MessageProducerSupport extends AbstractEndpoint implements
if (message == null) {
throw new MessagingException("cannot send a null message");
}
if (this.shouldTrack) {
message = MessageHistory.write(message, this, getMessageBuilderFactory());
}
message = trackMessageIfAny(message);
try {
MessageChannel messageChannel = getOutputChannel();
Assert.state(messageChannel != null, "The 'outputChannel' or `outputChannelName` must be configured");
this.messagingTemplate.send(messageChannel, message);
MessageChannel outputChannel = getRequiredOutputChannel();
this.messagingTemplate.send(outputChannel, message);
}
catch (RuntimeException ex) {
if (!sendErrorMessageIfNecessary(message, ex)) {
@@ -211,6 +215,33 @@ public abstract class MessageProducerSupport extends AbstractEndpoint implements
}
}
protected void subscribeToPublisher(Publisher<? extends Message<?>> publisher) {
MessageChannel outputChannel = getRequiredOutputChannel();
Flux<? extends Message<?>> messageFlux =
Flux.from(publisher)
.map(this::trackMessageIfAny)
.doOnComplete(this::stop)
.doOnCancel(this::stop)
.takeWhile((message) -> isRunning());
if (outputChannel instanceof ReactiveStreamsSubscribableChannel) {
((ReactiveStreamsSubscribableChannel) outputChannel).subscribeTo(messageFlux);
}
else {
messageFlux
.doOnNext((message) -> {
try {
sendMessage(message);
}
catch (Exception ex) {
logger.error("Error sending a message: " + message, ex);
}
})
.subscribe();
}
}
/**
* Send an error message based on the exception and message.
* @param message the message.
@@ -218,7 +249,7 @@ public abstract class MessageProducerSupport extends AbstractEndpoint implements
* @return true if the error channel is available and message sent.
* @since 4.3.10
*/
protected final boolean sendErrorMessageIfNecessary(Message<?> message, RuntimeException exception) {
protected final boolean sendErrorMessageIfNecessary(Message<?> message, Exception exception) {
MessageChannel channel = getErrorChannel();
if (channel != null) {
this.messagingTemplate.send(channel, buildErrorMessage(message, exception));
@@ -235,9 +266,8 @@ public abstract class MessageProducerSupport extends AbstractEndpoint implements
* @return the error message.
* @since 4.3.10
*/
protected final ErrorMessage buildErrorMessage(Message<?> message, RuntimeException exception) {
return this.errorMessageStrategy.buildErrorMessage(exception,
getErrorMessageAttributes(message));
protected final ErrorMessage buildErrorMessage(Message<?> message, Exception exception) {
return this.errorMessageStrategy.buildErrorMessage(exception, getErrorMessageAttributes(message));
}
/**
@@ -252,4 +282,19 @@ public abstract class MessageProducerSupport extends AbstractEndpoint implements
return ErrorMessageUtils.getAttributeAccessor(message, null);
}
private MessageChannel getRequiredOutputChannel() {
MessageChannel messageChannel = getOutputChannel();
Assert.state(messageChannel != null, "The 'outputChannel' or `outputChannelName` must be configured");
return messageChannel;
}
private Message<?> trackMessageIfAny(Message<?> message) {
if (this.shouldTrack) {
return MessageHistory.write(message, this, getMessageBuilderFactory());
}
else {
return message;
}
}
}