如何从weblogic JMS队列中清除/删除消息

时间:2010-03-17 11:43:13

标签: weblogic-10.x

是否有方法(除了消费消息)我可以从JMS队列中以编程方式清除/删除消息。即使有可能通过wlst命令行工具,它也会有很大的帮助。

5 个答案:

答案 0 :(得分:5)

您可以使用JMX从Java或WLST(Python)清除队列。您可以在http://download.oracle.com/docs/cd/E11035_01/wls100/wlsmbeanref/core/index.html上找到WLS 10.0的MBean定义。 这是一个基本的Java示例(不要忘记将weblogic.jar放在CLASSPATH中):

import java.util.Hashtable;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.management.ObjectName;
import javax.naming.Context;
import weblogic.management.mbeanservers.runtime.RuntimeServiceMBean;

public class PurgeWLSQueue {

    private static final String WLS_USERNAME = "weblogic";
    private static final String WLS_PASSWORD = "weblogic";
    private static final String WLS_HOST = "localhost";
    private static final int WLS_PORT = 7001;
    private static final String JMS_SERVER = "wlsbJMSServer";
    private static final String JMS_DESTINATION = "test.q";

    private static JMXConnector getMBeanServerConnector(String jndiName) throws Exception {
        Hashtable<String,String> h = new Hashtable<String,String>();
        JMXServiceURL serviceURL = new JMXServiceURL("t3", WLS_HOST, WLS_PORT, jndiName);
        h.put(Context.SECURITY_PRINCIPAL, WLS_USERNAME);
        h.put(Context.SECURITY_CREDENTIALS, WLS_PASSWORD);
        h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
        JMXConnector connector = JMXConnectorFactory.connect(serviceURL, h);
        return connector;
    }

    public static void main(String[] args) {
        try {
            JMXConnector connector = 
              getMBeanServerConnector("/jndi/"+RuntimeServiceMBean.MBEANSERVER_JNDI_NAME);
            MBeanServerConnection mbeanServerConnection = 
              connector.getMBeanServerConnection();

            ObjectName service = new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
            ObjectName serverRuntime = (ObjectName) mbeanServerConnection.getAttribute(service, "ServerRuntime");
            ObjectName jmsRuntime = (ObjectName) mbeanServerConnection.getAttribute(serverRuntime, "JMSRuntime");
            ObjectName[] jmsServers = (ObjectName[]) mbeanServerConnection.getAttribute(jmsRuntime, "JMSServers");
            for (ObjectName jmsServer: jmsServers) {
                if (JMS_SERVER.equals(jmsServer.getKeyProperty("Name"))) {
                    ObjectName[] destinations = (ObjectName[]) mbeanServerConnection.getAttribute(jmsServer, "Destinations");
                    for (ObjectName destination: destinations) {
                        if (destination.getKeyProperty("Name").endsWith("!"+JMS_DESTINATION)) {
                            Object o = mbeanServerConnection.invoke(
                                destination,
                                "deleteMessages",
                                new Object[] {""},        // selector expression
                                new String[] {"java.lang.String"});
                            System.out.println("Result: "+o);
                            break;
                        }
                    }
                    break;
                }
            }
            connector.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

答案 1 :(得分:5)

以下是WLST中针对在端口7005上运行的受管服务器的示例:

connect('weblogic', 'weblogic', 't3://localhost:7005')
serverRuntime()

cd('/JMSRuntime/ManagedSrv1.jms/JMSServers/MyAppJMSServer/Destinations/MyAppJMSModule!QueueNameToClear')

cmo.deleteMessages('')

最后一个命令应该返回它删除的消息数。

答案 2 :(得分:3)

在单节点环境中运行良好,但如果您在具有一个可迁移JMSServer(当前在节点#1上)的集群环境中并且此代码在节点#2上执行,会发生什么。然后将没有可用的JMSServer,也不会删除任何消息。

这是我现在面临的问题......

有没有办法在没有JMSServer可用的情况下连接到JMSQueue?

[编辑]
找到解决方案:改为使用域运行时服务:

ObjectName service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");

并确保访问WLS群集上的 admin 端口。

答案 3 :(得分:0)

如果这是一次,最简单的方法是通过控制台......

答案 4 :(得分:0)

以下链接中的程序可帮助您根据重新传递的消息参数

仅清除队列中的待处理消息

http://techytalks.blogspot.in/2016/02/deletepurge-pending-messages-from-jms.html