Skip to content

Commit 469bd6b

Browse files
committed
GH-3816: Fix MongoDb module for the latest SD
Fixes #3816 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 ab6060f commit 469bd6b

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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;
@@ -496,9 +496,9 @@ private long getNextId() {
496496
Query query = Query.query(Criteria.where("_id").is(SEQUENCE_NAME));
497497
query.fields().include(SEQUENCE);
498498
return ((Number) this.template.findAndModify(query,
499-
new Update().inc(SEQUENCE, 1L),
500-
FindAndModifyOptions.options().returnNew(true).upsert(true),
501-
Map.class, this.collectionName)
499+
new Update().inc(SEQUENCE, 1L),
500+
FindAndModifyOptions.options().returnNew(true).upsert(true),
501+
Map.class, this.collectionName)
502502
.get(SEQUENCE)) // NOSONAR - never returns null
503503
.longValue();
504504
}
@@ -590,9 +590,14 @@ public <S> S read(Class<S> clazz, Bson source) {
590590
if (!MessageWrapper.class.equals(clazz)) {
591591
return super.read(clazz, source);
592592
}
593+
594+
return (S) readAsMessageWrapper(source);
595+
}
596+
597+
private MessageWrapper readAsMessageWrapper(Bson source) {
593598
if (source != null) {
594599
Map<String, Object> sourceMap = asMap(source);
595-
Message<?> message = null;
600+
Message<?> message;
596601
Object messageType = sourceMap.get("_messageType");
597602
if (messageType == null) {
598603
messageType = GenericMessage.class.getName();
@@ -630,11 +635,21 @@ public <S> S read(Class<S> clazz, Bson source) {
630635
}
631636
wrapper.setCondition((String) sourceMap.get("_condition"));
632637

633-
return (S) wrapper;
638+
return wrapper;
634639
}
635640
return null;
636641
}
637642

643+
@Override
644+
@SuppressWarnings({ UNCHECKED })
645+
protected <S> S read(TypeInformation<S> type, Bson source) {
646+
if (!MessageWrapper.class.equals(type.getType())) {
647+
return super.read(type, source);
648+
}
649+
650+
return (S) readAsMessageWrapper(source);
651+
}
652+
638653
private Map<String, Object> normalizeHeaders(Map<String, Object> headers) {
639654
Map<String, Object> normalizedHeaders = new HashMap<>();
640655
for (Entry<String, Object> entry : headers.entrySet()) {
@@ -644,7 +659,7 @@ private Map<String, Object> normalizeHeaders(Map<String, Object> headers) {
644659
Bson source = (Bson) headerValue;
645660
Map<String, Object> document = asMap(source);
646661
try {
647-
Class<?> typeClass = null;
662+
Class<?> typeClass;
648663
if (document.containsKey(CLASS)) {
649664
Object type = document.get(CLASS);
650665
typeClass = ClassUtils.forName(type.toString(), MongoDbMessageStore.this.classLoader);
@@ -846,7 +861,7 @@ private static final class MessageWrapper {
846861

847862
private volatile Object _groupId; // NOSONAR name
848863

849-
@Transient
864+
// @Transient
850865
private final Message<?> message; // NOSONAR name
851866

852867
@SuppressWarnings(UNUSED)

0 commit comments

Comments
 (0)