【问题标题】:Correct way to kill a session in a distributed system在分布式系统中终止会话的正确方法
【发布时间】:2015-04-13 19:38:00
【问题描述】:

我正在开发一个使用 AKKA 框架并在 dropwizard 服务器上运行的分布式 java 项目。我在应用程序启动时创建一个会话,并通过以下方式在我的所有参与者中使用此会话

MyApplication.java

    public void run() {
      final Session actorSession = hibernate.getSessionFactory()
            .openSession();
     // pass this session to various actors
      final ActorSystem system = ActorSystem.create("myActorSystem");
      environment.lifecycle().manage(new ActorSystemGuardian(system));

      system.registerOnTermination(new Runnable() {
            public void run() {
            //Clean all resources after actor termination
            actorSession.close();
        }

ActorSystemGuardan.java

  public class ActorSystemGuardian implements Managed {
    private ActorSystem system;
    public ActorSystemManager(ActorSystem system) {
      this.system = system;
    }

    public void start() throws Exception {

    }

    public void stop() throws Exception {
      system.shutdown();

   }

}

这是否确保正确清理所有资源? actorSession 会关闭吗?在应用程序的整个生命周期中,所有参与者都需要使用相同的会话。有没有办法让这个会话持久化?

【问题讨论】:

    标签: mysql session shutdown dropwizard broken-pipe


    【解决方案1】:

    我通过以下方式传递 sessionFactory 而不是会话来解决问题

    final SessionFactory actorSessionFactory = hibernate.getSessionFactory()
    

    然后可以使用该工厂来生成可以(应该)在使用后关闭的临时会话。

    system.registerOnTermination(new Runnable() {
            public void run() {
            //Clean all resources after actor termination
    
        }
    

    system.registerOnTermination(new Runnable) 仅在actor系统关闭且不应与会话绑定时调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-19
      • 2014-09-01
      • 2013-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多