Exemplo n.º 1
0
        /// <summary>
        ///     http://activemq.apache.org/nms/nms-simple-synchornous-consumer-example.html
        /// </summary>
        /// <param name="message"></param>
        /// <param name="id"></param>
        public void WriteMessageToQueue(string message, string id, bool persist)
        {
            ActiveMQConnectionManager cm = new ActiveMQConnectionManager(_queueUrl, _queueName);
            ActiveMQHandler.ActiveMQConnectionManager.ConnectionData cd = cm.getConnection();
            IConnection connection = cd.iconnection;
            using (ISession session = connection.CreateSession())
            {
                IDestination destination = SessionUtil.GetDestination(session, "queue://" + _queueName);
                using (IMessageProducer producer = session.CreateProducer(destination))
                {
                    // Start the connection so that messages will be processed.
                    connection.Start();
                    if (persist)
                        producer.DeliveryMode = MsgDeliveryMode.Persistent;
                    else
                        producer.DeliveryMode = MsgDeliveryMode.NonPersistent;

                    // Send a message
                    ITextMessage request = session.CreateTextMessage(message);
                    request.NMSCorrelationID = id;
                    request.NMSTimeToLive = new TimeSpan(0, 5, 0);
                    producer.Send(request);
                    connection.Stop();
                }
            }
            cm.ReleaseConnection(cd);
        }
Exemplo n.º 2
0
        //public static Tuple<string, string> ReadMessage(string messageId, IMessageConsumer consumer)
        //{

        //    Tuple<string, string> message;
        //    // Consume a message
        //    ITextMessage textMessage = consumer.Receive(new TimeSpan(0, 0, 2)) as ITextMessage;

        //    if (textMessage == null)
        //    {
        //        message = Tuple.Create<string, string>(null, "No messages found");
        //    }
        //    else
        //    {
        //        message = Tuple.Create<string, string>(textMessage.NMSCorrelationID, textMessage.Text);
        //    }

        //    return message;
        //}


        public Tuple<string,string>ReadMessage(string messageId)
        {
            ActiveMQConnectionManager cm = new ActiveMQConnectionManager(_queueUrl, _queueName);
            Tuple<string,string> message;

            ActiveMQHandler.ActiveMQConnectionManager.ConnectionData cd = cm.getConnection();
            IConnection connection = cd.iconnection;
            try
            {
                //using (IConnection connection = _factory.CreateConnection())
                using (ISession session = connection.CreateSession())
                {
                    IDestination destination = SessionUtil.GetDestination(session, "queue://" + _queueName);

                    string selector = messageId == string.Empty ? "" : "JMSCorrelationID = '" + messageId + "'";
                    using (IMessageConsumer consumer = selector.Equals(string.Empty) ? session.CreateConsumer(destination) : session.CreateConsumer(destination, selector, false))
                    {
                        // Start the connection so that messages will be processed.
                        connection.Start();

                        // Consume a message
                        ITextMessage textMessage = consumer.Receive(new TimeSpan(0, 0, 2)) as ITextMessage;

                        if (textMessage == null)
                        {
                            message = Tuple.Create<string, string>(null, "No messages found");
                        }
                        else
                        {
                            message = Tuple.Create<string, string>(textMessage.NMSCorrelationID, textMessage.Text);
                        }

                        connection.Stop();
                    }
                }
            }
            finally
            {
                cm.ReleaseConnection(cd);
            }
            return message;
        }
Exemplo n.º 3
0
        public void WriteMessageListToQueue(List<Tuple<string, string>> messageList, bool persist)
        {
            try
            {
                ActiveMQConnectionManager cm = new ActiveMQConnectionManager(_queueUrl, _queueName);
                ActiveMQHandler.ActiveMQConnectionManager.ConnectionData cd = cm.getConnection();
                IConnection connection = cd.iconnection;
                using (ISession session = connection.CreateSession())
                {
                    IDestination destination = SessionUtil.GetDestination(session, "queue://" + _queueName);
                    using (IMessageProducer producer = session.CreateProducer(destination))
                    {
                        // Start the connection so that messages will be processed.
                        connection.Start();
                        if (persist)
                            producer.DeliveryMode = MsgDeliveryMode.Persistent;
                        else
                            producer.DeliveryMode = MsgDeliveryMode.NonPersistent;


                        ITextMessage request;
                        List<Task> tList = new List<Task>();
                        foreach (Tuple<string, string> t in messageList)
                        {
                            // Send a message
                            request = session.CreateTextMessage(t.Item2);
                            request.NMSCorrelationID = t.Item1;
                            request.NMSTimeToLive = new TimeSpan(0, 5, 0);
                            producer.Send(request);
                        }

                        connection.Stop();
                    }
                }
                cm.ReleaseConnection(cd);
            }
            catch (Exception e)
            {
                throw e;
            }

        }