Boot.scala で ssh デーモンを起動するリフト アプリ。問題は次のとおりです。sbtcontainer:restart /
セッションで実行すると、 Address already in use という例外が発生します。今2つの質問:
- Boot.scala で依存サービスを開始するのは正しい方法ですか?
- とにかく、container:stop イベントをどのように処理できますか?
Boot.scala で ssh デーモンを起動するリフト アプリ。問題は次のとおりです。sbtcontainer:restart /
セッションで実行すると、 Address already in use という例外が発生します。今2つの質問:
それを行うLift-yの方法はLiftRules.unloadHooks
.
十分に文書化されているわけではありませんが (AFAIK)、Lift のソース コードを見ると、LiftServlet
がdestroy()
ed のときに で定義された関数LiftRules.unloadHooks
が実行されることがわかります。
関数を実行する順序に応じてunloadHooks
RulesSeq
、append
またはメソッドを使用して関数を追加できます。そのため、メソッドでは、次のようにすることができます。prepend
bootstrap.liftweb.Boot.boot
sshDaemon.start()
LiftRules.unloadHooks.append( () => sshDaemon.stop() )
(それが SSH デーモンを開始および停止した方法であると仮定します。)
LiftServlet.destroy()
sbt web-plugin のコマンドが実行されたときにメソッドが呼び出されるとは 100% 確信が持てcontainer:restart
ませんが、それはプラグインと、Lift ではなく Jetty との相互作用によって決定されますが、container:stop
コマンドは確実に機能するはずです。
私は Lift に詳しくありませんが、このアドバイスは、サーブレット ベースの Web アプリケーションならどれでも当てはまるはずです。
に を登録し、メソッド内のすべてのリソースを解放ServletContextListener
します。(起動はメソッド内で行う必要があります。)web.xml
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()
}