且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当activemq中客户端的空闲时间删除特定队列

更新时间:2023-11-22 08:21:04

我的要求

同步进程

客户端将消息发送到服务器,但是MessageLestener处于不活动状态/关闭状态,我想从队列中删除该特定消息。

Client send the message to the server, but MessageLestener is not active/down, I want to remove this specific message from the queue.

如何使用messageid从队列中删除特定消息?

我也喜欢您的问题,我提供可恢复的功能。您只需要传递 MessageId Queue 名称。

I also have like your problem, I provide the resuable function. You just need to pass MessageId and Queue name. It is ok for me.

private void deleteMessage(String messageId, String queueName) {
    try {
         JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi");
         JMXConnector jmxc = JMXConnectorFactory.connect(url);
         MBeanServerConnection conn = jmxc.getMBeanServerConnection();
         ObjectName name = new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost");
         BrokerViewMBean proxy = (BrokerViewMBean)MBeanServerInvocationHandler.newProxyInstance(conn, name, BrokerViewMBean.class, true);
         for (ObjectName queue : proxy.getQueues()) {  
            QueueViewMBean queueBean = (QueueViewMBean) MBeanServerInvocationHandler.newProxyInstance(conn, queue, QueueViewMBean.class, true);
            if(queueBean.getName().equals(queueName)) {
                System.out.println("Deleted : " + messageId);
                queueBean.removeMessage(messageId);
                return;
            }
         }
    } catch(Exception e) {
        e.printStackTrace();
    }
}

我使用 activemq-all -5.8.0.jar