【发布时间】:2021-04-30 15:50:04
【问题描述】:
我正在使用 Spring 集成收听 Solace 动态主题,如下所示:
<int-jms:message-driven-channel-adapter
id="myJmsAdapter" channel="receiveChannel" container="jmsContainer" />
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="solaceConnectionFactory" />
<property name="destinationName" value="${my.topic}/#{main.getCar_type_value()}" />
<property name="pubSubDomain" value="true" />
<property name="sessionTransacted" value="false" />
</bean>
我能够聆听主题、使用消息并进行处理。
但是在System.exit(0) 上,即使在 ShutdownHook 中关闭了 spring 应用程序上下文,JMS 侦听器线程仍处于挂起状态,无法正常关闭:
ctxt.registerShutdownHook();
...
trigger = new ShutdownTrigger(maxWaitForResponseMillis, ctxt);
trigger.startShutdownTimer();
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
log.info("Exiting Main Thread!");
ctxt.close();
}
});
应用程序在超时时正常关闭,但是当我尝试在计时器之前使用 System.exit(0) 从其他类中退出时,它没有关闭并且线程挂起。
定时器功能如下:
public void startShutdownTimer() {
timer.schedule(new TimerTask() {
@Override
public void run() {
log.info("Timer max wait has elapsed - triggering graceful
shutdown");
// perform some task here on timeout..
}
log.info("Returning with exit code: " + exitCode);
System.exit(exitCode); //this one works and it properly shutsdown
}
}, maxWaitForResponseMillis);
}
我还尝试在关闭主类的上下文之前添加 Timer.cancel,如下所示,但它不起作用:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
log.info("Cancelling current timer task..")
trigger.getTimer().cancel(); //it didnt work and the thread still hangs
log.info("Exiting Main Thread!");
ctxt.close();
}
});
我尝试取出 Timer 函数及其所有引用,以查看是否它的这个计时器线程导致了问题,但没有。即使计时器任务被删除,应用程序也会挂起。
下面是挂起时的线程栈:
"Thread-3" #26 prio=5 os_prio=0 tid=0x00007fcf48024800 nid=0x60ad in Object.wait() [0x00007fd026ef1000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at org.springframework.jms.listener.DefaultMessageListenerContainer.doShutdown(DefaultMessageListenerContainer.java:568)
- locked <0x00000000ee0c9ec0> (a java.lang.Object)
at org.springframework.jms.listener.AbstractJmsListeningContainer.shutdown(AbstractJmsListeningContainer.java:237)
请帮助找出问题和解决方案...
【问题讨论】:
-
将整个线程转储发布到某个地方,例如 GitHub Gist 或 PasteBin 等。
标签: java spring spring-jms jms-topic application-shutdown