SpringBoot ActiveMQ注入JmsListenerEndpointRegistry时通过IDEA停止服务时报错(通过优雅停服正常)。本想通过注入JmsListenerEndpointRegistry类停止(stop)和销毁(destroy)activemq,发现注入JmsListenerEndpointRegistry类后,通过idea停止程序时会报错,报错如下:

javax.jms.IllegalStateException: The Session is closed
    at org.apache.activemq.ActiveMQSession.checkClosed(ActiveMQSession.java:772) ~[activemq-client-5.16.4.jar:5.16.4]
    at org.apache.activemq.ActiveMQSession.rollback(ActiveMQSession.java:597) ~[activemq-client-5.16.4.jar:5.16.4]
    at org.messaginghub.pooled.jms.JmsPoolSession.close(JmsPoolSession.java:112) ~[pooled-jms-1.2.4.jar:na]
    at org.springframework.jms.support.JmsUtils.closeSession(JmsUtils.java:109) [spring-jms-5.3.19.jar:5.3.19]
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.clearResources(DefaultMessageListenerContainer.java:1289) [spring-jms-5.3.19.jar:5.3.19]
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.access$100(DefaultMessageListenerContainer.java:1089) [spring-jms-5.3.19.jar:5.3.19]
    at org.springframework.jms.listener.DefaultMessageListenerContainer.doShutdown(DefaultMessageListenerContainer.java:638) [spring-jms-5.3.19.jar:5.3.19]
    at org.springframework.jms.listener.AbstractJmsListeningContainer.shutdown(AbstractJmsListeningContainer.java:244) [spring-jms-5.3.19.jar:5.3.19]
    at org.springframework.jms.listener.AbstractJmsListeningContainer.destroy(AbstractJmsListeningContainer.java:184) [spring-jms-5.3.19.jar:5.3.19]
    at org.springframework.jms.config.JmsListenerEndpointRegistry.destroy(JmsListenerEndpointRegistry.java:251) [spring-jms-5.3.19.jar:5.3.19]
    at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:213) [spring-beans-5.3.19.jar:5.3.19]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:587) [spring-beans-5.3.19.jar:5.3.19]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:559) [spring-beans-5.3.19.jar:5.3.19]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:1161) [spring-beans-5.3.19.jar:5.3.19]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:520) [spring-beans-5.3.19.jar:5.3.19]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:1154) [spring-beans-5.3.19.jar:5.3.19]
    at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1106) [spring-context-5.3.19.jar:5.3.19]
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1075) [spring-context-5.3.19.jar:5.3.19]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.doClose(ServletWebServerApplicationContext.java:172) [spring-boot-2.6.7.jar:2.6.7]
    at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:1021) [spring-context-5.3.19.jar:5.3.19]
    at org.springframework.boot.SpringApplicationShutdownHook.closeAndWait(SpringApplicationShutdownHook.java:145) [spring-boot-2.6.7.jar:2.6.7]
    at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_271]
    at org.springframework.boot.SpringApplicationShutdownHook.run(SpringApplicationShutdownHook.java:114) [spring-boot-2.6.7.jar:2.6.7]
    at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_271]

解决办法:
方法一:在注入的时候加上@Lazy,进行延迟加载bean
方法二:在stop或destroy时通过SpringBeanUtils.getBean(JmsListenerEndpointRegistry.class).xxx();

最后修改:2022 年 09 月 03 日
如果觉得我的文章对你有用,请随意赞赏