=clu #18245 Improve registerOnMemberRemoved example

This commit is contained in:
Patrik Nordwall 2015-09-10 08:48:50 +02:00
parent 569b18d56e
commit 13f5b255f3
5 changed files with 40 additions and 19 deletions

View file

@ -339,7 +339,7 @@ class Cluster(val system: ExtendedActorSystem) extends Extension {
/** /**
* The supplied thunk will be run, once, when current cluster member is `Removed`. * The supplied thunk will be run, once, when current cluster member is `Removed`.
* and if the cluster have been shutdown,that thunk will run on the caller thread immediately. * If the cluster has already been shutdown the thunk will run on the caller thread immediately.
* Typically used together `cluster.leave(cluster.selfAddress)` and then `system.shutdown()`. * Typically used together `cluster.leave(cluster.selfAddress)` and then `system.shutdown()`.
*/ */
def registerOnMemberRemoved[T](code: T): Unit = def registerOnMemberRemoved[T](code: T): Unit =
@ -347,7 +347,7 @@ class Cluster(val system: ExtendedActorSystem) extends Extension {
/** /**
* Java API: The supplied thunk will be run, once, when current cluster member is `Removed`. * Java API: The supplied thunk will be run, once, when current cluster member is `Removed`.
* and if the cluster have been shutdown,that thunk will run on the caller thread immediately. * If the cluster has already been shutdown the thunk will run on the caller thread immediately.
* Typically used together `cluster.leave(cluster.selfAddress)` and then `system.shutdown()`. * Typically used together `cluster.leave(cluster.selfAddress)` and then `system.shutdown()`.
*/ */
def registerOnMemberRemoved(callback: Runnable): Unit = { def registerOnMemberRemoved(callback: Runnable): Unit = {

View file

@ -333,8 +333,7 @@ How To Cleanup when Member is Removed
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You can do some clean up in a ``registerOnMemberRemoved`` callback, which will You can do some clean up in a ``registerOnMemberRemoved`` callback, which will
be invoked when the current member status is changed to 'Removed' or the cluster have been shutdown,i.e. be invoked when the current member status is changed to 'Removed' or the cluster have been shutdown.
terminate the actor system.
For example, this is how to shut down the ``ActorSystem`` and thereafter exit the JVM: For example, this is how to shut down the ``ActorSystem`` and thereafter exit the JVM:

View file

@ -327,8 +327,7 @@ How To Cleanup when Member is Removed
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You can do some clean up in a ``registerOnMemberRemoved`` callback, which will You can do some clean up in a ``registerOnMemberRemoved`` callback, which will
be invoked when the current member status is changed to 'Removed' or the cluster have been shutdown,i.e. be invoked when the current member status is changed to 'Removed' or the cluster have been shutdown.
terminate the actor system.
For example, this is how to shut down the ``ActorSystem`` and thereafter exit the JVM: For example, this is how to shut down the ``ActorSystem`` and thereafter exit the JVM:

View file

@ -1,8 +1,10 @@
package sample.cluster.factorial; package sample.cluster.factorial;
import java.util.concurrent.TimeUnit;
import scala.concurrent.duration.FiniteDuration; import java.util.concurrent.TimeoutException;
import java.util.concurrent.TimeUnit;
import scala.concurrent.Await;
import scala.concurrent.duration.Duration;
import com.typesafe.config.Config; import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory; import com.typesafe.config.ConfigFactory;
@ -38,18 +40,29 @@ public class FactorialFrontendMain {
public void run() { public void run() {
// exit JVM when ActorSystem has been terminated // exit JVM when ActorSystem has been terminated
final Runnable exit = new Runnable() { final Runnable exit = new Runnable() {
@Override @Override public void run() {
public void run() { System.exit(0);
System.exit(-1);
} }
}; };
system.registerOnTermination(exit); system.registerOnTermination(exit);
// in case ActorSystem shutdown takes longer than 10 seconds,
// exit the JVM forcefully anyway
system.scheduler().scheduleOnce(FiniteDuration.create(10, TimeUnit.SECONDS),
exit, system.dispatcher());
// shut down ActorSystem // shut down ActorSystem
system.terminate(); system.terminate();
// In case ActorSystem shutdown takes longer than 10 seconds,
// exit the JVM forcefully anyway.
// We must spawn a separate thread to not block current thread,
// since that would have blocked the shutdown of the ActorSystem.
new Thread() {
@Override public void run(){
try {
Await.ready(system.whenTerminated(), Duration.create(10, TimeUnit.SECONDS));
} catch (Exception e) {
System.exit(-1);
}
}
}.start();
} }
}); });
//#registerOnRemoved //#registerOnRemoved

View file

@ -9,6 +9,8 @@ import akka.actor.Props
import akka.cluster.Cluster import akka.cluster.Cluster
import akka.routing.FromConfig import akka.routing.FromConfig
import akka.actor.ReceiveTimeout import akka.actor.ReceiveTimeout
import scala.util.Try
import scala.concurrent.Await
//#frontend //#frontend
class FactorialFrontend(upToN: Int, repeat: Boolean) extends Actor with ActorLogging { class FactorialFrontend(upToN: Int, repeat: Boolean) extends Actor with ActorLogging {
@ -61,12 +63,20 @@ object FactorialFrontend {
//#registerOnRemoved //#registerOnRemoved
Cluster(system).registerOnMemberRemoved { Cluster(system).registerOnMemberRemoved {
// exit JVM when ActorSystem has been terminated // exit JVM when ActorSystem has been terminated
system.registerOnTermination(System.exit(-1)) system.registerOnTermination(System.exit(0))
// in case ActorSystem shutdown takes longer than 10 seconds,
// exit the JVM forcefully anyway
system.scheduler.scheduleOnce(10.seconds)(System.exit(-1))(system.dispatcher)
// shut down ActorSystem // shut down ActorSystem
system.terminate() system.terminate()
// In case ActorSystem shutdown takes longer than 10 seconds,
// exit the JVM forcefully anyway.
// We must spawn a separate thread to not block current thread,
// since that would have blocked the shutdown of the ActorSystem.
new Thread {
override def run(): Unit = {
if (Try(Await.ready(system.whenTerminated, 10.seconds)).isFailure)
System.exit(-1)
}
}.start()
} }
//#registerOnRemoved //#registerOnRemoved