Example #1
0
        private IActorRef GetShard(ShardId id)
        {
            //TODO: change on ConcurrentDictionary.GetOrAdd?
            IActorRef region = null;

            if (!Shards.TryGetValue(id, out region))
            {
                if (EntityProps == null || EntityProps.Equals(Actor.Props.Empty))
                {
                    throw new IllegalStateException("Shard must not be allocated to a proxy only ShardRegion");
                }
                else if (ShardsByRef.Values.All(shardId => shardId != id))
                {
                    Log.Debug("Starting shard [{0}] in region", id);

                    //val name = URLEncoder.encode(id, "utf-8")
                    var name     = Uri.EscapeDataString(id);
                    var shardRef = Context.Watch(Context.ActorOf(PersistentShard.Props(
                                                                     TypeName,
                                                                     id,
                                                                     EntityProps,
                                                                     Settings,
                                                                     IdExtractor,
                                                                     ShardResolver,
                                                                     HandOffStopMessage).WithDispatcher(Context.Props.Dispatcher), name));

                    ShardsByRef = ShardsByRef.SetItem(shardRef, id);
                    return(shardRef);
                }
            }

            return(region ?? ActorRefs.Nobody);
        }