You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pekko.apache.org by fa...@apache.org on 2022/10/31 09:34:45 UTC

[incubator-pekko-samples] 05/09: roll 3 - scala

This is an automated email from the ASF dual-hosted git repository.

fanningpj pushed a commit to branch wip-rolling-update-typed-patriknw
in repository https://gitbox.apache.org/repos/asf/incubator-pekko-samples.git

commit b27976b9454e700bb233cf4924976167a07dad44
Author: Patrik Nordwall <pa...@gmail.com>
AuthorDate: Wed Jun 19 11:46:51 2019 +0200

    roll 3 - scala
    
    * custom ShardingMessageExtractor can be removed
---
 .../src/main/scala/sample/sharding/Device.scala    | 52 +++-------------------
 1 file changed, 5 insertions(+), 47 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 6854b4d..162e43e 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
@@ -4,9 +4,6 @@ import akka.actor.typed.ActorRef
 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
@@ -21,50 +18,11 @@ object Device {
 
   def init(system: ActorSystem[_]): Unit = {
 
-    val messageExtractor =
-      new ShardingMessageExtractor[Any, Command] {
-
-        // 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.Command](
-          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 Device.GetTemperature(deviceId, _) =>
-              deviceId.toString
-            case env: ShardingEnvelope[Device.Command] =>
-              delegate.entityId(env)
-          }
-        }
-
-        override def shardId(entityId: String): String = {
-          delegate.shardId(entityId)
-        }
-
-        override def unwrapMessage(message: Any): Command = {
-          message match {
-            case m: Device.RecordTemperature => m
-            case m: Device.GetTemperature    => 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()))
   }
 
   sealed trait Command extends Message


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pekko.apache.org
For additional commands, e-mail: commits-help@pekko.apache.org