Skip to content

Commit 860f9fe

Browse files
committed
Fix MongoDb module for the latest Spring Data
The `MappingMongoConverter` now used different path when it iterates documents form conversion from a DB cursor * Fix `MongoDbMessageStore.MessageReadingMongoConverter` to override a `read(TypeInformation<S>, Bson)` method to convert a `MessageWrapper` properly * Upgrade to the latest MongoDb driver
1 parent 55c5bef commit 860f9fe

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ ext {
8585
mailVersion = '2.0.1'
8686
micrometerVersion = '1.10.0-SNAPSHOT'
8787
mockitoVersion = '4.4.0'
88-
mongoDriverVersion = '4.5.0'
88+
mongoDriverVersion = '4.6.0'
8989
mysqlVersion = '8.0.28'
9090
pahoMqttClientVersion = '1.2.5'
9191
postgresVersion = '42.3.3'
9292
r2dbch2Version = '0.9.1.RELEASE'
93-
reactorVersion = '2020.0.15'
93+
reactorVersion = '2020.0.18'
9494
resilience4jVersion = '1.7.1'
9595
romeToolsVersion = '1.18.0'
9696
rsocketVersion = '1.1.1'

spring-integration-mongodb/src/main/java/org/springframework/integration/mongodb/store/MongoDbMessageStore.java

+25-12
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.springframework.core.convert.converter.Converter;
4444
import org.springframework.core.serializer.support.SerializingConverter;
4545
import org.springframework.data.annotation.Id;
46-
import org.springframework.data.annotation.Transient;
4746
import org.springframework.data.convert.ReadingConverter;
4847
import org.springframework.data.convert.WritingConverter;
4948
import org.springframework.data.domain.Sort;
@@ -63,6 +62,7 @@
6362
import org.springframework.data.mongodb.core.query.Criteria;
6463
import org.springframework.data.mongodb.core.query.Query;
6564
import org.springframework.data.mongodb.core.query.Update;
65+
import org.springframework.data.util.TypeInformation;
6666
import org.springframework.integration.history.MessageHistory;
6767
import org.springframework.integration.message.AdviceMessage;
6868
import org.springframework.integration.store.AbstractMessageGroupStore;
@@ -495,9 +495,9 @@ private long getNextId() {
495495
Query query = Query.query(Criteria.where("_id").is(SEQUENCE_NAME));
496496
query.fields().include(SEQUENCE);
497497
return ((Number) this.template.findAndModify(query,
498-
new Update().inc(SEQUENCE, 1L),
499-
FindAndModifyOptions.options().returnNew(true).upsert(true),
500-
Map.class, this.collectionName)
498+
new Update().inc(SEQUENCE, 1L),
499+
FindAndModifyOptions.options().returnNew(true).upsert(true),
500+
Map.class, this.collectionName)
501501
.get(SEQUENCE)) // NOSONAR - never returns null
502502
.longValue();
503503
}
@@ -589,9 +589,14 @@ public <S> S read(Class<S> clazz, Bson source) {
589589
if (!MessageWrapper.class.equals(clazz)) {
590590
return super.read(clazz, source);
591591
}
592+
593+
return (S) readAsMessageWrapper(source);
594+
}
595+
596+
private MessageWrapper readAsMessageWrapper(Bson source) {
592597
if (source != null) {
593598
Map<String, Object> sourceMap = asMap(source);
594-
Message<?> message = null;
599+
Message<?> message;
595600
Object messageType = sourceMap.get("_messageType");
596601
if (messageType == null) {
597602
messageType = GenericMessage.class.getName();
@@ -629,21 +634,30 @@ public <S> S read(Class<S> clazz, Bson source) {
629634
}
630635
wrapper.setCondition((String) sourceMap.get("_condition"));
631636

632-
return (S) wrapper;
637+
return wrapper;
633638
}
634639
return null;
635640
}
636641

642+
@Override
643+
@SuppressWarnings({ UNCHECKED })
644+
protected <S> S read(TypeInformation<S> type, Bson source) {
645+
if (!MessageWrapper.class.equals(type.getType())) {
646+
return super.read(type, source);
647+
}
648+
649+
return (S) readAsMessageWrapper(source);
650+
}
651+
637652
private Map<String, Object> normalizeHeaders(Map<String, Object> headers) {
638653
Map<String, Object> normalizedHeaders = new HashMap<>();
639654
for (Entry<String, Object> entry : headers.entrySet()) {
640655
String headerName = entry.getKey();
641656
Object headerValue = entry.getValue();
642-
if (headerValue instanceof Bson) {
643-
Bson source = (Bson) headerValue;
657+
if (headerValue instanceof Bson source) {
644658
Map<String, Object> document = asMap(source);
645659
try {
646-
Class<?> typeClass = null;
660+
Class<?> typeClass;
647661
if (document.containsKey(CLASS)) {
648662
Object type = document.get(CLASS);
649663
typeClass = ClassUtils.forName(type.toString(), MongoDbMessageStore.this.classLoader);
@@ -670,8 +684,7 @@ else if (source instanceof BasicDBList) {
670684
private Object extractPayload(Bson source) {
671685
Object payload = asMap(source).get("payload");
672686

673-
if (payload instanceof Bson) {
674-
Bson payloadObject = (Bson) payload;
687+
if (payload instanceof Bson payloadObject) {
675688
Object payloadType = asMap(payloadObject).get(CLASS);
676689
try {
677690
Class<?> payloadClass =
@@ -845,7 +858,7 @@ private static final class MessageWrapper {
845858

846859
private volatile Object _groupId; // NOSONAR name
847860

848-
@Transient
861+
// @Transient
849862
private final Message<?> message; // NOSONAR name
850863

851864
@SuppressWarnings(UNUSED)

0 commit comments

Comments
 (0)