Merge pull request #19229 from akka/wip-sharding-leaving-patriknw

=cls improve the graceful shutdown example
This commit is contained in:
Roland Kuhn 2015-12-20 18:48:21 +01:00
commit 6cb350314d
2 changed files with 24 additions and 5 deletions

View file

@ -3,6 +3,7 @@
*/
package akka.cluster.sharding
import scala.concurrent.duration._
import java.io.File
import akka.actor._
@ -49,8 +50,16 @@ object ClusterShardingGracefulShutdownSpec {
region ! ShardRegion.GracefulShutdown
case Terminated(`region`)
cluster.registerOnMemberRemoved(system.terminate())
cluster.registerOnMemberRemoved(self ! "member-removed")
cluster.leave(cluster.selfAddress)
case "member-removed"
// Let singletons hand over gracefully before stopping the system
import context.dispatcher
system.scheduler.scheduleOnce(3.seconds, self, "stop-system")
case "stop-system"
system.terminate()
}
}
//#graceful-shutdown

View file

@ -4,7 +4,7 @@
package akka.cluster.sharding;
import java.util.concurrent.TimeUnit;
import static java.util.concurrent.TimeUnit.SECONDS;
import scala.concurrent.duration.Duration;
import akka.actor.AbstractActor;
@ -139,7 +139,7 @@ public class ClusterShardingTest {
@Override
public void preStart() throws Exception {
super.preStart();
context().setReceiveTimeout(Duration.create(120, TimeUnit.SECONDS));
context().setReceiveTimeout(Duration.create(120, SECONDS));
}
void updateState(CounterChanged event) {
@ -198,9 +198,19 @@ public class ClusterShardingTest {
region.tell(ShardRegion.gracefulShutdownInstance(), self());
}).
match(Terminated.class, t -> t.actor().equals(region), t -> {
cluster.registerOnMemberRemoved(() -> system.terminate());
cluster.registerOnMemberRemoved(() ->
self().tell("member-removed", self()));
cluster.leave(cluster.selfAddress());
}).build());
}).
match(String.class, s -> s.equals("member-removed"), s -> {
// Let singletons hand over gracefully before stopping the system
context().system().scheduler().scheduleOnce(Duration.create(3, SECONDS),
self(), "stop-system", context().dispatcher(), self());
}).
match(String.class, s -> s.equals("stop-system"), s -> {
system.terminate();
}).
build());
}
}
//#graceful-shutdown