rss
SOATUBE
Oracle
Custom Search SOABYTE here

Thursday, April 24, 2014

Suspend / Resume an MDB from Java code and WLST script

The below scripts are to suspend and resume an MDB.Server settings values need to be modified accordingly. 

Please set up the environment for your domain and then run the command line if you want to suspend the MDB:
java weblogic.WLST suspend.py

WLST Code

suspend.py :

connect('username','password','t3://host:port')
domainRuntime()
cd('/ServerRuntimes/TargetServerName/ApplicationRuntimes/ApplicationDeploymentName/ComponentRuntimes/ApplicationDeploymentName/EJBRuntimes/MDBName_JNDINameoftheQueue')
cmo.suspend()

resume.py

connect('username','password','t3://host:port')
domainRuntime()
cd('/ServerRuntimes/TargetServerName/ApplicationRuntimes/ApplicationDeploymentName/ComponentRuntimes/ApplicationDeploymentName/EJBRuntimes/MDBName_JNDINameoftheQueue')
cmo.resume()

Java Code

JMX is the technology for managing and monitoring WLS server, just like WLST does. The following class changes the state of the MDB to resume or suspend.

import java.util.Hashtable;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;

public class EjbSuspend {

public static void main(String[] args) {
  try {
    String hostname = "127.0.0.1";
    String portString = "7001";
    String username = "weblogic";
    String password = "weblogic";

    String protocol = "t3";
    Integer portInteger = Integer.valueOf(portString);
    int port = portInteger.intValue();
    String jndiroot = "/jndi/";
    String mserver = "weblogic.management.mbeanservers.runtime";

    JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, jndiroot + mserver);
    Hashtable h = new Hashtable();
    h.put(Context.SECURITY_PRINCIPAL, username);
    h.put(Context.SECURITY_CREDENTIALS, password);
    h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
    h.put("jmx.remote.x.request.waiting.timeout", new Long(10000));
    JMXConnector connector = JMXConnectorFactory.connect(serviceURL, h);
    MBeanServerConnection connection = connector.getMBeanServerConnection();

    Hashtable prop = new Hashtable();
    // configuration of the server:
    prop.put("Name", "messageDriven_examples-jms!quotes"); //MDBName_JNDINameoftheQueue
    prop.put("Type", "MessageDrivenEJBRuntime");
    prop.put("ServerRuntime", "AdminServer"); // TargetServerName
    prop.put("ApplicationRuntime", "ejb20_message"); // ApplicationDeploymentName
    prop.put("EJBComponentRuntime", "ejb20_message.jar"); // ApplicationDeploymentName.jar
    ObjectName beanName = new ObjectName("com.bea", prop);

    String action = "suspend"; // suspend - resume
    connection.invoke(beanName, action, null, null);

    System.out.println("MDB " + action );
    connector.close();

  } catch (Exception e) {
    e.printStackTrace();
  }
 }

}

0 comments:

Post a Comment

 
Blogger Profile