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); } } }
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); }
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."); }