/// <summary>
        /// Create a destination.  This will delete an existing destination and re-create it.
        /// </summary>
        /// <param name="session"></param>
        /// <param name="destinationName"></param>
        /// <returns></returns>
        public virtual IDestination CreateDestination(ISession session, string destinationName)
        {
            try
            {
                SessionUtil.DeleteDestination(session, destinationName);
            }
            catch (Exception)
            {
                // Can't delete it, so lets try and purge it.
                IDestination destination = SessionUtil.GetDestination(session, destinationName);

                using (IMessageConsumer consumer = session.CreateConsumer(destination))
                {
                    while (consumer.Receive(TimeSpan.FromMilliseconds(750)) != null)
                    {
                    }
                }
            }

            return(SessionUtil.GetDestination(session, destinationName));
        }
示例#2
0
 /// <summary>
 /// Create a destination.  This will delete an existing destination and re-create it.
 /// </summary>
 /// <param name="session"></param>
 /// <param name="destinationName"></param>
 /// <returns></returns>
 public virtual IDestination CreateDestination(ISession session, string destinationName)
 {
     SessionUtil.DeleteDestination(session, destinationName);
     return(SessionUtil.GetDestination(session, destinationName));
 }
 /// <summary>
 /// Extension function to delete the named destination by parsing the embedded type prefix.
 /// </summary>
 public static void DeleteTopic(this ISession session, string topicName)
 {
     SessionUtil.DeleteDestination(session, topicName);
 }
 /// <summary>
 /// Extension function to delete the named destination by parsing the embedded type prefix.
 /// </summary>
 public static void DeleteQueue(this ISession session, string queueName)
 {
     SessionUtil.DeleteDestination(session, queueName);
 }
 /// <summary>
 /// Extension function to delete the named destination by parsing the embedded type prefix.
 /// </summary>
 public static void DeleteDestination(this ISession session, string destinationName, DestinationType defaultType)
 {
     SessionUtil.DeleteDestination(session, destinationName, defaultType);
 }
 /// <summary>
 /// Extension function to delete the named destination by parsing the embedded type prefix.  Default is Queue if no prefix is
 /// embedded in the destinationName.
 /// </summary>
 public static void DeleteDestination(this ISession session, string destinationName)
 {
     SessionUtil.DeleteDestination(session, destinationName);
 }
        public void TestReceiveIgnoreExpirationMessage(
            [Values(AcknowledgementMode.AutoAcknowledge, AcknowledgementMode.ClientAcknowledge,
                    AcknowledgementMode.DupsOkAcknowledge, AcknowledgementMode.Transactional)]
            AcknowledgementMode ackMode,
            [Values(MsgDeliveryMode.NonPersistent, MsgDeliveryMode.Persistent)]
            MsgDeliveryMode deliveryMode,
            [Values(ExpirationOptions.DEFAULT, ExpirationOptions.IGNORE, ExpirationOptions.DO_NOT_IGNORE)]
            ExpirationOptions expirationOption)
        {
            using (IConnection connection = CreateConnection(TEST_CLIENT_ID))
            {
                connection.Start();
                using (Session session = connection.CreateSession(ackMode) as Session)
                {
                    string destinationName = DESTINATION_NAME;

                    if (ExpirationOptions.IGNORE == expirationOption)
                    {
                        destinationName += "?consumer.nms.ignoreExpiration=true";
                    }
                    else if (ExpirationOptions.DO_NOT_IGNORE == expirationOption)
                    {
                        destinationName += "?consumer.nms.ignoreExpiration=false";
                    }

                    try
                    {
                        IDestination destination = SessionUtil.GetDestination(session, destinationName);

                        using (IMessageConsumer consumer = session.CreateConsumer(destination))
                            using (IMessageProducer producer = session.CreateProducer(destination))
                            {
                                producer.DeliveryMode = deliveryMode;

                                string msgText = string.Format("ExpiredMessage: {0}", Guid.NewGuid().ToString());

                                TextMessage msg = session.CreateTextMessage(msgText) as TextMessage;

                                // Give it two seconds to live.
                                msg.NMSTimeToLive = TimeSpan.FromMilliseconds(2000);

                                producer.Send(msg);

                                if (AcknowledgementMode.Transactional == ackMode)
                                {
                                    session.Commit();
                                }

                                // Wait for four seconds before processing it.  The broker will have sent it to our local
                                // client dispatch queue, but we won't attempt to process the message until it has had
                                // a chance to expire within our internal queue system.
                                Thread.Sleep(4000);

                                TextMessage rcvMsg = consumer.ReceiveNoWait() as TextMessage;

                                if (ExpirationOptions.IGNORE == expirationOption)
                                {
                                    Assert.IsNotNull(rcvMsg, "Did not receive expired message.");
                                    rcvMsg.Acknowledge();

                                    Assert.AreEqual(msgText, rcvMsg.Text, "Message text does not match.");
                                    Assert.IsTrue(rcvMsg.IsExpired());

                                    if (AcknowledgementMode.Transactional == ackMode)
                                    {
                                        session.Commit();
                                    }
                                }
                                else
                                {
                                    // Should not receive a message.
                                    Assert.IsNull(rcvMsg, "Received an expired message!");
                                }

                                consumer.Close();
                                producer.Close();
                            }
                    }
                    finally
                    {
                        try
                        {
                            // Ensure that Session resources on the Broker release transacted Consumers.
                            session.Close();
                            // Give the Broker some time to remove the subscriptions.
                            Thread.Sleep(2000);
                            SessionUtil.DeleteDestination(session, destinationName);
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }