问题
I have a list of let's say size 10, I want to aggregate using max size 6. In this case it should work like this: first six messages are aggregated into one message and then right away (without any timeout) the next 4 messages are aggregated into second message. How can I achieve this in spring integration? I tried using releaseStrategy but I can only define in it the max size, and then the messages that are left (4 messages in my case) are waiting in the aggregator for more messages (so the max size condition is met). I don't want to wait for aggregator timeout cause I know that my list's size is 10, so there is nothing to wait for after the 4th message, it should create the second aggregated message right away.
回答1:
Use a custom ReleaseStrategy
; something like...
public class MyReleaseStrategy implements ReleaseStrategy {
private final Map<Object, AtomicInteger>() map = new HashMap<>();
public boolean canRelease(MessageGroup group) {
AtomicInteger count = map.get(group.getGroupId());
if (count == null) {
count = new AtomicInteger();
map.put(int);
}
int n = count.incrementAndGet();
boolean canRelease = n == 6 || n == 10;
if (n == 10) {
map.remove(group.getGroupId());
}
return canRelease;
}
}
来源:https://stackoverflow.com/questions/31077611/aggregate-list-based-on-size