2

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

4

1 回答 1

2

使用自定义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;
    }
}
于 2015-06-26T17:47:52.153 回答