Example #1
0
        /// <summary>
        /// The async-way subscriber
        /// </summary>
        /// <returns></returns>
        private async Task SubscriberAsync()
        {
            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            using (IConnection conn = cf.CreateConnection(opts))
            {
                //subscribe to the subject
                IPassiveSubscription sub = conn.SubscribePassive("The.>");

                //waits the message
                MsgOut m = await sub.NextMessageAsync(CancellationToken.None);

                //verify the expectation
                Console.WriteLine("Async received: " + m.GetPayloadAsString());
                Debug.Assert(m.GetPayloadAsString() == TestPayload);
            }
        }
Example #2
0
        private void SubscriberSync()
        {
            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            using (IConnection conn = cf.CreateConnection(opts))
            {
                IPassiveSubscription sub = conn.SubscribePassive("The.>");

                //instructs the subscriber to self-destroy after
                //exactly MessageCount messages
                sub.AutoUnsubscribe(MessageCount);

                int count = MessageCount;
                while (sub.IsValid)
                {
                    try
                    {
                        //waits for the next message
                        MsgOut m = sub.NextMessage(CancellationToken.None);
                        Console.WriteLine("Sync received: " + m.GetPayloadAsString());
                        Debug.Assert(m.GetPayloadAsString() == TestPayload);
                    }
                    catch (NATSBadSubscriptionException)
                    {
                        //that should be raised by the subscriber,
                        //because the current thread is stick to NextMessage method
                        //but the subscription is actually being disposed
                        break;
                    }
                    catch
                    {
                        //any other exception should be thrown elsewhere
                        throw;
                    }
                    count--;
                }

                Console.WriteLine("Unsubscribed");
                Debug.Assert(count == 0);
            }
        }
        private void Slave()
        {
            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            using (IConnection conn = cf.CreateConnection(opts))
            {
                IPassiveSubscription sub = conn.SubscribePassive("The.>");

                //waits for a request
                MsgOut m = sub.NextMessage(CancellationToken.None);
                Console.WriteLine("Slave received: " + m.GetPayloadAsString());

                //builds the response up, then publish it back as reply
                this._expectedResponsePayload = "Hello " + m.GetPayloadAsString() + "!";
                conn.Publish(
                    new MsgIn(m.ReplyTo).SetPayload(this._expectedResponsePayload),
                    CancellationToken.None
                    );
            }
        }
        private void SubscriberSync()
        {
            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            using (IConnection conn = cf.CreateConnection(opts))
            {
                IPassiveSubscription sub = conn.SubscribePassive("The.>");

                //waits for a message
                MsgOut m = sub.NextMessage(CancellationToken.None);
                Console.WriteLine("Sync received: " + m.GetPayloadAsString());
                Debug.Assert(m.GetPayloadAsString() == TestPayload);

                //unsubscribes the subject
                sub.Unsubscribe();
                Task.Delay(1000).Wait();

                //releases the semaphore
                this._sem.Release();
            }
        }