Beispiel #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 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");
        }
Beispiel #3
0
        public static void Main(string[] args)
        {
            Console.WriteLine("SSL 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();

            X509CertificateCollection certCollection = null;
            if (cliArgs.CertificatePath != null)
            {
                X509Certificate cert = X509Certificate.CreateFromCertFile(cliArgs.CertificatePath);

                certCollection = new X509CertificateCollection();
                certCollection.Add(cert);
            }

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

            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.Subscription, notification.Message.MessageId);
                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 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");
        }
        public static void Main(string[] args)
        {
            Console.WriteLine("BrokerDB authenticated 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();

            BasicConfigurator.Configure();

            X509CertificateCollection certCollection = null;
            if (cliArgs.CertificatePath != null)
            {
                X509Certificate cert = X509Certificate.CreateFromCertFile(cliArgs.CertificatePath);

                certCollection = new X509CertificateCollection();
                certCollection.Add(cert);
            }

            List<HostInfo> hosts = new List<HostInfo>();
            hosts.Add(new HostInfo(cliArgs.Hostname, cliArgs.SslPortNumber));

            SslBrokerClient brokerClient = new SslBrokerClient(hosts, certCollection);

            brokerClient.OnFault += (f) =>
            {
                Console.WriteLine("Error");
                Console.WriteLine(String.Format("Code: {0}, Message: {1}, Detail: {2}", f.Code, f.Message, f.Detail));
            };

            ICredentialsProvider provider = new BrokerDbProvider("luis", "luis");

            Console.WriteLine("Authenticating");
            if (!brokerClient.Authenticate(provider))
            {
                Console.WriteLine("Authentication failed");
                return;
            }

            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.Subscription, 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 void RemoveSubscription(Subscription subscription)
 {
     lock(subscriptions){
         IDictionary<string, Subscription> destSubscriptions = subscriptions[subscription.DestinationType == NetAction.DestinationType.TOPIC ? NetAction.DestinationType.TOPIC : NetAction.DestinationType.QUEUE];
         destSubscriptions.Remove(subscription.DestinationPattern);
     }
 }
Beispiel #7
0
        /// <summary>
        /// Cancel a previous subscription.
        /// </summary>
        /// <param name="subscription">A Subscription instance.</param>
        /// <param name="acceptRequest">An AcceptRequest instance.</param>
        public void Unsubscribe(Subscription subscription, AcceptRequest acceptRequest)
        {
            if (IsClosed())
                return;

            NetUnsubscribe netUnsubscribe = new NetUnsubscribe(subscription.DestinationPattern, subscription.DestinationType);
            NetAction action = new NetAction(NetAction.ActionType.UNSUBSCRIBE);
            action.UnsbuscribeMessage = netUnsubscribe;
            NetMessage netMessage = new NetMessage(action, subscription.Headers);

            protocolHandler.HandleOutgoingMessage(netMessage, acceptRequest);

            protocolHandler.RemoveSubscription(subscription);
        }
Beispiel #8
0
 /// <summary>
 /// Cancel a previous subscription.
 /// </summary>
 /// <param name="subscription">A Subscription instance.</param>
 public void Unsubscribe(Subscription subscription)
 {
     Unsubscribe(subscription, null);
 }
Beispiel #9
0
        /// <summary>
        /// Subscribe to a destination.
        /// </summary>
        /// <param name="subscription">A Subscription instance.</param>
        /// <param name="acceptRequest">An AcceptRequest instance.</param>
        public void Subscribe(Subscription subscription, AcceptRequest acceptRequest)
        {
            if (IsClosed())
                return;

            subscription.BrokerClient = this;

            NetSubscribe netSubscribe = new NetSubscribe(subscription.DestinationPattern, subscription.DestinationType);
            NetAction action = new NetAction(NetAction.ActionType.SUBSCRIBE);
            action.SubscribeMessage = netSubscribe;
            NetMessage netMessage = new NetMessage(action, subscription.Headers);

            protocolHandler.HandleOutgoingMessage(netMessage, acceptRequest);

            protocolHandler.AddSubscription(subscription);
        }
        public virtual Subscription GetSubscription(NotificationHandler notificationHandler)
        {
            Subscription subs = new Subscription(this.SubscriptionName, notificationHandler.ActionType);
            subs.OnMessage += notificationHandler.OnMessageHandler;

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