基于大小的聚合列表 [英] Aggregate list based on size

查看:35
本文介绍了基于大小的聚合列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大小为 10 的列表,我想使用最大大小 6 进行聚合.在这种情况下,它应该是这样工作的:前 6 条消息聚合成一条消息,然后立即(没有任何超时)下 4 条消息聚合成第二条消息.如何在 spring 集成中实现这一目标?我尝试使用 releaseStrategy 但我只能在其中定义最大大小,然后剩下的消息(在我的情况下为 4 条消息)在聚合器中等待更多消息(因此满足最大大小条件).我不想等待聚合器超时,因为我知道我的列表大小是 10,所以在第 4 条消息之后没有什么可等待的,它应该立即创建第二条聚合消息.

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.

推荐答案

使用自定义ReleaseStrategy;像……

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;
    }
}

这篇关于基于大小的聚合列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆