private void Subscribing()
 {
     ReceiveAsync <DoSubscribe>(async _ =>
     {
         try
         {
             var ack = await _subscriptionManager.Subscribe(_tickerSymbol, TradeEventType.Fill, Self);
             Become(Asking);
             _askInterval = Context.System.Scheduler.ScheduleTellRepeatedlyCancelable(TimeSpan.FromSeconds(1),
                                                                                      TimeSpan.FromSeconds(10), Self, DoAsk.Instance, ActorRefs.NoSender);
         }
         catch (Exception ex)
         {
             _log.Error(ex, "Error while waiting for SubscribeAck for [{0}-{1}] - retrying in 5s.", _tickerSymbol, TradeEventType.Fill);
             Context.System.Scheduler.ScheduleTellOnce(TimeSpan.FromSeconds(5), Self, DoSubscribe.Instance, ActorRefs.NoSender);
         }
     });
 }
        private void Commands()
        {
            Command <ConfirmableMessage <Ask> >(a =>
            {
                // For the sake of efficiency - update orderbook and then persist all events
                var confirmation = new Confirmation(a.ConfirmationId, PersistenceId);
                var ask          = a.Message;
                ProcessAsk(ask, confirmation);
            });

            Command <Ask>(a =>
            {
                ProcessAsk(a, null);
            });

            Command <ConfirmableMessage <Bid> >(b =>
            {
                // For the sake of efficiency -update orderbook and then persist all events
                var confirmation = new Confirmation(b.ConfirmationId, PersistenceId);
                var bid          = b.Message;
                ProcessBid(bid, confirmation);
            });

            Command <Bid>(b =>
            {
                ProcessBid(b, null);
            });

            /*
             * Handle subscriptions directly in case we're using in-memory, local pub-sub.
             */
            CommandAsync <TradeSubscribe>(async sub =>
            {
                try
                {
                    var ack = await _subscriptionManager.Subscribe(sub.StockId, sub.Events, sub.Subscriber);
                    Context.Watch(sub.Subscriber);
                    sub.Subscriber.Tell(ack);
                    Sender.Tell(ack);     // need this for ASK operations.
                }
                catch (Exception ex)
                {
                    _log.Error(ex, "Error while processing subscription {0}", sub);
                    sub.Subscriber.Tell(new TradeSubscribeNack(sub.StockId, sub.Events, ex.Message));
                }
            });

            CommandAsync <TradeUnsubscribe>(async unsub =>
            {
                try
                {
                    var ack = await _subscriptionManager.Unsubscribe(unsub.StockId, unsub.Events, unsub.Subscriber);
                    // leave DeathWatch intact, in case actor is still subscribed to additional topics
                    unsub.Subscriber.Tell(ack);
                    Sender.Tell(ack); // need this for ASK operations.
                }
                catch (Exception ex)
                {
                    _log.Error(ex, "Error while processing unsubscribe {0}", unsub);
                    unsub.Subscriber.Tell(new TradeUnsubscribeNack(unsub.StockId, unsub.Events, ex.Message));
                }
            });

            CommandAsync <Terminated>(async t =>
            {
                try
                {
                    var ack = await _subscriptionManager.Unsubscribe(TickerSymbol, t.ActorRef);
                }
                catch (Exception ex)
                {
                    _log.Error(ex, "Error while processing unsubscribe for terminated subscriber {0} for symbol {1}", t.ActorRef, TickerSymbol);
                }
            });

            Command <SaveSnapshotSuccess>(s =>
            {
                // clean-up prior snapshots and journal events
                DeleteSnapshots(new SnapshotSelectionCriteria(s.Metadata.SequenceNr - 1));
                DeleteMessages(s.Metadata.SequenceNr);
            });

            Command <GetOrderBookSnapshot>(s =>
            {
                Sender.Tell(_matchingEngine.GetSnapshot());
            });
        }