Documentation

You are viewing the documentation for the 3.0.5 release. The latest stable release series is 3.0.x.

§Cluster Sharding for Pekko Typed (incubating)

Play provides an incubating module for integration with Pekko 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 Pekko’s pekko.cluster.sharding.typed.javadsl.ClusterSharding for Java and pekko.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.ApplicationLoader;
import play.BuiltInComponentsFromContext;
import play.cluster.sharding.typed.ClusterShardingComponents;
import play.controllers.AssetsComponents;
import play.filters.components.HttpFiltersComponents;
import play.routing.Router;

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.cluster.sharding.typed.ClusterShardingComponents
import play.api.routing.Router
import play.api.ApplicationLoader.Context

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 Pekko Remote and bind it, by default, to port 17355 (see Pekko 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 Pekko’s documentation on how to form an Pekko Cluster.

Next: Contributing to Play