Documentation

You are viewing the documentation for the 2.8.15 release in the 2.8.x series of releases. The latest stable release series is 3.0.x.

§Cluster Sharding for Akka Typed (incubating)

Play provides an incubating module for integration with Akka Cluster Sharding Typed. To enable this module, add the following dependency to your build:

Java
libraryDependencies += javaClusterSharding
Scala
libraryDependencies += clusterSharding

§Usage

After having properly included Cluster Sharding as a dependency, you can obtain an instance by using dependency injection. We’ve provided some helpers for both runtime and compile-time dependency injection.

Note that Play is only providing the DI mechanism. The class instance that will be made available for injection is Akka’s akka.cluster.sharding.typed.javadsl.ClusterSharding for Java and akka.cluster.sharding.typed.scaladsl.ClusterSharding for Scala.

§Runtime dependency injection

Runtime dependency injection works as any other runtime DI module in Play, meaning that adding the dependency enables the module automatically, and an instance is available for injection.

§Compile-time dependency injection

If you’re using compile-time DI, you can get have access to the ClusterSharding by using the components like below:

Java
import play.Application;
import play.ApplicationLoader;
import play.BuiltInComponentsFromContext;
import play.controllers.AssetsComponents;
import play.routing.Router;
import play.cluster.sharding.typed.ClusterShardingComponents;
import play.filters.components.HttpFiltersComponents;

public class ComponentsWithClusterSharding extends BuiltInComponentsFromContext
    implements ClusterShardingComponents, AssetsComponents, HttpFiltersComponents {

  public ComponentsWithClusterSharding(ApplicationLoader.Context context) {
    super(context);
  }

  @Override
  public Router router() {
    return Router.empty();
  }
}
Scala
import play.api._
import play.api.ApplicationLoader.Context
import play.api.routing.Router
import play.api.cluster.sharding.typed.ClusterShardingComponents

class MyApplicationLoader extends ApplicationLoader {
  def load(context: Context) = {
    new ComponentsWithClusterSharding(context).application
  }
}

class ComponentsWithClusterSharding(context: Context)
    extends BuiltInComponentsFromContext(context)
    with play.filters.HttpFiltersComponents
    with ClusterShardingComponents {
  lazy val router = Router.empty
}

§Cluster Formation

When including this module, the application ActorSystem will be configured for a clustered environment. As a result, it will start Akka Remote and bind it, by default, to port 25520 (see Akka docs for how to configure a different port).

In addition to that, it is expected that your application’s Actor System forms a cluster with other instances of your application. Please consult Akka’s documentation on how to form an Akka Cluster.

Next: Contributing to Play