示例#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);
            }
        }
 public CreateChannelFailoverSupport(AMQConnection connection, bool transacted, AcknowledgeMode acknowledgeMode, int prefetchHigh, int prefetchLow)
 {
     _connection      = connection;
     _transacted      = transacted;
     _acknowledgeMode = acknowledgeMode;
     _prefetchHigh    = prefetchHigh;
     _prefetchLow     = prefetchLow;
 }
示例#3
0
 public void SimpleConnection()
 {
     IConnectionInfo connectionInfo = new QpidConnectionInfo();
     connectionInfo.VirtualHost = "test";
     connectionInfo.AddBrokerInfo(_broker);
     using (IConnection connection = new AMQConnection(connectionInfo))
     {
         Console.WriteLine("connection = " + connection);
     }
 }
示例#4
0
 private static void MakeBrokerConnection(SslOptions options)
 {
     IConnectionInfo connectionInfo = new QpidConnectionInfo();
     connectionInfo.VirtualHost = "test";
     connectionInfo.AddBrokerInfo(new AmqBrokerInfo("amqp", "localhost", 8672, options));
     
     using ( IConnection connection = new AMQConnection(connectionInfo) )
     {
         Console.WriteLine("connection = " + connection);
     }
 }
示例#5
0
        public void PasswordFailureConnection()
        {
            IConnectionInfo connectionInfo = new QpidConnectionInfo();
            connectionInfo.VirtualHost = "test";
            connectionInfo.Password = "******";
            connectionInfo.AddBrokerInfo(_broker);

            using (IConnection connection = new AMQConnection(connectionInfo))
            {
                 Console.WriteLine("connection = " + connection);
                 // wrong
                 Assert.Fail("Authentication succeeded but should've failed");
            }
        }
示例#6
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);
                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);
            }
        }
        /// <summary> Sets up the nth test end-point. </summary>
        ///
        /// <param name="n">The index of the test end-point to set up.</param>
        /// <param name="producer"><tt>true</tt> to set up a producer on the end-point.</param>
        /// <param name="consumer"><tt>true</tt> to set up a consumer on the end-point.</param>
        /// <param name="routingKey">The routing key for the producer to send on.</param>
        /// <param name="ackMode">The ack mode for the end-points channel.</param>
        /// <param name="transacted"><tt>true</tt> to use transactions on the end-points channel.</param>
        /// <param name="exchangeName">The exchange to produce or consume on.</param>
        /// <param name="declareBind"><tt>true</tt> if the consumers queue should be declared and bound, <tt>false</tt> if it has already been.</param>
        /// <param name="durable"><tt>true</tt> to declare the consumers queue as durable.</param>
        /// <param name="subscriptionName">If durable is true, the fixed unique queue name to use.</param>
        /// <param name="exclusive"><tt>true</tt> declare queue as exclusive.</param>
        /// <param name="browse"><tt>true</tt> only browse, don''t consume.</param>
        public void SetUpEndPoint(int n, bool producer, bool consumer, string routingKey, AcknowledgeMode ackMode, bool transacted,
                                  string exchangeName, bool declareBind, bool durable, string subscriptionName, bool exclusive, bool browse)
        {
            // Allow client id to be fixed, or undefined.
            {
                // Use unique id for end point.
                connectionInfo = QpidConnectionInfo.FromUrl(connectionUri);

                connectionInfo.ClientName = "test" + n;
            }

            testConnection[n] = new AMQConnection(connectionInfo);            
            testConnection[n].Start();
            testChannel[n] = testConnection[n].CreateChannel(transacted, ackMode);
            
            if (producer)
            {
                testProducer[n] = testChannel[n].CreatePublisherBuilder()
                    .WithExchangeName(exchangeName)
                    .WithRoutingKey(routingKey)
                    .Create();
            }

            if (consumer)
            {
                string queueName;

                // Use the subscription name as the queue name if the subscription is durable, otherwise use a generated name.
                if (durable)
                {
                    // The durable queue is declared without auto-delete, and passively, in case it has already been declared.
                    queueName = subscriptionName;
                    
                    if (declareBind)
                    {
                        testChannel[n].DeclareQueue(queueName, durable, exclusive, false);
                        testChannel[n].Bind(queueName, exchangeName, routingKey);
                    }
                }
                else
                {
                    queueName = testChannel[n].GenerateUniqueName();

                    if (declareBind)
                    {
                    	if (durable) 
                    	{
                    		testQueue[n] = queueName;
                    	}
                        testChannel[n].DeclareQueue(queueName, durable, true, true);
                        testChannel[n].Bind(queueName, exchangeName, routingKey);
                    }
                }

                testConsumer[n] = testChannel[n].CreateConsumerBuilder(queueName).WithBrowse(browse).Create();
            }
        }
示例#8
0
        static void Main(string[] args)
        {
             XmlConfigurator.Configure(new FileInfo("..\\..\\log.xml"));
            // DOMConfigurator.Configure()            

            string host = ConfigurationManager.AppSettings["Host"];
            int port = int.Parse(ConfigurationManager.AppSettings["Port"]);
            string virtualhost = ConfigurationManager.AppSettings["VirtualHost"];
            string username = ConfigurationManager.AppSettings["Username"];
            string password = ConfigurationManager.AppSettings["Password"];
            IConnectionInfo connectionInfo = new QpidConnectionInfo();
            connectionInfo.VirtualHost = virtualhost;
            //connectionInfo.host = host;
            //connectionInfo.port = port;
            connectionInfo.Username = username;
            connectionInfo.Password = password;

            //Client client = new Client();

            Console.WriteLine("Client created");
            //client.Connect(host, port, virtualhost, username, password);
            IConnection clientConnection = new AMQConnection(connectionInfo);
            Console.WriteLine("Connection established");

            IClientSession ssn = client.CreateSession(50000);

            Console.WriteLine("Session created");
            ssn.QueueDeclare("queue1", null, null);
            ssn.ExchangeBind("queue1", "amq.direct", "queue1", null);


            Object wl = new Object();
            ssn.AttachMessageListener(new MyListener(ssn, wl), "myDest");

            ssn.MessageSubscribe("queue1", "myDest", MessageAcceptMode.EXPLICIT, MessageAcquireMode.PRE_ACQUIRED, null,
                                 0, null);
            DateTime start = DateTime.Now;

            // issue credits     
            ssn.MessageSetFlowMode("myDest", MessageFlowMode.WINDOW);
            ssn.MessageFlow("myDest", MessageCreditUnit.BYTE, ClientSession.MESSAGE_FLOW_MAX_BYTES);
            ssn.MessageFlow("myDest", MessageCreditUnit.MESSAGE, 10000);
            ssn.Sync();


            for (int i = 0; i < 10000; i ++)
            {            
            ssn.MessageTransfer("amq.direct", MessageAcceptMode.NONE, MessageAcquireMode.PRE_ACQUIRED,
                                new Header(new DeliveryProperties().SetRoutingKey("queue1"),
                                           new MessageProperties().SetMessageId(UUID.RandomUuid())),
                                Encoding.UTF8.GetBytes("test: " + i));
            }

            lock(wl)
            {
                Monitor.Wait(wl);
            }
            DateTime now = DateTime.Now;
            Console.WriteLine("Start time " + start + " now: " + now);

            Console.WriteLine("Done time: " +  (now - start));
            lock (wl)
            {
                Monitor.Wait(wl, 30000);
            }
            client.Close();
        }
示例#9
0
        /// <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);
        }
示例#10
0
 public CreateChannelFailoverSupport(AMQConnection connection, bool transacted, AcknowledgeMode acknowledgeMode, int prefetchHigh, int prefetchLow)
 {
     _connection = connection;
     _transacted = transacted;
     _acknowledgeMode = acknowledgeMode;
     _prefetchHigh = prefetchHigh;
     _prefetchLow = prefetchLow;
 }
示例#11
0
        /// <summary>
        /// Establishes an AMQ connection. This is a simple convenience method for code that does not anticipate handling connection failures. 
        /// All exceptions that indicate that the connection has failed, are allowed to fall through.
        /// </summary>
        ///
        /// <param name="brokerUrl">   The broker url to connect to, <tt>null</tt> to use the default from the properties. </param>
        /// <param name="virtualHost"> The virtual host to connectio to, <tt>null</tt> to use the default. </param>
        ///
        /// <returns> A JMS conneciton. </returns>
        public static IConnection CreateConnection(string brokerUrl, string virtualHost)
        {
            log.Info("public static Connection createConnection(string brokerUrl = " + brokerUrl + ", string virtualHost = " 
                     + virtualHost + "): called");

            // Create a connection to the broker.
            IConnectionInfo connectionInfo = QpidConnectionInfo.FromUrl(brokerUrl);
            connectionInfo.VirtualHost = virtualHost;
            IConnection connection = new AMQConnection(connectionInfo);

            return connection;
        }
示例#12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AmqChannel"/> class.
        /// </summary>
        /// <param name="con">The connection.</param>
        /// <param name="channelId">The channel id.</param>
        /// <param name="transacted">if set to <c>true</c> [transacted].</param>
        /// <param name="acknowledgeMode">The acknowledge mode.</param>
        /// <param name="defaultPrefetchHigh">Default prefetch high value</param>
        /// <param name="defaultPrefetchLow">Default prefetch low value</param>
        internal AmqChannel(AMQConnection con, ushort channelId, bool transacted, AcknowledgeMode acknowledgeMode, 
                            int defaultPrefetchHigh, int defaultPrefetchLow)
            : this()
        {
            _sessionNumber = Interlocked.Increment(ref _nextSessionNumber);
            _connection = con;
            _transacted = transacted;
            
            if ( transacted )
            {
                _acknowledgeMode = AcknowledgeMode.SessionTransacted;
            } 
            else
            {
                _acknowledgeMode = acknowledgeMode;
            }

            _channelId = channelId;
            _defaultPrefetchHighMark = defaultPrefetchHigh;
            _defaultPrefetchLowMark = defaultPrefetchLow;

            if ( _acknowledgeMode == AcknowledgeMode.NoAcknowledge )
            {
                _queue = new FlowControlQueue(_defaultPrefetchLowMark, _defaultPrefetchHighMark,
                                              new ThresholdMethod(OnPrefetchLowMark),
                                              new ThresholdMethod(OnPrefetchHighMark));
            } 
            else
            {
                // low and upper are the same
                _queue = new FlowControlQueue(_defaultPrefetchHighMark, _defaultPrefetchHighMark,
                                              null, null);
            }
            DefaultInstance = this;
        }
        public void initRKListener(string connectionUri)
        {
            IConnectionInfo connectionInfo = QpidConnectionInfo.FromUrl(connectionUri);
            if (connection == null) 
                connection = new AMQConnection(connectionInfo);
            //IConnection connection;
            //AMQConnection  connection; 
            if (channel == null) 
                channel = (AmqChannel)EnsureConnected(connectionUri, true, true, out connection);

        }
        public event MessageReceivedDelegate OnAvroMessage; // { get; set; }

        /// <summary> Creates a topic listener using the specified broker URL. </summary>
        /// 
        /// <param name="connectionUri">The broker URL to listen on.</param>
        public void RoboKindListener(string connectionUri, string botcontrol)
        {
            LogDebug("TopicListener(string connectionUri = " + connectionUri + "): called " + botcontrol);

            // Create a connection to the broker.
            IConnectionInfo connectionInfo = QpidConnectionInfo.FromUrl(connectionUri);
            if (connection == null) connection = new AMQConnection(connectionInfo);

            // Establish a session on the broker.
            if (channel == null) channel = (AmqChannel)connection.CreateChannel(false, AcknowledgeMode.AutoAcknowledge, 1);
            
            // Set up a queue to listen for test messages on.
            string topicQueueName = channel.GenerateUniqueName();
          //  AMQDestination dest = null;
            // doug just hanged to !isEclusive
            channel.DeclareQueue(topicQueueName, false, false, true);

            // Set this listener up to listen for incoming messages on the test topic queue.
            channel.Bind(topicQueueName, ExchangeNameDefaults.TOPIC, botcontrol);
            IMessageConsumer consumer = channel.CreateConsumerBuilder(topicQueueName)
                .Create();
            consumer.OnMessage += OnMessage;

            // Set up this listener with a producer to send the reports on.
            //publisher = channel.CreatePublisherBuilder()
            //    .WithExchangeName(ExchangeNameDefaults.DIRECT)
            //    .WithRoutingKey(RoboKindAvroQPIDModuleMain.ROBOKIND_RESPONSE_ROUTING_KEY)
            //    .Create();
            //publisher.Close();
            //publisher = null;

            connection.Start();

        }
        public IChannel EnsureConnected(String connectionUri, bool createNewChannel, bool createNewConnection, out AMQConnection madeCon)
        {
            madeCon = (AMQConnection)(createNewConnection ? null : this.connection);
            bool startConnection = createNewConnection;
            AmqChannel channel = (AmqChannel)(createNewConnection ? null : this.channel);
            // Create a connection to the broker.
            IConnectionInfo connectionInfo0 = QpidConnectionInfo.FromUrl(connectionUri);
            if (madeCon == null)
            {
                madeCon = new AMQConnection(connectionInfo0);
                startConnection = true;

                // Set up a queue to listen for reports on.
                //CreateDirectListener(channel, RoboKindAvroQPIDModuleMain.ROBOKIND_RESPONSE_ROUTING_KEY, OnMessage);
                //TopicDefault = botcontrol;

            }

            // Establish a session on the broker.
            if (channel == null || createNewChannel)
            {
                channel = (AmqChannel) madeCon.CreateChannel(false, AcknowledgeMode.AutoAcknowledge, 1);
            }

            if (startConnection)
            {
             //   madeCon.Start();
            }
            return channel;
        }
示例#16
0
 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));
 	}
 }