Spring integration Spring聚合组
我确实创建了一个聚合服务,如下所示Spring integration Spring聚合组,spring-integration,spring-cloud-stream,Spring Integration,Spring Cloud Stream,我确实创建了一个聚合服务,如下所示 @EnableBinding(Processor.class) class Configuration { @Autowired Processor processor; @ServiceActivator(inputChannel = Processor.INPUT) @Bean public MessageHandler aggregator() { AggregatingMessageHandler aggregatingMessageH
@EnableBinding(Processor.class)
class Configuration {
@Autowired
Processor processor;
@ServiceActivator(inputChannel = Processor.INPUT)
@Bean
public MessageHandler aggregator() {
AggregatingMessageHandler aggregatingMessageHandler =
new AggregatingMessageHandler(new DefaultAggregatingMessageGroupProcessor(),
new SimpleMessageStore(10));
//AggregatorFactoryBean aggregatorFactoryBean = new AggregatorFactoryBean();
//aggregatorFactoryBean.setMessageStore();
aggregatingMessageHandler.setOutputChannel(processor.output());
//aggregatorFactoryBean.setDiscardChannel(processor.output());
aggregatingMessageHandler.setSendPartialResultOnExpiry(true);
aggregatingMessageHandler.setSendTimeout(1000L);
aggregatingMessageHandler.setCorrelationStrategy(new ExpressionEvaluatingCorrelationStrategy("requestType"));
aggregatingMessageHandler.setReleaseStrategy(new MessageCountReleaseStrategy(3)); //ExpressionEvaluatingReleaseStrategy("size() == 5")
aggregatingMessageHandler.setExpireGroupsUponCompletion(true);
aggregatingMessageHandler.setGroupTimeoutExpression(new ValueExpression<>(3000L)); //size() ge 2 ? 5000 : -1
aggregatingMessageHandler.setExpireGroupsUponTimeout(true);
return aggregatingMessageHandler;
}
}
@EnableBinding(Processor.class)
类配置{
@自动连线
处理器;
@ServiceActivator(inputChannel=Processor.INPUT)
@豆子
public MessageHandler聚合器(){
聚合MessageHandler聚合MessageHandler=
新建AggregatingMessageHandler(新建DefaultAggregatingMessageGroupProcessor(),
新SimpleMessageStore(10));
//AggregatorFactoryBean AggregatorFactoryBean=新的AggregatorFactoryBean();
//aggregatorFactoryBean.setMessageStore();
aggregatingMessageHandler.setOutputChannel(processor.output());
//aggregatorFactoryBean.setDiscardChannel(processor.output());
aggregatingMessageHandler.setSendPartialResultOnExpiry(true);
aggregatingMessageHandler.setSendTimeout(1000L);
aggregatingMessageHandler.SetCorrelationsStrategy(新的ExpressionEvaluationCorrelationsStrategy(“requestType”);
aggregatingMessageHandler.setReleaseStrategy(新MessageCountReleaseStrategy(3));//表达式评估ReleaseStrategy(“size()==5”)
aggregatingMessageHandler.setExpireGroupsUponCompletion(true);
aggregatingMessageHandler.setGroupTimeoutExpression(新值表达式(3000L));//大小()ge 2?5000:-1
aggregatingMessageHandler.setExpireGroupsUponTimeout(true);
返回聚合MessageHandler;
}
}
现在我想在创建一个新组后立即释放该组,因此我一次只有一个组
更具体地说,我确实收到两种类型的请求“PUT”和“DEL”。我希望按照上述规则继续聚合,但一旦收到除我要聚合的请求类型以外的其他请求类型,我希望释放当前组并开始聚合新类型
我之所以要这样做,是因为这些请求被发送到不支持同时执行PUT和DEL请求的另一方,我不能延迟任何DEL请求,因为PUT和DEL之间的顺序很重要
我知道我需要创建一个定制的发布Pojo,但是我能检查当前的组吗
比如说
如果我收到如下6条消息
放,放,放,放,放
它们应汇总如下
3份
2德尔
一掷
嗯。谢谢分享更多信息 是的,您自定义的
ReleaseStrategy
可以检查该消息类型,并返回true
,以实现组完成功能
只要您只有staticcorrelationKey
,存储中就只有一个组。当您的消息进入发布策略时
,只需检查当前组的完成信号就不会有太大的魔力。由于存储中没有任何其他组,因此不需要任何复杂的发布逻辑
您应该添加expireGroupsUponCompletion=true
,以便在完成后删除组,下一条消息将为相同的correlationKey
组成一个新组
更新
感谢您提供更多信息
所以,是的,你原来的PoC很好。即使是静态的correlationKey
也可以,因为您只需要将传入消息收集到批处理中
您的自定义ReleaseStrategy
应该分析MessageGroup
中具有不同键的消息,并在这种情况下返回true
自定义MessageGroupProcessor
应从输出列表中筛选具有不同键的消息,并将该消息发送回聚合器,以便为其键的序列组成新组。谢谢分享更多信息
是的,您自定义的ReleaseStrategy
可以检查该消息类型,并返回true
,以实现组完成功能
只要您只有staticcorrelationKey
,存储中就只有一个组。当您的消息进入发布策略时
,只需检查当前组的完成信号就不会有太大的魔力。由于存储中没有任何其他组,因此不需要任何复杂的发布逻辑
您应该添加expireGroupsUponCompletion=true
,以便在完成后删除组,下一条消息将为相同的correlationKey
组成一个新组
更新
感谢您提供更多信息
所以,是的,你原来的PoC很好。即使是静态的correlationKey
也可以,因为您只需要将传入消息收集到批处理中
您的自定义ReleaseStrategy
应该分析MessageGroup
中具有不同键的消息,并在这种情况下返回true
自定义MessageGroupProcessor
应该从输出列表中过滤具有不同键的消息,并将该消息发送回聚合器,以便为其键的序列组成一个新组我最终实现了以下释放策略
,因为我发现它比删除更简单消息并再次对其进行排队
class MessageCountAndOnlyOneGroupReleaseStrategy implements org.springframework.integration.aggregator.ReleaseStrategy {
private final int threshold;
private final MessageGroupProcessor messageGroupProcessor;
public MessageCountAndOnlyOneGroupReleaseStrategy(int threshold,MessageGroupProcessor messageGroupProcessor) {
super();
this.threshold = threshold;
this.messageGroupProcessor = messageGroupProcessor;
}
private MessageGroup currentGroup;
@Override
public boolean canRelease(MessageGroup group) {
if(currentGroup == null)
currentGroup = group;
if(!group.getGroupId().equals(currentGroup.getGroupId())) {
messageGroupProcessor.processMessageGroup(currentGroup);
currentGroup = group;
return false;
}
return group.size() >= this.threshold;
}
}
请注意,我确实使用了新的HeaderAttributeCorrelationsStrategy(“request_type”)
而不是FOO
作为协作策略我最终实现了以下发布策略
,因为我发现它比删除消息并再次排队更简单
class MessageCountAndOnlyOneGroupReleaseStrategy implements org.springframework.integration.aggregator.ReleaseStrategy {
private final int threshold;
private final MessageGroupProcessor messageGroupProcessor;
public MessageCountAndOnlyOneGroupReleaseStrategy(int threshold,MessageGroupProcessor messageGroupProcessor) {
super();
this.threshold = threshold;
this.messageGroupProcessor = messageGroupProcessor;
}
private MessageGroup currentGroup;
@Override
public boolean canRelease(MessageGroup group) {
if(currentGroup == null)
currentGroup = group;
if(!group.getGroupId().equals(currentGroup.getGroupId())) {
messageGroupProcessor.processMessageGroup(currentGroup);
currentGroup = group;
return false;
}
return group.size() >= this.threshold;
}
}
请注意,我确实使用了新的HeaderAttributeCorrelationsStrategy(“请求类型”)
而不是FOO
作为ColollationStrategy
抱歉,您的问题不清楚。将CorrelationStrategy
作为FOO
literal,您总是得到同一个组,并且所有消息仅聚合到此组。没别的了。完全不清楚“新团队成立后尽快释放团队”应该采取什么行动