awfulHack
awfulHack

Reputation: 875

running Scala with commons-daemon from jsvc

I'm trying to write a basic Scala application that can run as a daemon using commons-daemon. The following code was based on the java program in this post

package daemon

import org.apache.commons.daemon._ 
import java.util.{ Timer, TimerTask, Date }

class EchoTask extends TimerTask{
  def run() { println(new Date() + " running ...") }
}

object DaemonApp extends App with Daemon {
  val timer = new Timer();
  timer.schedule(new EchoTask(), 0, 1000); 

  def init(daemonContext: DaemonContext) {} 
  def start(){} 
  def stop(){} 
  def destroy(){}
}

this works fine when I run from sbt or build a jar. I can't get the code to run from jsvc though. for example if I run:

$ ./jsvc -cp $DAEMON_HOME/daemon.jar -pidfile $DAEMON_HOME/pidfile -errfile '&2' -outfile '&1' daemon.DaemonApp 

I'll get the following on stderr

java.lang.NoSuchMethodException: daemon.DaemonApp.init([Ljava.lang.String;)
    at java.lang.Class.getMethod(Class.java:1670)
    at org.apache.commons.daemon.support.DaemonLoader.load(DaemonLoader.java:176)
Cannot load daemon
Service exit with a return value of 3

I don't understand that whats going on with the method it's looking for, the init(Array[String]). Is there something in the way scalac is compiling the main class that is making the code incompatible with the daemon interface?

Thanks!

Upvotes: 2

Views: 913

Answers (2)

jimKaravias
jimKaravias

Reputation: 11

The daemon should be implemented as a class in Scala, not an object. I ran into this when I coded the daemon in Scala as an object to make it easy to run in the IDE.

Upvotes: 1

ppopoff
ppopoff

Reputation: 678

I faced the same problem. And looks like the solution is implementing this class in java. After that I've got things working.

Upvotes: 0

Related Questions