在Boot.scala中有一个电梯应用程序启动ssh守护程序。 这里是问题:当我运行container:restart /
在sbt会话中我得到地址在使用例外。 现在有两个问题:处理容器停止/重装事件
- 在Boot.scala中启动依赖服务是否正确?
- 无论如何,如何处理容器:停止事件?
在Boot.scala中有一个电梯应用程序启动ssh守护程序。 这里是问题:当我运行container:restart /
在sbt会话中我得到地址在使用例外。 现在有两个问题:处理容器停止/重装事件
我觉得电梯-Y的方式来做到这一点是与LiftRules.unloadHooks
。
它不充分证明(据我所知),但如果你在电梯源代码看,你会看到,当LiftServlet
是destroy()
版,在LiftRules.unloadHooks
定义的功能被执行。
您可以添加功能到unloadHooks
RulesSeq
与append
或prepend
方法,这取决于你希望他们在执行什么样的顺序所以,在你bootstrap.liftweb.Boot.boot
方法,你可能会做这样的事情:
sshDaemon.start()
LiftRules.unloadHooks.append(() => sshDaemon.stop())
(假设你是如何启动和停止你的SSH守护进程的。)
我不是100%肯定当sbt web-plugin的container:restart
命令运行时,这个方法被调用 - 这是由插件决定的d与Jetty的相互作用,而不是Lift - 但是container:stop
命令肯定可以做到这一点。
我对Lift并不熟悉,但是这个建议应该适用于任何基于Servlet的Web应用程序。
在您的web.xml
中注册一个ServletContextListener
,并释放contextDestroyed
方法中的所有资源。 (启动应在contextCreated
方法中完成。)
您可以使用setAttribute
/getAttribute
来存储并稍后检索服务器。
把所有这些组合起来:
import javax.servlet.{ServletContextEvent, ServletContextListener}
final class SshListener extends ServletContextListener{
val attributeKey = "sshServer"
def contextInitialized(sce: ServletContextEvent) {
val server = new Server()
server.start()
sce.getServletContext.setAttribute(attributeKey, server)
}
def contextDestroyed(sce: ServletContextEvent) {
Option(sce.getServletContext.getAttribute(attributeKey)).foreach(_.asInstanceOf[Server].stop())
}
}
class Server {
def start()
def stop()
}