Aggregate list based on size

守給你的承諾、 提交于 2019-12-11 04:00:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!