Thursday, February 18, 2016

Delete/Purge pending messages from JMS queue using JAVA



Below code deletes only the pending messages that are in the queue based on the Redelivered parameter in the message. There are two types of messages in the queue Current messages and pending messages. Below code deletes only the pending messages and not the current messages.
Pending messages will be in delayed state string whereas current messages will be in visible state string.

import java.util.Hashtable;

import javax.jms.Message;
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 deleteJMSMessages {

    private static final String WLS_USERNAME = "weblogic";
    private static final String WLS_PASSWORD = "weblogic123";
    private static final String WLS_HOST = "din35000861";
    private static final int WLS_PORT = 7001;
    private static final String JMS_SERVER = "oms_jms_server";
    private static final String JMS_DESTINATION = "oms_jms_module!";

    private static JMXConnector getMBeanServerConnector(String jndiName) throws Exception {
       System.out.println("inside getMbeanServerConnector");
        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 {
             
              System.out.println("inside main");
            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");
            Message message = null;
            weblogic.jms.extensions.JMSMessageInfo info = new weblogic.jms.extensions.JMSMessageInfo( ((weblogic.jms.extensions.WLMessage) message) );
       
         
            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)) {
                    //if (destination.getKeyProperty("Name").startsWith(JMS_DESTINATION) && (!destination.getKeyProperty("Name").endsWith("topic") && (!destination.getKeyProperty("Name").endsWith("oms_order_events")))) {
                           System.out.println("destionationqueue"+destination.getKeyProperty("Name"));
                           System.out.println("destionationclass"+destination.getClass());
                            Object o = mbeanServerConnection.invoke(
                                destination,
                                "deleteMessages",
                                new Object[] {"JMSRedelivered=true"},               // selector expression
                                new String[] {"java.lang.String"});
                            System.out.println("No of pending messages deleted: "+o);
                         
                        }
                    }
                    break;
                }
            }
            connector.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    }


No comments: