Sometimes it is necessary to undeploy an application when creating a custom Java administrative client programs using the WebSphere Application Server administrative Java API. The following java code will undeploy an application from WebSphere Application Server.
Note:
This document assumes that the user:
PREREQUISITES:
For the sample to work, the WebSphere Application Server administrative APIs (e.g. admin.jar, wsexception.ajr, etc.) and Java Management Extentions (JMX) must be accessible by the Java Runtime Environment.
CODE:
package com.Strategy.examples;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Locale;
import java.util.Properties;
import javax.management.Notification;
import javax.management.NotificationFilterSupport;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import com.ibm.websphere.management.AdminClient;
import com.ibm.websphere.management.AdminClientFactory;
import com.ibm.websphere.management.application.AppConstants;
import com.ibm.websphere.management.application.AppManagement;
import com.ibm.websphere.management.application.AppManagementProxy;
import com.ibm.websphere.management.application.AppNotification;
import com.ibm.websphere.management.exception.ConnectorException;
public class UndeployMSTRWebOnWebSphere {
private String cell;
private String node;
private String server;
private AdminClient adminClient;
public UndeployMSTRWebOnWebSphere(String host,String adminport,String usr,String pwd,String cell, String node, String server) {
this.cell = cell;
this.node = node;
this.server = server;
Properties connectProps = new Properties();
connectProps.setProperty(
AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
connectProps.setProperty(AdminClient.CONNECTOR_HOST, host);
connectProps.setProperty(AdminClient.CONNECTOR_PORT, adminport);
connectProps.setProperty(AdminClient.USERNAME, usr);
connectProps.setProperty(AdminClient.PASSWORD, pwd);
// Get an AdminClient based on the connector properties
try {
this.adminClient = AdminClientFactory.createAdminClient(connectProps);
System.out.println("Connected to DeploymentManager");
} catch (ConnectorException e) {
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) {
String host = "host";
String adminport = "60006";
String usr = "usr";
String pwd = "pwd";
String cell = "Node01Cell";
String node = "Node01";
String server = "server1";
UndeployMSTRWebOnWebSphere deply = new UndeployMSTRWebOnWebSphere(host,adminport,usr,pwd,cell,node,server);
String contextname = "Strategy";
deply.undeployApplication(contextname);
}
public boolean undeployApplication(String name) {
try {
Hashtable options = new Hashtable();
options.put (AppConstants.APPDEPL_LOCALE, Locale.getDefault());
//Create the notification filter.
NotificationFilterSupport myFilter = new NotificationFilterSupport();
myFilter.enableType (AppConstants.NotificationType);
//Add the listener.
ActionListener listener = new ActionListener(adminClient,
myFilter, "Unintall: " + name, AppNotification.UNINSTALL);
// Uninstall the application.
AppManagement appM = AppManagementProxy.getJMXProxyForClient (adminClient);
appM.uninstallApplication (name, options, null);
// Wait for some timeout. The installation application programming interface (API) is
// asynchronous and so returns immediately.
// If the program does not wait here, the program ends.
Thread.sleep(20000);
while(listener.status.equals("InProgress")) {
Thread.sleep(5000);
}
return true;
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
private class ActionListener implements NotificationListener {
private AdminClient _soapClient;
private NotificationFilterSupport myFilter;
private Object handback;
private ObjectName on;
private String eventTypeToCheck;
public String status = "";
public String uid;
public ActionListener(AdminClient adminClient, NotificationFilterSupport fl,
Object h, String eType) throws Exception {
_soapClient = adminClient;
myFilter = fl;
handback = h;
eventTypeToCheck = eType;
Iterator iter = _soapClient.queryNames(
new ObjectName("WebSphere:type=AppManagement,*"), null)
.iterator();
on = (ObjectName) iter.next();
_soapClient.addNotificationListener(on, this, myFilter, handback);
}
public void handleNotification(Notification notf, Object handback) {
AppNotification ev = (AppNotification) notf.getUserData();
status = ev.taskStatus;
System.out.println("status=" + ev.taskStatus + ", message=" + ev.message);
//When the installation is done, remove the listener and quit.
if (ev.taskName.equals(eventTypeToCheck)
&& (ev.taskStatus.equals(AppNotification.STATUS_COMPLETED) ev.taskStatus
.equals(AppNotification.STATUS_FAILED))) {
try {
_soapClient.removeNotificationListener(on, this);
} catch (Throwable th) {
System.out.println("Error removing listener: " + th);
}
}
}
}
}
For information on how to accomplish the same task using the WebSphere WSAdmin utility, refer to the following Strategy Knowledge Base technical note:
KB16570: How to undeploy an application in WebSphere Application Server via the WSAdmin utility
CUSTOMIZATION WARNING:
This customization is provided as a convenience to Strategy users and is only directly applicable to the version stated. While this code may apply to other releases directly, Strategy Technical Support makes no guarantees that the code provided will apply to any future or previous builds. In the event of a code change in future builds, Strategy Technical Support makes no guarantee that an updated version of this particular customization will be provided. In the event of a code change in future builds, Strategy may not be able to provide additional code on this matter even though this customization is provided at this time for this specific build. For enhancements to this customization or to incorporate similar functionality into other versions, contact your Account Executive to inquire about Strategy Consulting assistance.