package java4weblogic.thinclient;
import weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean;
import weblogic.management.runtime.AppDeploymentRuntimeMBean;
import weblogic.management.runtime.DeploymentManagerMBean;
import weblogic.management.runtime.DeploymentProgressObjectMBean;
import java.util.Hashtable;
import java.util.Properties;
import javax.management.MBeanServerConnection;
import javax.management.Notification;
import javax.management.NotificationListener;
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 JMXDeploymentExample {
// Deployment Manager JMX proxy
DeploymentManagerMBean deploymentManager;
// Domain Runtime MBean Server connection
MBeanServerConnection connection;
private void setUp() throws Exception {
System.out.println("*** Setting up...");
// Get connection to the Domain Runtime MBean Server.
// For more information, see "Make Remote Connections to an MBean Server"
// in Developing Custom Management Utilities Using JMX for Oracle WebLogic Server.
connection = getDomainRuntimeJMXConnection();
// Get DeploymentManager JMX proxy.
// For more information, see Oracle WebLogic Server MBean Reference.
DomainRuntimeServiceMBean svcBean = (DomainRuntimeServiceMBean)
weblogic.management.jmx.MBeanServerInvocationHandler.newProxyInstance(
connection, new ObjectName(DomainRuntimeServiceMBean.OBJECT_NAME));
deploymentManager = svcBean.getDomainRuntime().getDeploymentManager();
// Add a JMX notification listener that outputs the JMX notifications generated during deployment operations.
connection.addNotificationListener(new ObjectName("com.bea:Name=DeploymentManager,Type=DeploymentManager"),
new DeployListener(), null, null);
}
/*
* Demonstrates synchronously deploying an application.
*/
private void deploySynchronously() throws Exception {
System.out.println("*** Deploying SimpleApp...");
// This form of the deploy operation is synchronous.
// Errors are still returned through a progress object.
// By default, the SimpleApp is deployed to all servers.
DeploymentProgressObjectMBean progressObj = deploymentManager.deploy(
"SimpleApp", "/apps/simpleapp.war", /* no plan */ null);
printCompletionStatus(progressObj);
}
/*
* Demonstrates asynchronously deploying an application to a server instance.
*/
private void deployASynchronously() throws Exception {
System.out.println("*** Deploying VersionedApp...");
// This form of the deploy operation is asynchronous.
// The caller should utilize the returned progress object to monitor the progress of the deployment.
Properties deploymentOptions = new Properties();
deploymentOptions.put("appVersion", "V1");
deploymentOptions.put("planVersion", "P1");
DeploymentProgressObjectMBean progressObj = deploymentManager.deploy("VersionedApp", "/apps/app-v1.war",
new String[] { "myserver" },
"/apps/app-v1-plan.xml", deploymentOptions);
waitForCompletion(progressObj, 200);
}
/*
* Demonstrates using a deployment progress object to display the status of the deployment operation.
*/
private void printCompletionStatus(DeploymentProgressObjectMBean progressObj) throws Exception {
System.out.println(" State: " + progressObj.getState());
if ("STATE_FAILED".equals(progressObj.getState())) {
Exception[] exceptions = progressObj.getRootExceptions();
for (int i = 0; exceptions != null && i < exceptions.length; i++)
System.out.println(" Exception: " + exceptions[i]);
}
}
/*
* Demonstrates using a deployment progress object to wait for the completion of the deployment operation.
*/
private void waitForCompletion(DeploymentProgressObjectMBean progressObj, int timeoutSecs) throws Exception {
for (int i = 0; i < timeoutSecs; i++) {
String state = progressObj.getState();
if ("STATE_COMPLETED".equals(state) || "STATE_FAILED".equals(state))
break;
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException ex) {
//ignore
}
}
printCompletionStatus(progressObj);
}
/*
* Demonstrates stopping an application asynchronously.
*/
private void stopAsynchonously() throws Exception {
System.out.println("*** Stopping SimpleApp...");
// The DeploymentManagerMBean is used for the initial deployment of an application.
// After the initial deployment, the AppDeploymentRuntimeMBean is used for stop, start,
// redeploy, and undeploy of an application.
AppDeploymentRuntimeMBean appRuntime = deploymentManager.lookupAppDeploymentRuntime("bmw");
Properties deploymentOptions = new Properties();
deploymentOptions.put("gracefulIgnoreSessions", "true");
DeploymentProgressObjectMBean progressObj = appRuntime.stop(new String[]{"AdminServer"}, deploymentOptions);
waitForCompletion(progressObj, 200);
}
/*
* Demonstrates using an AppDeploymentRuntimeMBean to undeploy an application.
*/
private void undeploySynchronously() throws Exception {
System.out.println("*** Undeploying SimpleApp...");
// The DeploymentManagerMBean is used for the initial deployment of an application.
// After the initial deployment, the AppDeploymentRuntimeMBean is used for stop, start,
// redeploy, and undeploy of an application.
AppDeploymentRuntimeMBean appRuntime = deploymentManager.lookupAppDeploymentRuntime("bmw");
DeploymentProgressObjectMBean progressObj = appRuntime.undeploy();
printCompletionStatus(progressObj);
}
/*
* Demonstrates the notifications that are generated by WebLogic Server deployment operations.
*/
private class DeployListener implements NotificationListener {
public void handleNotification(Notification notification, Object handback) {
System.out.println(" Notification from DeploymentManagerMBean");
System.out.println(" notification type: " + notification.getType());
String userData = (String)notification.getUserData();
System.out.println(" userData: " + userData);
}
}
private MBeanServerConnection getDomainRuntimeJMXConnection() throws Exception {
JMXServiceURL serviceURL = new JMXServiceURL("t3", "192.168.50.238", 7001,
"/jndi/weblogic.management.mbeanservers.domainruntime");
Hashtable h = new Hashtable();
h.put(Context.SECURITY_PRINCIPAL, "weblogic");
h.put(Context.SECURITY_CREDENTIALS, "123456");
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
JMXConnector connector = JMXConnectorFactory.connect(serviceURL, h);
MBeanServerConnection connection = connector.getMBeanServerConnection();
return connection;
}
public static void main(String args[]) throws Exception {
JMXDeploymentExample example = new JMXDeploymentExample();
example.setUp();
example.deploySynchronously();
// example.deployASynchronously();
// example.stopAsynchonously();
example.undeploySynchronously();
}
}