示例#1
0
        public static void Main(string[] args)
        {
            try
            {
                const string connectionUrl = @"amqp://*****:*****@clientid/test?brokerlist='tcp://localhost:5672'";
                const string queueName     = @"test-queue";

                var connectionInfo = QpidConnectionInfo.FromUrl(connectionUrl);
                var connection     = new AMQConnection(connectionInfo);
                var channel        = connection.CreateChannel(false, AcknowledgeMode.AutoAcknowledge, 1);

                channel.DeclareQueue(queueName, false, true, true);
                channel.Bind(queueName, ExchangeNameDefaults.DIRECT, queueName);
                IMessageConsumer consumer = channel.CreateConsumerBuilder(queueName).Create();
                connection.Start();

                ITextMessage message = (ITextMessage)consumer.Receive();
                Console.WriteLine("Got: " + message.Text);
                connection.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        /// <summary>
        /// Creates the test connection with a fail-over set up, and a producer/consumer pair on that connection.
        /// </summary>
        /// [SetUp]
        public void Init(IConnectionInfo connectionInfo)
        {
            //log4net.Config.BasicConfigurator.Configure();
            // Reset all counts.
            messagesSent     = 0;
            messagesReceived = 0;
            failedOver       = false;
            _extraMessage    = 0;

            PromptAndWait("Ensure both brokers are running, then press Enter");

            // Create a connection for the test.
            _connection = new AMQConnection(connectionInfo);
            _connection.ConnectionListener = this;

            // Create a consumer to receive the test messages.
            IChannel receivingChannel = _connection.CreateChannel(false, _acknowledgeMode);

            string queueName = receivingChannel.GenerateUniqueName();

            receivingChannel.DeclareQueue(queueName, false, true, true);
            receivingChannel.Bind(queueName, "amq.direct", queueName);

            IMessageConsumer consumer = receivingChannel.CreateConsumerBuilder(queueName)
                                        .WithPrefetchLow(30)
                                        .WithPrefetchHigh(60).Create();

            consumer.OnMessage = new MessageReceivedDelegate(OnMessage);
            _connection.Start();

            // Create a publisher to send the test messages.
            publishingChannel = _connection.CreateChannel(transacted, AcknowledgeMode.NoAcknowledge);
            publisher         = publishingChannel.CreatePublisherBuilder()
                                .WithRoutingKey(queueName)
                                .Create();

            _log.Debug("connection = " + _connection);
            _log.Debug("connectionInfo = " + connectionInfo);
            _log.Debug("connection.AsUrl = " + _connection.toURL());
            _log.Debug("AcknowledgeMode is " + _acknowledgeMode);
        }
        private static void SendMessages()
        {
            AMQConnection conn = new AMQConnection(QpidConnectionInfo.FromUrl(BaseMessagingTestFixture.connectionUri));

            conn.Start();
            IChannel          channel  = conn.CreateChannel(false, AcknowledgeMode.AutoAcknowledge);
            IMessagePublisher producer = channel.CreatePublisherBuilder().
                                         WithExchangeName(ExchangeNameDefaults.DIRECT).
                                         WithRoutingKey(TEST_ROUTING_KEY).
                                         Create();

            for (int i = 0; i < MESSAGE_COUNT; i++)
            {
                if ((i % 10) == 0)
                {
                    Console.WriteLine("Sending message " + i);
                }
                producer.Send(channel.CreateTextMessage("Msg" + i));
            }
        }
        public static void Main(string[] args)
        {
            try
            {
                const string connectionUrl = @"amqp://*****:*****@clientid/test?brokerlist='tcp://localhost:5672'";
                const string queueName     = @"test-queue";

                var connectionInfo = QpidConnectionInfo.FromUrl(connectionUrl);
                var connection     = new AMQConnection(connectionInfo);
                var channel        = connection.CreateChannel(false, AcknowledgeMode.AutoAcknowledge);
                var publisher      = channel.CreatePublisherBuilder()
                                     .WithExchangeName(ExchangeNameDefaults.DIRECT)
                                     .WithRoutingKey(queueName)
                                     .Create();
                IMessage message = channel.CreateTextMessage("0123456789");
                publisher.Send(message);
                Console.WriteLine("Sent message");
                connection.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }