コード例 #1
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Consumer test");

            if (args.Length == 0)
            {
                System.Console.WriteLine(CommandLineArguments.Usage());
                return;
            }

            CommandLineArguments cliArgs = new CommandLineArguments();
            Parser parser = new Parser(System.Environment.CommandLine, cliArgs);

            parser.Parse();

            BrokerClient brokerClient = new BrokerClient(new HostInfo(cliArgs.Hostname, cliArgs.PortNumber));

            Subscription subscription = new Subscription(cliArgs.DestinationName, cliArgs.DestinationType);
            int          i            = 0;

            subscription.OnMessage += delegate(NetNotification notification)
            {
                if (notification.DestinationType != NetAction.DestinationType.TOPIC)
                {
                    brokerClient.Acknowledge(notification.Subscription, notification.Message.MessageId);
                }
                System.Console.WriteLine("Message received: {0}, Total: {1}",
                                         System.Text.Encoding.UTF8.GetString(notification.Message.Payload), (++i).ToString());
                if (notification.DestinationType != NetAction.DestinationType.TOPIC)
                {
                    brokerClient.Acknowledge(notification);
                }
            };

            brokerClient.Subscribe(subscription);



            Console.WriteLine("Write X to unsbscribe and exit");
            while (!System.Console.Read().Equals('X'))
            {
                ;
            }
            Console.WriteLine();
            Console.WriteLine("Unsubscribe...");

            // Note Subscription instance could other than the one used for subscription as long as it was equivelent (same destination type and subscription pattern). Since the application is ending and therefor the socket will be closed agent's will discard the previous subscription.
            brokerClient.Unsubscribe(subscription);

            Console.WriteLine("Good bye");
        }
コード例 #2
0
        public static void Main(string[] args)
        {
            Console.WriteLine("New Blog Consumer");

            BrokerClient brokerClient = new BrokerClient(new HostInfo("broker.bk.sapo.pt", 3323));

            Subscription subscription = new Subscription("test@/sapo/blogs/activity/post", NetAction.DestinationType.VIRTUAL_QUEUE);

            subscription.OnMessage += delegate(NetNotification notification)
            {
                if (((++count) % 100) == 0)
                {
                    Console.WriteLine("{0} - New message received. Count: {1}", DateTime.Now.ToLongTimeString(), count);
                }
                brokerClient.Acknowledge(notification.Destination, notification.Message.MessageId);
            };

            brokerClient.Subscribe(subscription);

            Console.WriteLine("Write X to unsbscribe and exit");
            while (!System.Console.Read().Equals('X'))
            {
                ;
            }
            Console.WriteLine();
            Console.WriteLine("Unsubscribe...");

            // Note Subscription instance could other than the one used for subscription as long as it was equivelent (same destination type and subscription pattern). Since the application is ending and therefor the socket will be closed agent's will discard the previous subscription.
            brokerClient.Unsubscribe(subscription);

            Console.WriteLine("Good bye");
        }
コード例 #3
0
        public void OnMessageHandler(NetNotification notification)
        {
            lock (this)
            {
                Notifications.Add(notification);

                if (Notifications.Count == expectedMessages)
                {
                    ManualResetEvent.Set();
                }
            }
            if (!this.actionType.Equals(NetAction.DestinationType.TOPIC))
            {
                client.Acknowledge(notification.Subscription, notification.Message.MessageId);
                Console.WriteLine("Acknowledge");
            }
        }
コード例 #4
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Poll test");

            if (args.Length == 0)
            {
                System.Console.WriteLine(CommandLineArguments.Usage());
                return;
            }

            log4net.Config.BasicConfigurator.Configure();

            CommandLineArguments cliArgs = new CommandLineArguments();
            Parser parser = new Parser(System.Environment.CommandLine, cliArgs);

            parser.Parse();

            BrokerClient brokerClient = new BrokerClient(new HostInfo(cliArgs.Hostname, cliArgs.PortNumber));

            while (true)
            {
                try
                {
                    //NetNotification notification = brokerClient.Poll(cliArgs.DestinationName); // Wait forever
                    //NetNotification notification = brokerClient.Poll(cliArgs.DestinationName, 0, 30000, null); //Wait forever, Reserve time: 30s, No Accept Request.
                    NetNotification notification = brokerClient.Poll(cliArgs.DestinationName, 2000);
                    if (notification != null)
                    {
                        System.Console.WriteLine("Message received: {0}",
                                                 System.Text.Encoding.UTF8.GetString(notification.Message.Payload));
                        brokerClient.Acknowledge(notification.Destination, notification.Message.MessageId);
                    }
                    else
                    {
                        Console.WriteLine("No message received");
                    }
                }
                catch (TimeoutException te)
                {
                    Console.WriteLine("Message timedout...", te);
                }
            }
        }
コード例 #5
0
        public override void AddConsequences()
        {
            Consequence consequence = new Consequence("Poll", "Consumer");

            consequence.Runnable = () =>
            {
                BrokerClient    brokerClient = new BrokerClient(new HostInfo(TestContext.GetValue("agent1-host"), Int32.Parse(TestContext.GetValue("agent1-port"))));
                NetNotification notification = brokerClient.Poll(DestinationName, 10000);

                if (notification != null)
                {
                    consequence.Sucess = true;
                    brokerClient.Acknowledge(notification.Subscription, notification.Message.MessageId);
                }

                brokerClient.Close();
                consequence.Done = true;
            };
            this.AddConsequence(consequence);
        }
コード例 #6
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Poll test");

            if (args.Length == 0)
            {
                System.Console.WriteLine(CommandLineArguments.Usage());
                return;
            }

            log4net.Config.BasicConfigurator.Configure();

            CommandLineArguments cliArgs = new CommandLineArguments();
            Parser parser = new Parser(System.Environment.CommandLine, cliArgs);

            parser.Parse();

            int MAX_CONSUMERS       = 32;
            int MESSAGES_TO_RECEIVE = 1000;

            Thread[] consumerThreads = new Thread[MAX_CONSUMERS];

            Object syncObject = new Object();

            for (int i = 0; i != MAX_CONSUMERS; ++i)
            {
                Thread t = new Thread(new ThreadStart(() =>
                {
                    Console.WriteLine("Thread started");

                    BrokerClient brokerClient = new BrokerClient(new HostInfo(cliArgs.Hostname, cliArgs.PortNumber));

                    while (true)
                    {
                        try
                        {
                            NetNotification notification = brokerClient.Poll(cliArgs.DestinationName, -1);
                            if (notification != null)
                            {
                                brokerClient.Acknowledge(notification);
                                int count = ++messagesReceived;
                                if (count == MESSAGES_TO_RECEIVE)
                                {
                                    Console.WriteLine("All messages received");

                                    break;
                                }
                            }
                            else
                            {
                                Console.WriteLine("No message received");
                                break;
                            }
                        }
                        catch (TimeoutException te)
                        {
                            Console.WriteLine("Message timedout...", te);
                            break;
                        }
                    }

                    if ((++threadsEnded) == MAX_CONSUMERS)
                    {
                        lock (syncObject)
                        {
                            Monitor.PulseAll(syncObject);
                        }
                    }
                }));
                consumerThreads[i] = t;
                t.Start();
            }
            lock (syncObject)
            {
                Monitor.Wait(syncObject);
            }
            Console.WriteLine("All threads ended.");
        }