示例#1
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");
        }
 public NotificationHandler(BrokerClient client, NetAction.DestinationType actionType, int expectedMessages)
 {
     this.client = client;
     this.actionType = actionType;
     this.expectedMessages = expectedMessages;
     this.Notifications = new List<NetNotification>();
     this.ManualResetEvent = new ManualResetEvent(false);
 }
        public static void Main(string[] args)
        {
            Console.WriteLine("MultipleProducers 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));
            int numberOfMessages = 500000;

            Thread[] threads = new System.Threading.Thread[NR_THREADS];

            for (int i = 0; i != threads.Length; ++i)
            {
                threads[i] = new Thread(
                    new ThreadStart(() =>
                    {
                        int msgs = numberOfMessages;
                        int threadId = i;

                        while ((--msgs) != 0)
                        {
                            int msgId = Interlocked.Increment(ref message_id);
                            string message = String.Format("{0} - Thread id: {1}", msgId, threadId);
                            NetBrokerMessage brokerMessage = new NetBrokerMessage(System.Text.Encoding.UTF8.GetBytes(message));

                            System.Console.WriteLine(message);
                            if (cliArgs.DestinationType == NetAction.DestinationType.TOPIC)
                            {
                                brokerClient.Publish(brokerMessage, cliArgs.DestinationName);
                            }
                            else
                            {
                                brokerClient.Enqueue(brokerMessage, cliArgs.DestinationName);
                            }
                        }
                    }
                )
                );

                threads[i].Start();

            }

            Console.WriteLine("Write X to unsbscribe and exit");
            while (!System.Console.Read().Equals('X'))
                ;
        }
        public static void Main(string[] args)
        {
            Console.WriteLine("ConsumerWithAutoAck 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);

            if (cliArgs.DestinationType != NetAction.DestinationType.TOPIC)
            {
                // Set AutoAcknowledge
                subscription.AutoAcknowledge = true;
            }

            int i = 0;
            subscription.OnMessage += delegate(NetNotification notification)
            {
                System.Console.WriteLine("Message received: {0}, Total: {1}",
                                         System.Text.Encoding.UTF8.GetString(notification.Message.Payload), (++i).ToString());
                /*
                 *  AutoAcknowledge is enable, so, there is no need to excplicit acknowledge message
                 *
                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");
        }
 public BrokerSMILoggerAppender(pt.sapo.gis.trace.configuration.appender config)
     : base(config)
 {
     Properties = new BrokerSMILoggerAppenderConfigProperties(base.Properties);
     base.Properties = Properties;
     try
     {
         this.brokerClient = new BrokerClient(new HostInfo(Properties.Host, Properties.Port));
     }
     catch (Exception ex)
     {
         throw new IntializationException("Error initialize sapo broker client", ex);
     }
     this.topic = Properties.Topic;
 }
        public static void Main(string[] args)
        {
            Console.WriteLine("Consumer with failover 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();

            List<HostInfo> hosts = new List<HostInfo>();
            hosts.Add(new HostInfo(cliArgs.Hostname, cliArgs.PortNumber));
            //hosts.Add(new HostInfo(cliArgs.Hostname, 3423)); // Add an alternative

            BrokerClient brokerClient = new BrokerClient(hosts);
            Subscription subscription = new Subscription(cliArgs.DestinationName, cliArgs.DestinationType);
            subscription.OnMessage += delegate(NetNotification notification)
            {
                System.Console.WriteLine("Message received: {0}",
                                         System.Text.Encoding.UTF8.GetString(notification.Message.Payload));
                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");
        }
示例#7
0
 private static void PublishMessages(BrokerClient brokerClient, string destination, int numberOfMessages, NetAction.DestinationType destinationType)
 {
     //string message = "Hello, how are you?";
     int i = 0;
     while ((numberOfMessages--) != 0)
     {
         System.Console.WriteLine("Publishing message");
         NetBrokerMessage brokerMessage = new NetBrokerMessage(System.Text.Encoding.UTF8.GetBytes((i++).ToString()));
         if (destinationType == NetAction.DestinationType.TOPIC)
         {
             brokerClient.Publish(brokerMessage, destination);
         }
         else
         {
             brokerClient.Enqueue(brokerMessage, destination);
         }
         System.Threading.Thread.Sleep(50);
     }
 }
示例#8
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Ping 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));

            NetPong pong = brokerClient.Ping();

            Console.WriteLine("Pong was null? {0}", pong == null);
        }
示例#9
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Producer 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));

            PublishMessages(brokerClient, cliArgs.DestinationName, 100, cliArgs.DestinationType);
        }
示例#10
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);
                }
            }
        }
        public static void Main(string[] args)
        {
            Console.WriteLine("ConsumerWithNoAckRequired 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);

            if (cliArgs.DestinationType != NetAction.DestinationType.TOPIC)
            {
                subscription.SetHeader("ACK_REQUIRED", "false");
            }

            int i = 0;
            subscription.OnMessage += delegate(NetNotification notification)
            {
                System.Console.WriteLine("Message received: {0}, Total: {1}",
                                         System.Text.Encoding.UTF8.GetString(notification.Message.Payload), (++i).ToString());

                IDictionary<string, string> headers = notification.Headers;
                if (headers.Keys != null)
                {
                    System.Console.WriteLine("Headers:");

                    foreach (string header in headers.Keys)
                    {
                        System.Console.WriteLine("{0} - {1}", header, headers[header]);
                    }
                }

                /*
                 *  ACK IS NOT REQUIRED because ACK_REQUIRED was set to false.
                 *
                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");
        }