GH-3446: Stream support in the MessageGroupStore

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

* For better resources utilization provide a `Stream<Message<?>>` API
on the `MessageGroupStore`, `MessageGroup` and `MessageGroupQueue`
* Use this API in the `DelayHandler` when it reschedules persisted messages
This commit is contained in:
Artem Bilan
2021-01-19 18:40:10 -05:00
committed by Gary Russell
parent 51e240b761
commit f0f2c41ae3
12 changed files with 157 additions and 72 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* Copyright 2013-2021 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.
@@ -23,6 +23,7 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.springframework.data.domain.Sort;
@@ -74,7 +75,9 @@ public class ConfigurableMongoDbMessageStore extends AbstractConfigurableMongoDb
this(mongoDbFactory, null, DEFAULT_COLLECTION_NAME);
}
public ConfigurableMongoDbMessageStore(MongoDatabaseFactory mongoDbFactory, MappingMongoConverter mappingMongoConverter) {
public ConfigurableMongoDbMessageStore(MongoDatabaseFactory mongoDbFactory,
MappingMongoConverter mappingMongoConverter) {
this(mongoDbFactory, mappingMongoConverter, DEFAULT_COLLECTION_NAME);
}
@@ -82,8 +85,8 @@ public class ConfigurableMongoDbMessageStore extends AbstractConfigurableMongoDb
this(mongoDbFactory, null, collectionName);
}
public ConfigurableMongoDbMessageStore(MongoDatabaseFactory mongoDbFactory, MappingMongoConverter mappingMongoConverter,
String collectionName) {
public ConfigurableMongoDbMessageStore(MongoDatabaseFactory mongoDbFactory,
MappingMongoConverter mappingMongoConverter, String collectionName) {
super(mongoDbFactory, mappingMongoConverter, collectionName);
}
@@ -278,6 +281,18 @@ public class ConfigurableMongoDbMessageStore extends AbstractConfigurableMongoDb
.collect(Collectors.toList());
}
@Override
public Stream<Message<?>> streamMessagesForGroup(Object groupId) {
Assert.notNull(groupId, GROUP_ID_MUST_NOT_BE_NULL);
Query query = groupOrderQuery(groupId);
Stream<MessageDocument> documents =
getMongoTemplate()
.stream(query, MessageDocument.class, this.collectionName)
.stream();
return documents.map(MessageDocument::getMessage);
}
private void updateGroup(Object groupId, Update update) {
getMongoTemplate()
.findAndModify(groupOrderQuery(groupId), update, FindAndModifyOptions.none(), Map.class,

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
@@ -28,6 +28,7 @@ import java.util.Map.Entry;
import java.util.Properties;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.bson.Document;
import org.bson.conversions.Bson;
@@ -426,6 +427,17 @@ public class MongoDbMessageStore extends AbstractMessageGroupStore
.collect(Collectors.toList());
}
@Override
public Stream<Message<?>> streamMessagesForGroup(Object groupId) {
Assert.notNull(groupId, GROUP_ID_MUST_NOT_BE_NULL);
Query query = whereGroupIdOrder(groupId);
Stream<MessageWrapper> messageWrappers =
this.template.stream(query, MessageWrapper.class, this.collectionName)
.stream();
return messageWrappers.map(MessageWrapper::getMessage);
}
@Override
@ManagedAttribute
public int getMessageCountForAllMessageGroups() {