问题
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.
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.
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>
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.
Thanks
Vincent
回答1:
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 :
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;
}
}
回答2:
The upcoming 4.2 release (we just released the first milestone) has support for conditional pollers.
来源:https://stackoverflow.com/questions/30567742/exit-spring-integration-when-no-more-messages