没有更多消息时退出 Spring 集成

Exit Spring Integration when no more messages

我正在使用 Spring 集成 (4.1) 配置从数据库中批量检索消息,而不是作为服务检索消息。我知道我每天要处理一打消息,因此我需要每天 运行 一次批处理。

我的 jdbc:inbound-channel-adapter 配置为检索 max-rows-per-poll="1"。我希望在没有更多消息时以某种方式收到通知,以便我可以退出批处理,但我无法找到 "plug" 的位置。我尝试使用一个拦截器来记住最后一条消息何时通过 + 一个计划任务,该任务检索该时间戳并检查它是否超过配置的超时,但它感觉非常麻烦并尝试使用 AOP,这感觉像是一个更好的解决方案。

我想拦截对 AbstractPollingEndpoint.doPoll() 的调用,当没有消息时 returns false 但我找不到方法来做到这一点:我尝试子类化 AbstractRequestHandlerAdvice但这在应用于轮询器时不起作用(它在日志中告诉我)。现在我正在尝试实现 MethodInterceptor 并如下配置,我发现我可以拦截对 "call" 方法的调用,但我不确定这是正确的方法

<int:poller default="true" trigger="periodicTriggerWithInitialDelay">
    <int:advice-chain>          
        <bean class="com.myBatch.NoMoreMessageNotifierAdvice" />
        <ref bean="txAdvice"/>
    </int:advice-chain>
</int:poller>

没有更简单的方法吗?如此处 http://forum.spring.io/forum/spring-projects/integration/127410-poller-with-advice-chain 所述,主要困难在于,在这个阶段,我们没有要处理的消息。

谢谢

文森特

看起来我实际上非常接近答案。因为我可以访问调用方法的结果,我需要做的就是在结果为假时抛出异常,以及 XML 配置来自问题:

public class NoMessageNotifierAdvice 实现 MethodInterceptor {

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {

    Object result=invocation.proceed();

    if(result instanceof Boolean){

        boolean hasPolledAMessage=(Boolean)result;

        if(hasPolledAMessage){
            return true;
        }
        else{
            throw new StopBatchException("no message was received on latest poll -> throwing exception to exit");
        }       
    }

    return result;
}

}

即将发布的 4.2 版本(我们刚刚发布 the first milestone) has support for conditional pollers