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

2.2k Views Asked by At

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

2

There are 2 best solutions below

2
On

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
                {
                }
            }
        }
    }
0
On

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;
    }