activemq-classicsubscriberapache-nms

How can I delete/remove an ActiveMQ subscriber using NMS API


I need to remove/delete my topic subscriber. I found this http://activemq.apache.org/manage-durable-subscribers.html
However, it's not good enough for us. We want to control the timing of removing a subscriber, and no matter there are any message or not. Besides, our program is written by C#. So the best solution for us is NMS API.

Thanks.


Here are the code,

Apache.NMS.ActiveMQ.ConnectionFactory factory = new Apache.NMS.ActiveMQ.ConnectionFactory(m_brokerURI);
m_connection = factory.CreateConnection(username, password);

Apache.NMS.ActiveMQ.Connection con = (Apache.NMS.ActiveMQ.Connection)m_connection;
ISession session = m_connection.CreateSession(AcknowledgementMode.AutoAcknowledge);

try
{
    session.DeleteDurableConsumer(strQueueName);
}
catch (Exception ex)
{
    // log the error message
}

Update

Our scenario is quite simple.

  1. A client built a queue and subscribed a consumer on a topic.
  2. the client side closed the connection.
  3. delete the consumer on the server side(as the example code in the last update)

Here is the snapshot of activemq broker via jconsole: jconsole snapshot

We would like to remove the subscriber “7B0FD84D-6A2A-4921-967F-92B215E22751” by following method, But always got this error "javax.jms.InvalidDestinationException : No durable subscription exists for: 7B0FD84D-6A2A-4921-967F-92B215E22751"

strSubscriberName = “7B0FD84D-6A2A-4921-967F-92B215E22751”
session.DeleteDurableConsumer(strSubscriberName);

Solution

  • To delete a durable subscription from the NMS API you use the DeleteDurableConsumer method defined in ISession. You must call this method from a Connection that uses the same client Id as was used when the subscription was created and you pass the name of the subscription that is to be removed. The method will fail if there is an active subscriber though so be prepared for that exception.

    In the sample code you don't set a Client Id on the connection. When working with durable subscriptions you must, must, MUST always use the same client Id and subscription name. So in you same you will get this error until you set the client Id to the same value as the connection that created the subscription in the first place.