【问题标题】:How to explicitely close Spring context in a Spring web application?如何在 Spring Web 应用程序中显式关闭 Spring 上下文?
【发布时间】:2013-06-14 07:30:30
【问题描述】:

我有一个在 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

如何在 webapp 中实现这一点(在应用程序停止时调用上下文上的 close() 方法),其中上下文由 ServletContextListener (org.springframework.web.context.ContextLoaderListener) 创建?

编辑:我的问题实际上被误导了。上下文实际上已经被关闭,RmiServiceExporter 的 destroy() 方法也是如此。问题(tomcat 无法正常关闭)似乎来自其他问题。请花时间回答这个问题的用户原谅我匆忙发布它。

【问题讨论】:

  • 您能否分享一下导致最初问题的原因。(tomcat 无法正常关闭)及其解决方案。 (目前我也面临类似的问题)
  • @Akash :对不起,那是很久以前的事了,我不记得了。但与 RMI 无关。

标签: java spring spring-mvc rmi


【解决方案1】:

1)在你的bean定义中添加destroy方法:

<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)如果您有指向上下文实例的链接,请添加关闭挂钩

context.registerShutdownHook();

【讨论】:

  • RmiServiceExporter 实现了 DisposableBean 所以恕我直言,没有必要提供销毁方法。
  • 没错。出口商的 destroy() 方法已经被调用。请参阅我对问题的编辑。
【解决方案2】:

尝试使用 ContextLoaderListener 来启动您的应用程序上下文,如 here 所述。它应该会自动为您关闭上下文。

【讨论】:

  • 我已经在使用 org.springframework.web.context.ContextLoaderListener 但它似乎没有关闭上下文......或者它可能确实是问题出在其他地方。我会调查的。
  • 你试过调试吗? destroy() 方法中有一些 try/catch。您也可以使用 jVisualVM 对其进行分析并找出 VM 未关闭的原因。
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 2017-03-06
  • 2016-03-28
  • 2016-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多