停止Camel Route后删除ActiveMQ队列

时间:2015-12-21 22:59:54

标签: java spring jms apache-camel activemq

由于错误

,我在删除ActiveMQ队列时遇到问题
Destination   still has an active subscription: queue://MyQueue

原因是有一个消费者附加到队列,这是我从ActiveMQ控制台看到的,但消费者不应该在那里并且活跃(恕我直言,但有问题我肯定是错的),因为:

T0:队列MyQueue有消息而没有消费者附加
T1:Spring TaskScheduler执行一个代码,该代码创建一个Camel Route负责使用此代码移动另一个队列中的消息

....
//context is a CamelContext instance autowired
context.addRoutes(new RouteBuilder() {

        public void configure() {

            from(myQueueURI)
            .id(myQueueURI)
            .to(destinationQueueURI);                       
        }

T2:在特定的固定时间间隔内,另一个Spring TaskScheduler负责查询MyQueue以了解它是否为空,在这种情况下,尝试删除队列。

....
....
@Autowired
public CachingConnectionFactory localCachingConnectionFactory;
....
....
removeCamelRoutes(queueName, routeId);

private boolean removeCamelRoutes(String queueName, String routeId) throws JMSException{

        if (isQueueEmpty(queueName)){
            try {
                //context is a CamelContext instance autowired
                context.stopRoute(routeId);
                context.removeRoute(routeId);
                deleteQueue(queueName);
            } catch (Exception e) {
                //logs 
                return false;
            }
        }
        ....
        ....
}

private boolean isQueueEmpty(String queueName) throws JMSException{
    ActiveMQConnectionFactory activeMQConnectionFactory = (ActiveMQConnectionFactory) localCachingConnectionFactory.getTargetConnectionFactory();
    ActiveMQConnection connection = (ActiveMQConnection) activeMQConnectionFactory.createConnection();
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ActiveMQQueue queue = (ActiveMQQueue) session.createQueue(queueName);
    QueueBrowser queueBrowser = session.createBrowser(queue);
    try {
        if (queueBrowser.getEnumeration().hasMoreElements()){
            queueBrowser.close();
            return false;
        }
        return true;
    }
    finally {
        if (queueBrowser != null) {
            queueBrowser.close();       
        }
        session.close();
        connection.close();
    }
}


private void deleteQueue(String queueName) throws Exception {

    ActiveMQConnectionFactory activeMQConnectionFactory = (ActiveMQConnectionFactory) localCachingConnectionFactory.getTargetConnectionFactory();
    ActiveMQConnection connection = (ActiveMQConnection) activeMQConnectionFactory.createConnection();
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ActiveMQQueue queue = (ActiveMQQueue) session.createQueue(queueName);

    try {
        //HERE is where the operation is failing
        connection.destroyDestination(queue);
    } 
    finally {
        session.close();
        connection.close();
    }

}

我要求提供一些关于遗漏消费者遗漏的地方的一些提示,我正在考虑与Camel Route相关的消费者,但它应该与Route自动关闭,它可能是在查询队列时附加的消费者,但我应该正确关闭Session和Connection。

Camel版本2.15.1,ActiveMQ 5.9.0,Spring 4.1.3

提前多多感谢。

0 个答案:

没有答案