I want to detect when my ActorSystem's Remoting fails to start. 
 Specifically, I want my program to System.exit(1) when that happens.  I 
can't figure out how to detect the failure, and the process doesn't exit 
after the failure.

Here's a test program:

import akka.actor.{Props, Actor, ActorSystem}
import com.typesafe.config.{ConfigFactory, Config}
object TestBindFailed {
  def main(argv: Array[String]) {
    try {
      val system = ActorSystem("Master", akkaConfig())
      system.eventStream.subscribe(
        subscriber = system.actorOf(Props[Watcher], "watcher"),
        channel = classOf[RemotingErrorEvent]
      )
    } catch {
      case exception: Exception => {
        System.err.println("main caught exception: " + exception)
      }
    }
  }
  private[this] def akkaConfig(): Config = {
    val config = ConfigFactory.parseString("""
      akka {
        actor {
          provider = "akka.remote.RemoteActorRefProvider"
        }
        remote {
          enabled-transports = [ "akka.remote.netty.tcp" ]
          netty {
            tcp {
              hostname = "localhost"
              port = 22
            }
          }
        }
      }
    """)
    return config.withFallback(ConfigFactory.load())
  }
}
class Watcher extends Actor {
  def receive = {
    case event: AnyRef => {
      System.err.println("Watcher received event: " + event)
    }
  }
}

This program uses port 22 only to demonstrate the problem.  I don't 
normally use that as my port number.  Normally you cannot bind port 22 on a 
Unix-like system because (1) normally you can't bind ports below 1024 
(unless you're root) and (2) normally sshd has already bound port 22.

The Watcher doesn't get any events.  I also tried subscribing it to AnyRef 
but it made no difference.

I'm running on Mac OS X 10.9.2, JDK 1.7.0_40, scala 2.10.3, akka 2.3.0.

Here's the output of the program:

[INFO] [03/05/2014 15:46:14.006] [main] [Remoting] Starting remoting
main caught exception: org.jboss.netty.channel.ChannelException: Failed to 
bind to: localhost/127.0.0.1:22
[ERROR] [03/05/2014 15:46:14.146] [main] [Remoting] Remoting error: 
[Startup failed] [
akka.remote.RemoteTransportException: Startup failed
at 
akka.remote.Remoting.akka$remote$Remoting$$notifyError(Remoting.scala:127)
at akka.remote.Remoting.start(Remoting.scala:192)
at akka.remote.RemoteActorRefProvider.init(RemoteActorRefProvider.scala:184)
at akka.actor.ActorSystemImpl.liftedTree2$1(ActorSystem.scala:617)
at akka.actor.ActorSystemImpl._start$lzycompute(ActorSystem.scala:615)
at akka.actor.ActorSystemImpl._start(ActorSystem.scala:615)
at akka.actor.ActorSystemImpl.start(ActorSystem.scala:632)
at akka.actor.ActorSystem$.apply(ActorSystem.scala:141)
at akka.actor.ActorSystem$.apply(ActorSystem.scala:118)
at TestBindFailed$.main(TestBindFailed.scala:9)
at TestBindFailed.main(TestBindFailed.scala)
Caused by: org.jboss.netty.channel.ChannelException: Failed to bind to: 
localhost/127.0.0.1:22
at org.jboss.netty.bootstrap.ServerBootstrap.bind(ServerBootstrap.java:272)
at 
akka.remote.transport.netty.NettyTransport$$anonfun$listen$1.apply(NettyTransport.scala:392)
at 
akka.remote.transport.netty.NettyTransport$$anonfun$listen$1.apply(NettyTransport.scala:389)
at scala.util.Success$$anonfun$map$1.apply(Try.scala:206)
at scala.util.Try$.apply(Try.scala:161)
at scala.util.Success.map(Try.scala:206)
at scala.concurrent.Future$$anonfun$map$1.apply(Future.scala:235)
at scala.concurrent.Future$$anonfun$map$1.apply(Future.scala:235)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
at 
akka.dispatch.BatchingExecutor$Batch$$anonfun$run$1.processBatch$1(BatchingExecutor.scala:67)
at 
akka.dispatch.BatchingExecutor$Batch$$anonfun$run$1.apply$mcV$sp(BatchingExecutor.scala:82)
at 
akka.dispatch.BatchingExecutor$Batch$$anonfun$run$1.apply(BatchingExecutor.scala:59)
at 
akka.dispatch.BatchingExecutor$Batch$$anonfun$run$1.apply(BatchingExecutor.scala:59)
at scala.concurrent.BlockContext$.withBlockContext(BlockContext.scala:72)
at akka.dispatch.BatchingExecutor$Batch.run(BatchingExecutor.scala:58)
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:41)
at 
akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:393)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at 
scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at 
scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
Caused by: java.net.SocketException: Permission denied
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:444)
at sun.nio.ch.Net.bind(Net.java:436)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at 
org.jboss.netty.channel.socket.nio.NioServerBoss$RegisterTask.run(NioServerBoss.java:193)
at 
org.jboss.netty.channel.socket.nio.AbstractNioSelector.processTaskQueue(AbstractNioSelector.java:372)
at 
org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:296)
at 
org.jboss.netty.channel.socket.nio.NioServerBoss.run(NioServerBoss.java:42)
at 
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at 
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
]
[INFO] [03/05/2014 15:46:14.151] 
[Master-akka.remote.default-remote-dispatcher-5] 
[akka://Master/system/remoting-terminator] Shutting down remote daemon.
[INFO] [03/05/2014 15:46:14.152] 
[Master-akka.remote.default-remote-dispatcher-5] 
[akka://Master/system/remoting-terminator] Remote daemon shut down; 
proceeding with flushing remote transports.
[INFO] [03/05/2014 15:46:14.158] [ForkJoinPool-3-worker-15] [Remoting] 
Remoting shut down
[INFO] [03/05/2014 15:46:14.159] 
[Master-akka.remote.default-remote-dispatcher-5] 
[akka://Master/system/remoting-terminator] Remoting shut down.




-- 

---------------------------------------------------------------
This email, along with any attachments, is confidential. If you 
believe you received this message in error, please contact the 
sender immediately and delete all copies of the message.  
Thank you.

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: http://akka.io/faq/
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to