繁体   English   中英

如何在Spring Web应用程序中显式关闭Spring上下文?

[英]How to explicitely close Spring context in a Spring web application?

我有一个运行在Tomcat中的Spring webapp。

该应用还需要公开一项服务,以通过RMI进行远程访问。 我正在使用Spring的RmiServiceExporter来做到这一点:

<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
    <property name="serviceName" value="${server.rmi.remoteServiceName}"/>
    <property name="service" ref="remoteFacade"/>
    <property name="serviceInterface" value="my.ServiceInterface"/>
    <!-- defaults to 1099 -->
    <property name="registryPort" value="${server.rmi.registryPort}"/>
</bean>

它运行良好,但是当我关闭Tomcat时,它挂起了,我必须将其杀死,然后手动杀死它为RMI注册表创建的Java进程。 似乎是因为我应该在创建RmiServiceExporter的上下文中显式关闭(即调用close()方法),在这种情况下,是我的应用程序上下文(而不是Web上下文)。 (来源: https : //issues.springsource.org/browse/SPR-5601

如何在web应用程序中实现此目标(在停止应用程序时在上下文上调用close()方法),而该上下文是由ServletContextListenerorg.springframework.web.context.ContextLoaderListener )创建的?

编辑:我的问题实际上被误导了。 上下文实际上已经关闭,RmiServiceExporter的destroy()方法也已经关闭。 问题(tomcat无法正常关闭)似乎是由其他原因引起的。 希望那些花时间来回答这个问题的用户能原谅我的匆忙。

1)添加destroy方法到您的bean定义:

<bean class="org.springframework.remoting.rmi.RmiServiceExporter" destroy-method="destroy">
    <property name="serviceName" value="${server.rmi.remoteServiceName}"/>
    <property name="service" ref="remoteFacade"/>
    <property name="serviceInterface" value="my.ServiceInterface"/>
    <!-- defaults to 1099 -->
    <property name="registryPort" value="${server.rmi.registryPort}"/>
</bean>

2)如果您有链接到上下文实例,请添加shutdown hook

context.registerShutdownHook();

尝试通过描述使用ContextLoaderListener来启动应用程序上下文这里 它应该自动为您关闭上下文。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM