From 5294705373c16b846c6bf5969591a9b6f0a01510 Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Wed, 19 Jun 2019 11:46:51 +0200 Subject: [PATCH] roll 3 * custom ShardingMessageExtractor can be removed --- .../main/scala/sample/sharding/Device.scala | 49 ++----------------- 1 file changed, 5 insertions(+), 44 deletions(-) diff --git a/akka-sample-sharding-typed-scala/src/main/scala/sample/sharding/Device.scala b/akka-sample-sharding-typed-scala/src/main/scala/sample/sharding/Device.scala index 66915aa9..0196f206 100644 --- a/akka-sample-sharding-typed-scala/src/main/scala/sample/sharding/Device.scala +++ b/akka-sample-sharding-typed-scala/src/main/scala/sample/sharding/Device.scala @@ -3,9 +3,6 @@ package sample.sharding import akka.actor.typed.ActorSystem import akka.actor.typed.Behavior import akka.actor.typed.scaladsl.Behaviors -import akka.cluster.sharding.typed.HashCodeMessageExtractor -import akka.cluster.sharding.typed.ShardingEnvelope -import akka.cluster.sharding.typed.ShardingMessageExtractor import akka.cluster.sharding.typed.scaladsl.ClusterSharding import akka.cluster.sharding.typed.scaladsl.Entity import akka.cluster.sharding.typed.scaladsl.EntityTypeKey @@ -20,47 +17,11 @@ object Device { def init(system: ActorSystem[_]): Unit = { - val messageExtractor = - new ShardingMessageExtractor[Any, Device.RecordTemperature] { - - // Note that `HashCodeMessageExtractor` is using - // `(math.abs(id.hashCode) % numberOfShards).toString`. - // If the old Untyped nodes were using a different hashing function - // this delegate HashCodeMessageExtractor can't be used and - // same hashing function as before must be implemented here. - // `akka.cluster.sharding.typed.HashCodeMessageExtractor` is compatible - // with `akka.cluster.sharding.ShardRegion.HashCodeMessageExtractor`. - val delegate = new HashCodeMessageExtractor[Device.RecordTemperature]( - system.settings.config - .getInt("akka.cluster.sharding.number-of-shards") - ) - - override def entityId(message: Any): String = { - message match { - case Device.RecordTemperature(deviceId, _) => - deviceId.toString - case env: ShardingEnvelope[Device.RecordTemperature] => - delegate.entityId(env) - } - } - - override def shardId(entityId: String): String = { - delegate.shardId(entityId) - } - - override def unwrapMessage(message: Any): RecordTemperature = { - message match { - case m: Device.RecordTemperature => m - case env: ShardingEnvelope[Device.RecordTemperature] => - delegate.unwrapMessage(env) - } - } - } - - ClusterSharding(system).init( - Entity(TypeKey, _ => Device()) - .withMessageExtractor(messageExtractor) - ) + // If the original hashing function was using + // `(math.abs(id.hashCode) % numberOfShards).toString` + // the default HashCodeMessageExtractor in Typed can be used. + // That is also compatible with `akka.cluster.sharding.ShardRegion.HashCodeMessageExtractor`. + ClusterSharding(system).init(Entity(TypeKey, _ => Device())) } case class RecordTemperature(deviceId: Int, temperature: Double)