Ejemplo n.º 1
0
        private void Publisher()
        {
            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            using (IConnection conn = cf.CreateConnection(opts))
            {
                //sends a message...
                Console.WriteLine("Publishing: " + TestPayload);
                conn.Publish(
                    new MsgIn("The.Target").SetPayload(TestPayload),
                    CancellationToken.None
                    );

                //...then waits the semaphore...
                this._sem.Wait();
                this._sem.Dispose();

                //...then sends another one
                conn.Publish(
                    new MsgIn("The.Target").SetPayload(TestPayload),
                    CancellationToken.None
                    );
            }
        }
Ejemplo n.º 2
0
        private void PublisherAuthorized()
        {
            var endPoint = ServerEndPoint.WithCredentials(
                Defaults.Address,
                Defaults.Port,
                userName: "******",
                password: "******",
                secured: false
                );

            ClientOptions opts = ConnectionUtils.GetDefaultOptions();

            opts.AddServer(endPoint);

            ConnectionFactory cf = new ConnectionFactory();

            using (IConnection conn = cf.CreateConnection(opts))
            {
                //sends a single message having a string as payload
                Console.WriteLine("Authorized is publishing...");
                conn.Publish(
                    new MsgIn("The.Target").SetPayload("the message"),
                    CancellationToken.None
                    );
            }
        }
Ejemplo n.º 3
0
        void IReqRepImpl.SetupConnection(ClientContextBase ctx)
        {
            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            ctx.Connection = cf.CreateConnection(opts);
        }
Ejemplo n.º 4
0
        private async Task ProducingWorkerAsync(MyWorkerContext ctx)
        {
            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            using (IConnection conn = await cf.CreateConnectionAsync(opts, CancellationToken.None))
            {
                Workers.RunPublisher(conn, ctx);
            }
        }
Ejemplo n.º 5
0
        private async Task ConsumingWorkerAsync(MyWorkerContext ctx)
        {
            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            using (IConnection conn = await cf.CreateConnectionAsync(opts, CancellationToken.None))
            {
                await Workers.RunParallelPassiveConsumerAsync(conn, ctx, ConsumingAgentCount);

                //Workers.RunParallelReactiveConsumer(conn, ctx, ConsumingAgentCount);
            }
        }
Ejemplo n.º 6
0
        private void ConsumingWorker(object state)
        {
            var ctx = (MyWorkerContext)state;

            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            using (IConnection conn = cf.CreateConnection(opts))
            {
                Workers.RunParallelPassiveConsumer(conn, ctx, ConsumingAgentCount);
                //Workers.RunParallelReactiveConsumer(conn, ctx, ConsumingAgentCount);
            }
        }
Ejemplo n.º 7
0
        private void ProducingWorker(object state)
        {
            var ctx = (MyWorkerContext)state;

            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            using (IConnection conn = cf.CreateConnection(opts))
            {
                Thread.Sleep(1000);
                Workers.RunPublisher(conn, ctx);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// The async-handler subscriber
        /// </summary>
        /// <returns></returns>
        private void Subscriber2()
        {
            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            using (IConnection conn = cf.CreateConnection(opts))
            {
                //subscribe to the subject
                IReactiveSubscription sub = conn.SubscribeAsyncReactive("The.>", Sub2HandlerAsync);

                //waits here until the semaphore will be released
                _sem2.Wait();
            }
        }
Ejemplo n.º 9
0
        private void Publisher()
        {
            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            using (IConnection conn = cf.CreateConnection(opts))
            {
                //sends a single message having a string as payload
                Console.WriteLine("Publishing: " + TestPayload);
                conn.Publish(
                    new MsgIn("The.Target").SetPayload(TestPayload),
                    CancellationToken.None
                    );
            }
        }
Ejemplo n.º 10
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);
            }
        }
Ejemplo n.º 11
0
        private void Master()
        {
            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            using (IConnection conn = cf.CreateConnection(opts))
            {
                //sends a request and waits for the response
                MsgOut m = conn.Request(
                    new MsgIn("The.Target").SetPayload("Mario"),
                    TimeSpan.FromSeconds(1),
                    CancellationToken.None
                    );

                Console.WriteLine("Master received: " + m.GetPayloadAsString());
                Debug.Assert(m.GetPayloadAsString() == this._expectedResponsePayload);
            }
        }
Ejemplo n.º 12
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);
            }
        }
Ejemplo n.º 13
0
        private void Publisher()
        {
            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            using (IConnection conn = cf.CreateConnection(opts))
            {
                //sends MessageCount+1 messages to the subscriber
                for (int i = 0; i <= MessageCount; i++)
                {
                    Console.WriteLine("Publishing: " + TestPayload);
                    conn.Publish(
                        new MsgIn("The.Target").SetPayload(TestPayload),
                        CancellationToken.None
                        );

                    //a short delay before the next message
                    Task.Delay(1000).Wait();
                }
            }
        }
Ejemplo n.º 14
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
                    );
            }
        }
Ejemplo n.º 15
0
        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();
            }
        }