我可以使用C#代码从ActiveMQ队列中删除消息吗? [英] Can I delete a message from a queue of ActiveMQ in c# code?

查看:497
本文介绍了我可以使用C#代码从ActiveMQ队列中删除消息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为消息头中的每条消息设置唯一的GUID,然后根据需要删除特定的消息。 NMS中是否有任何api可以帮助我删除邮件?我正在使用ActiveMQ 5.9.0和NMS 1.6.1

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

推荐答案

非常有可能直接从C#和NMS库中进行单个删除

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

我在C#项目中使用NuGet的NMS 18.0作为我们的支持和维护工具 Nodinite ,这是来自许多监视代理商,在本例中为ActiveMQ。此代码用于删除单独选择的消息。

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.

此代码从队列(queueName)中删除1条消息(messageId)

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}'");
                        }
                    }
                }
            }                
        }            
    }

创建工厂的辅助方法(使用一个简单的模型c#类,您需要用自己的代码替换该类,但示例应该足够简单才能遵循)

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

这篇关于我可以使用C#代码从ActiveMQ队列中删除消息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆