Can I delete a message from a queue of ActiveMQ in c# code?

ぃ、小莉子 提交于 2019-12-25 01:59:04

问题


I want to set a unique guid for each message in message head, then if I want, I could delete a specific message if I want to. Is there any api in NMS could help me deleting the message? I am using ActiveMQ 5.9.0 and NMS 1.6.1


回答1:


Yes, although only if that destination has no active consumers. You can do something like this:

    protected static void DeleteDestination()
    {
        IConnectionFactory factory = new ConnectionFactory(ReplaceEnvVar(connectionURI));

        using (Connection connection = factory.CreateConnection() as Connection)
        {
            using (ISession session = connection.CreateSession())
            {
                IQueue queue = session.GetQueue(testQueueName);
                try
                {
                    connection.DeleteDestination(queue);
                }
                catch
                {
                }
            }
        }
    }



回答2:


Very much possible to perform individual delete from C# and NMS library directly without the REST API.

I'm using NMS 18.0 from NuGet in a C# project for our support and maintenance tool Nodinite and this is the code from one of the many monitoring agents, in this case for ActiveMQ. This code is used to remove individually selected messages.

This code removes 1 message (messageId) from Queue (queueName)

 internal static void  DeleteMessageFromQueue(ActiveMQOption activeMQOption, string queueName, string messageId)
    {
        IConnectionFactory factory = CreateConnectionFactory(activeMQOption);

        using (IConnection connection = factory.CreateConnection())
        {
            using (ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge))
            {
                using (IDestination destination = session.GetQueue(queueName))
                {
                    using (IMessageConsumer consumer = session.CreateConsumer(destination, $"JMSMessageID LIKE '%{messageId}%'"))
                    {
                        connection.Start();
                        var message = consumer.Receive(new TimeSpan(0, 0, 1));
                        consumer.Close();
                        connection.Close();
                        if (message == null)
                        {
                            throw new Exception($"Message '{messageId}' not found on queue '{queueName}'");
                        }
                    }
                }
            }                
        }            
    }

Helper method to create the factory (using a simple model c# Class that you need to replace with your own code but example should be simple enough to follow)

    public static Apache.NMS.ActiveMQ.ConnectionFactory CreateConnectionFactory(ActiveMQOption activeMQOption)
    {
        Uri connecturi = new Uri(activeMQOption.ConnectionString);
        Apache.NMS.ActiveMQ.ConnectionFactory factory = new Apache.NMS.ActiveMQ.ConnectionFactory(connecturi);
        if (activeMQOption.UseAuthentication)
        {
            factory.UserName = activeMQOption.User;
            factory.Password = activeMQOption.Password;
        }

        return factory;
    }


来源:https://stackoverflow.com/questions/21299370/can-i-delete-a-message-from-a-queue-of-activemq-in-c-sharp-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!