//syncSubscriptions

        internal NetNotification GetSyncMessage(string queueName, NetMessage message)
        {
            NetNotification receivedMessage             = null;
            HandoverSyncObject <NetNotification> synObj = null;

            lock (syncSubscriptions)
            {
                if (syncSubscriptions.ContainsKey(queueName))
                {
                    throw new ArgumentException("Queue " + queueName + " has already a poll runnig.");
                }
                PollRequest pr = new PollRequest();
                pr.Subscription = message;
                pr.Handover     = synObj = new HandoverSyncObject <NetNotification>();
                syncSubscriptions.Add(queueName, pr);
            }

            receivedMessage = synObj.Get();
            lock (syncSubscriptions)
            {
                syncSubscriptions.Remove(queueName);
            }

            return(receivedMessage);
        }
        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");
            }
        }
Example #3
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);
                }
            }
        }
Example #4
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);
        }
Example #5
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.");
        }