经过一番调试和阅读,我终于找到了这个问题的解决方案。入站网关是一个 JmsMessageDrivenEndpoint,它基于两个内部组件,一个 MessageListenerContainer 和一个 MessageListener。MessageListenerContainer 是负责调度 MessageListener 行为的人,因此,覆盖 noMessageReceived 和 messageReceived,并添加一些属性来控制所需的行为,我可以做到“魔术”。
我的 MessageListenerContainer 实现是这样的。
public class ControlMessageListenerContainer extends DefaultMessageListenerContainer{
private JmsMessageDrivenEndpoint mainInputGateway;
private long timeOut;
private long lastTimeReceived;
public PassControlMessageListenerContainer() {
this.setAutoStartup(false);
}
@Override
public void start() throws JmsException {
/*When the container is started the lastTimeReceived is set to actial time*/
lastTimeReceived = (new Date()).getTime();
super.start();
}
@Override
protected void noMessageReceived(Object invoker, Session session) {
long actualTime = (new Date()).getTime();
if((actualTime - lastTimeReceived) >= timeOut
&& mainInputGateway != null && !mainInputGateway.isRunning()){
mainInputGateway.start();
}
super.noMessageReceived(invoker, session);
}
@Override
protected void messageReceived(Object invoker, Session session) {
/*lastTimeReceived is set again to actual time at new message arrive*/
lastTimeReceived = (new Date()).getTime();
super.messageReceived(invoker, session);
}
}
最后,spring bean 配置如下:
<bean id="listenerContainer"
class="org.merol.ControlMessageListenerContainer">
<property name="mainInputGateway" ref="mainGateway" />
<property name="destination" ref="onHoldQueue" />
<property name="timeOut" value="10000"/>
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
<bean id="messageListener"
class="org.springframework.integration.jms.ChannelPublishingJmsMessageListener">
<property name="requestChannel" ref="outputChannel" />
</bean>
<bean id="inboundGateway"
class="org.springframework.integration.jms.JmsMessageDrivenEndpoint">
<constructor-arg name="listenerContainer" ref="listenerContainer" />
<constructor-arg name="listener" ref="messageListener" />
</bean>
希望这对其他人有帮助。
感谢@Nicholas 提供线索。