将akka作为systemd作业运行

时间:2014-07-08 10:51:12

标签: scala akka daemon systemd jsvc

我想运行一个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

如何让这种方法发挥作用?这至少是一个可行的方向(当我想在系统脚本运行时编写系统脚本时),或者我看错了吗?

1 个答案:

答案 0 :(得分:0)

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

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