0

我想运行一个 Akka 应用程序,该应用程序在机器启动时在带有 systemd 的系统上自动运行和重新启动。我一直在尝试使用 apache daemon/jscv 来做到这一点,但我遇到了一些问题。现在我只是想运行这个东西(第一步),但这已经失败了。我下面的代码

object Launcher extends Daemon {

  val system = ActorSystem("testsystem")
  var schedule: Cancellable = new Cancellable{
    def cancel(): Unit = ()
    def isCancelled: Boolean = true
  }

  def roll(rand: Random) = () => {
    rand.nextInt(6)
  }

  val selection = system.actorSelection("mytestactor")


  def init(context: DaemonContext) = {
    val ref = system.actorOf(Props[TestActor], "mytestactor")
  }

  def start = {
    import scala.concurrent.ExecutionContext.Implicits.global
    val jrand = new Random()
    val roller = roll(jrand)
    schedule = system.scheduler.schedule(Duration(0, SECONDS), Duration(2, SECONDS))( () => selection ! roller() )
  }



  def stop = {
    if (!schedule.isCancelled) {
      schedule.cancel()
    }
  }

  def destroy = {
    system.shutdown()
  }

}

class TestActor extends Actor {

  def receive = {
    case 0 => println("rolled zero")
    case n: Int if n < 6 => println("rolling")
  }
}

我尝试将其打包,sbt assembly然后尝试使用

当我尝试启动它时

sudo jsvc -outfile ~/out.txt -errfile ~/err.txt -cp commons-daemon.jar:akkadaemon.jar Launcher

但似乎不起作用;err.txt包含

java.lang.ClassNotFoundException: Launcher
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at org.apache.commons.daemon.support.DaemonLoader.load(DaemonLoader.java:151)
Cannot load daemon
Service exit with a return value of 3

我应该怎么做才能让这种方法发挥作用?这至少是一个可行的方向(当我想在它开始工作后编写 systemd 脚本时),还是我看错了?

4

1 回答 1

0

要使用 jsvc,请使用完全限定的类名,并使用普通类,而不是对象。启动器调用看起来像

sudo jsvc -outfile ~/out.txt -errfile ~/err.txt -cp commons-daemon.jar:akkadaemon.jar Fully.Qualified.Name.Of.Launcher
于 2014-08-26T12:19:26.987 回答