Beispiel #1
0
        static void Main(string[] args)
        {
            string queueName = Constants.ChannelName; // The queue name.

            PrintHeader();

            // Setup the communications to the MQ service.
            LocalConnectionFactory factory    = new LocalConnectionFactory();
            IConnection            connection = factory.CreateConnection();
            IModel model = connection.CreateModel();

            model.QueueDeclare(queueName, false, false, false, null);

            // Set up the recieving event handler.
            EventingBasicConsumer myconsumer = new EventingBasicConsumer(model);

            myconsumer.Received   += Consumer_Received;
            myconsumer.Registered += Consumer_Registered;



            model.BasicConsume(queueName, true, myconsumer);

            // Wait for a keypress to end this program.
            Console.ReadKey();


            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Shutting down Consumer Side...");

            // Close down the communications.
            model.Close();
            connection.Close();
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            const string quitInput = "quit";                // The value to use to indicate that you want to quit the program.
            string       queueName = Constants.ChannelName; // The queue name.

            PrintHeader(quitInput);                         // Prints the header and the instuctions.

            // Setup the communications to the MQ service.
            LocalConnectionFactory factory    = new LocalConnectionFactory();
            IConnection            connection = factory.CreateConnection();
            IModel model = connection.CreateModel();

            model.QueueDeclare(queueName, false, false, false, null);


            // Start the input loop.
            string message;

            byte[]       body;
            MessageCoder messageCoder = new MessageCoder();
            string       nameInput    = GetConsoleInputForName();

            while (nameInput.ToLower() != quitInput)
            {
                // Build the message using our very sofisticated protocol.
                messageCoder.Name = nameInput;
                message           = messageCoder.Encode();

                // The body for the communication must be a byte array.
                body = Encoding.UTF8.GetBytes(message);

                // Publish the message to the queue.
                model.BasicPublish("", queueName, null, body);

                // Indicate that the message was send.
                IndicateSend(message);

                nameInput = GetConsoleInputForName();
            }

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Shutting down Publisher Side...");

            // Close down the communications.
            model.Close();
            connection.Close();
        }