Beispiel #1
0
        public void RegisterOnMemberUp()
        {
            Console.WriteLine(cluster.SelfAddress + "上线.....");

            var proxy = ClusterSharding.Get(actorSystem).StartProxy(
                typeName: "fzf003",
                role: "ClusterShardingApp",
                messageExtractor: new MessageExtractor(10));

            var random = new Random();


            actorSystem.Scheduler.Advanced.ScheduleRepeatedly(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(3), () =>
            {
                var Id            = random.Next(1, 10).ToString();
                var createproduct = new CreateProductCommand(Id, string.Format("fzf-{0}", Id));
                var message       = new ShardEnvelope(Id, createproduct);
                Console.WriteLine(createproduct.ToString());
                proxy.Tell(message);
                Console.WriteLine("打印:{0}", Id);
                proxy.Tell(new ShardEnvelope(Id, new PrintState(Id)));



                /*var state = ClusterSharding.Get(actorSystem).ShardRegion("fzf003").Ask<CurrentShardRegionState>(GetShardRegionState.Instance).Result;
                 * foreach (var shard in state.Shards)
                 *  foreach (var entityId in shard.EntityIds)
                 *      Console.WriteLine($"fzf003/{shard.ShardId}/{entityId}");
                 */
            });
        }
Beispiel #2
0
        private void UpdateState(object e)
        {
            e.Match().With <ChatCreatedEvent>(x =>
            {
                State = new ChatEntity(x.Id);
                State.Participants = x.Participants;
                Context.Become(Initialized);
                if (!IsRecovering)
                {
                    ResendToView(x);

                    foreach (var user in x.Participants)
                    {
                        var envelop = new ShardEnvelope(user.Id.ToString(), x);
                        _userRegion.Tell(envelop);
                    }
                }
            });

            e.Match().With <ChatMessageAddedEvent>(x =>
            {
                var chatLogEntity = new ChatLogEntity(x.MessageId, x.Author, x.Message, DateTime.UtcNow);
                State.Log.Add(chatLogEntity);
                if (!IsRecovering)
                {
                    ResendToView(x);
                    foreach (var user in State.Participants)
                    {
                        var envelop = new ShardEnvelope(user.Id.ToString(), x);
                        _userRegion.Tell(envelop);
                    }
                }
            });
        }
        public ActionResult Delete(string name)
        {
            var message = new ShardEnvelope(name, Customer.EmptyBasket.Instance);

            Actors.Instance.CustomerProxy.Tell(message, ActorRefs.NoSender);

            return(Ok());
        }
        public ActionResult Post([FromBody] AddItemRequest request, string name)
        {
            var message = new ShardEnvelope(name, new Customer.AddItem(request.Product, request.Quantity));

            Actors.Instance.CustomerProxy.Tell(message, ActorRefs.NoSender);

            return(Ok());
        }
        public void OrderItem(string id, string article, double quantity)
        {
            var message = new OrderItem {
                Article = article, Quantity = quantity
            };
            var envelope = new ShardEnvelope(id, message);

            router.Tell(envelope);
        }
        public async Task <ActionResult> Get(string name)
        {
            var message = new ShardEnvelope(name, Customer.ShowBasket.Instance);
            var timeout = TimeSpan.FromSeconds(10);

            var basket = await Actors.Instance.CustomerProxy.Ask <Customer.Basket>(message, timeout);

            return(Ok(basket));
        }
Beispiel #7
0
        private void Initialized(object message)
        {
            message.Match()
            .With <AddMessageToChatCommand>(mes =>
            {
                var envelop = new ShardEnvelope(mes.Author.ToString(), new GetUserById(mes.Author));
                GetUserByIdResult contactUser = _userRegion.Ask <GetUserByIdResult>(envelop).Result;

                Guid messageId    = Guid.NewGuid();
                var messaegeAdded = new ChatMessageAddedEvent(messageId, mes.ChatId,
                                                              DateTime.UtcNow, mes.Message, new ChatParticipant(contactUser.Id, contactUser.Login, contactUser.UserName));
                Persist(messaegeAdded, UpdateState);
            });
        }
        void Ready()
        {
            Context.System.EventStream.Subscribe(Self, typeof(UserRegisteredEvent));
            Context.System.EventStream.Subscribe(Self, typeof(SubscribedToUserEvent));

            Receive <RegisterModel>(register =>
            {
                var command  = new RegisterUserCommand(register.UserId, register.Login, register.UserName);
                var envelope = new ShardEnvelope(command.Id.ToString(), command);
                UserRegion.Tell(envelope);
            });

            Receive <CreateChatModel>(x =>
            {
                var command = new CreateChatCommand(x.ChatId, x.UserId, new List <Guid> {
                    x.UserId, x.TargetUserId
                });
                var envelope = new ShardEnvelope(command.Id.ToString(), command);
                ChatRegion.Tell(envelope);
            });

            Receive <UserRegisteredEvent>(evt =>
            {
                SignalrPusher.PlayerJoined(evt.Id, evt.Login, evt.UserName);
            });

            Receive <SubscribeToUserCommand>(mes =>
            {
                var envelope = new ShardEnvelope(mes.UserId.ToString(), mes);
                UserRegion.Tell(envelope);
            });

            Receive <SubscribedToUserEvent>(mes =>
            {
                SignalrPusher.UserAddedToContactList(mes.UserId, mes.ContactUserId, mes.ContactName);
            });

            Receive <AddMessageToChatCommand>(cmd =>
            {
                var envelope = new ShardEnvelope(cmd.ChatId.ToString(), cmd);
                ChatRegion.Tell(envelope);
            });

            Receive <MarkChatMessagesReadCommand>(cmd =>
            {
                var envelope = new ShardEnvelope(cmd.UserId.ToString(), cmd);
                UserRegion.Tell(envelope);
            });
        }
Beispiel #9
0
        private static void ProduceMessages(ActorSystem system, IActorRef shardRegion)
        {
            var customers = new[] { "Yoda", "Obi-Wan", "Darth Vader", "Princess Leia", "Luke Skywalker", "R2D2", "Han Solo", "Chewbacca", "Jabba", "Yodisoft" };
            var items     = new[] { "Yoghurt", "Fruits", "Lightsaber", "Fluffy toy", "Dreamcatcher", "Candies", "Cigars", "Chicken nuggets", "French fries" };

            system.Scheduler.Advanced.ScheduleRepeatedly(TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(10), () =>
            {
                var j = 0;
                while (j < 100)
                {
                    var customer = PickRandom(customers);
                    var item     = PickRandom(items);
                    var message  = new ShardEnvelope(customer, new PurchaseItem(item));

                    shardRegion.Tell(message);
                    j++;
                }
            });
        }
Beispiel #10
0
        void Initialized(object message)
        {
            message.Match()
            .With <ChangeUserNameEmailCommand>(cmd =>
            {
                var @event = new UserNameEmailChangedEvent(cmd.Id, cmd.Name, cmd.Email);
                Persist(@event, UpdateState);
            })
            .With <SubscribeToUserCommand>(cmd =>
            {
                if (State.Id != cmd.ContactUserId && !State.SubscribedToList.Any(x => x == cmd.ContactUserId))
                {
                    var envelop = new ShardEnvelope(cmd.ContactUserId.ToString(),
                                                    new GetUserById(cmd.ContactUserId));
                    GetUserByIdResult contactUser = _region.Ask <GetUserByIdResult>(envelop).Result;

                    var @event = new SubscribedToUserEvent(cmd.UserId, cmd.ContactUserId, contactUser.Login, contactUser.UserName);
                    Persist(@event, UpdateState);
                }
            })
            .With <GetUserById>(x =>
            {
                Sender.Tell(new GetUserByIdResult(State.Id, State.Login, State.UserName));
            })
            .With <ChatCreatedEvent>(x =>
            {
                ResendToViews(x);
            })
            .With <ChatMessageAddedEvent>(x =>
            {
                ResendToViews(x);
            })
            .With <MarkChatMessagesReadCommand>(x =>
            {
                var @event = new ChatMessagesMarkedReadEvent(x.UserId, x.ChatId);
                Persist(@event, UpdateState);
            });
        }
Beispiel #11
0
        private void UnInitialized(object message)
        {
            message.Match()
            .With <CreateChatCommand>(cmd =>
            {
                List <GetUserByIdResult> users = new List <GetUserByIdResult>();
                foreach (Guid cmdParticipantItem in cmd.Participants)
                {
                    var envelop = new ShardEnvelope(cmdParticipantItem.ToString(), new GetUserById(cmdParticipantItem));
                    GetUserByIdResult contactUser = _userRegion.Ask <GetUserByIdResult>(envelop).Result;
                    users.Add(contactUser);
                }

                List <ChatParticipant> chatParticipants =
                    users.Select(x => new ChatParticipant(x.Id, x.Login, x.UserName)).ToList();

                ChatParticipant creator = users.Where(x => x.Id == cmd.Creator)
                                          .Select(x => new ChatParticipant(x.Id, x.Login, x.UserName)).FirstOrDefault();

                var @event = new ChatCreatedEvent(cmd.Id, creator, chatParticipants);
                Persist <ChatCreatedEvent>(@event, UpdateState);
            });
        }
        public async Task <ShoppingBasket> GetOrderItemsForOrder(string orderId)
        {
            var envelope = new ShardEnvelope(orderId, new GetOrderItems());

            return(await router.Ask <ShoppingBasket>(envelope));
        }