How to purge/delete message from weblogic JMS queue
Asked Answered
J

5

6

Is there and way (apart from consuming the message) I can purge/delete message programmatically from JMS queue. Even if it is possible by wlst command line tool, it will be of much help.

Jezabella answered 17/3, 2010 at 11:43 Comment(0)
S
7

Here is an example in WLST for a Managed Server running on port 7005:

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

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

cmo.deleteMessages('')

The last command should return the number of messages it deleted.

Siegel answered 15/11, 2011 at 16:26 Comment(2)
I tried this but got this exception: java.lang.UnsupportedOperationException: deleteMessages(String) not valid for class weblogic.jms.backend.BEDestinationRuntimeMBeanImpl even though ls() includes -r-x deleteMessages Integer : String(selector) in the information it returns.Striation
Fixed it for myself! My situation has a topic with durable subscribers. I needed to cd to the MBean for the subscriber and call deleteMessages('') on it.Striation
B
6

You can use JMX to purge the queue, either from Java or from WLST (Python). You can find the MBean definitions for WLS 10.0 on http://download.oracle.com/docs/cd/E11035_01/wls100/wlsmbeanref/core/index.html. Here is a basic Java example (don't forget to put weblogic.jar in the 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();
        }
    }
}
Blakemore answered 17/3, 2010 at 22:17 Comment(0)
P
3

Works great on a single node environment, but what happens if you are on an clustered environment with ONE migratable JMSServer (currently on node #1) and this code is executing on node #2. Then there will be no JMSServer available and no message will be deleted.

This is the problem I'm facing right now...

Is there a way to connect to the JMSQueue without having the JMSServer available?

[edit]
Found a solution: Use the domain runtime service instead:

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

and be sure to access the admin port on the WLS-cluster.

Pilsudski answered 6/10, 2011 at 8:21 Comment(0)
B
0

if this is one time, the easiest would be to do it through the console...

Behr answered 7/2, 2013 at 17:3 Comment(0)
P
0

the program in below link helps you to clear only pending messages from queue based on redelivered message parameter

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

Porphyroid answered 19/2, 2016 at 7:4 Comment(1)
Since this is your first answer. thus i'm not saying anything, but try to avoid external links and show some efforts from your side.Superincumbent

© 2022 - 2024 — McMap. All rights reserved.