当没有更多消息时退出 Spring Integration [英] Exit Spring Integration when no more messages

查看:24
本文介绍了当没有更多消息时退出 Spring Integration的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using a Spring Integration (4.1) config to retrieve message from DB as a batch more than as a service. I know I'll have a dozen of messages to process daily, and therefore I need to run the batch once a day.

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

My jdbc:inbound-channel-adapter is configured to retrieve max-rows-per-poll="1". I'd like to be notified in some way when there are no more messages, so that I can exit the batch, but I have issues finding where to "plug". I tried with an interceptor that remembers when last message went through + a scheduled task that retrieves that timestamp and checks if it's more than a configured timeout, but it felt very cumbersome and tried with AOP instead, which feels like a better solution.

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

I'd like to intercept the call to AbstractPollingEndpoint.doPoll() which returns false when there's no message but I'm not able to find a way to do that : I tried subclassing AbstractRequestHandlerAdvice but that doesn't work when applied on a poller (it tells me so in the logs). Now I'm trying to implement MethodInterceptor and configure as below, and I see I can intercept the call to "call" method, but I'm not sure it's the right way

<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 ,是在这个阶段,我们没有消息要处理.

Isn't there an easier way to do this ? The main difficulty, as stated here http://forum.spring.io/forum/spring-projects/integration/127410-poller-with-advice-chain , is that at this stage, we don't have message to work on.

谢谢

文森特

推荐答案

看来我已经非常接近答案了.. 因为我可以访问 call 方法的结果,所以我需要做的就是如果结果抛出异常是错误的,以及问题中的 XML 配置:

Looks like I was very close to the answer actually.. Since I have access to the result of call method, all I need to do is throw exception if result is false, along with XML config from the question :

公共类 NoMoreMessageNotifierAdvice 实现 MethodInterceptor {

public class NoMoreMessageNotifierAdvice implements 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;
}

}

这篇关于当没有更多消息时退出 Spring Integration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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