通过编程准备并更新模块
您可以通过管理控制台、wsadmin 工具或编程来更新现有应用程序的模块。在更新模块时,您将现有模块替换为新版本。使用此示例,通过编程来更新模块。
开始之前
此任务假设您对 MBean 编程有基本的了解。有关 MBean 编程的信息,请参阅 MBean Java™ 应用程序编程接口 (API) 文档。 在此信息中心中,单击 。
您必须首先安装了应用程序,才能更新 WebSphere® Application Server 上的模块。
关于此任务
执行以下各项任务,从而通过编程更新模块。
过程
结果
成功运行代码之后,现有模块就被替换为新模块了。
示例
以下示例显示了根据先前几个步骤将模块添加到应用程序的方法。为了打印方便,某些语句被拆分为几行。
//Inputs:
//moduleName specifies the name of the module that you add to the application.
//moduleURI specifies a URI that gives the target location of the module
// archive contents on a file system. The URI provides the location of the new
// module after installation. The URI is relative to the application URL.
//uniquemoduleURI specfies the URI that gives the target location of the
// deployment descriptor file. The URI is relative to the application URL.
//target specifies the cell, node, and server on which the module is installed.
//appName specifies the name of the application to update.
![[Windows]](../images/windows.gif)
String moduleName = "C:\apps\foo.jar";
![[Linux]](../images/linux.gif)
![[HP-UX]](../images/hpux.gif)
![[Solaris]](../images/solaris.gif)
![[AIX]](../images/aixlogo.gif)
![[z/OS]](../images/ngzos.gif)
![[IBM i]](../images/iseries.gif)
String moduleName = "/apps/foo.jar";
String moduleURI = "Increment.jar";
String uniquemoduleURI = "Increment.jar+META-INF/ejb-jar.xml";
String target = "WebSphere:cell=cellname,node=nodename,server=servername";
String appName = "MyApp";
//Get the administrative client to connect to
//WebSphere Application Server.
AdminClient client = ...;
AppManagement proxy = AppManagementProxy.getJMXProxyForClient (client);
Vector tasks = proxy.getApplicationInfo (appName, new Hashtable(), null);
//Create an application deployment controller instance, AppDeploymentController,
//to populate the Java archive (JAR) file with binding information.
//The binding information is WebSphere Application Server-specific deployment information.
Hashtable preferences = new Hashtable();
preferences.put (AppConstants.APPDEPL_LOCALE, Locale.getDefault());
preferences.put (AppConstants.APPUPDATE_CONTENTTYPE, AppConstants.APPUPDATE_CONTENT_MODULEFILE);
// When a module is update, there are three possible merge actions:
// AppConstants.APPREDEPL_DEFAULT_MERGE (the default merge action),
// This option specifies during the update action, the binding information from
// the new version of the EAR file or module is preferred
// over the corresponding binding information from the old version. If any element
// of binding is missing in the new version, the corresponding
// element from the old version is used.
// AppConstants.APPREDEPL_IGNORE_OLDBND,
// This option specifies that during the update action, the binding information
// from the new version of the module is preferred over
// the corresponding binding information from the old version. The bindings from
// the old version of the application or module are ignored.
// AppConstants.APPREDEPL_IGNORE_NEWBND
// This option specifies that during the update action, binding information from
// the old version of the module is preferred over
// the corresponding binding information from the new version. If any element of
// the binding does not exist in the old version,
// the element from the new version is used.
//
// To find the matching configuration object to perform the merge action, the values
// of the read only fields of the task for the new module are compared
// with the values of the read only fields of the task from the existing module.
// If all the read only values match, then the appropriate merge action is
// performed.
preferences.put (AppConstants.APPREDEPL_IGNORE_NEWBND, Boolean.TRUE);
AppDeploymentController controller = AppManagementFactory.readArchiveForUpdate(
moduleName,
moduleURI,
AppConstants.APPUPDATE_UPDATE,
preferences,
tasks);
如果您更新的应用程序的模块缺少任何绑定,那么添加绑定以便模块的更新能够起作用。通过使用与产品一起提供的公共 API 来收集并添加绑定。请参阅 AppDeploymentController 实例的 Java 文档,以了解关于如何收集特定于 WebSphere Application Server 的绑定信息并用它来填充任务的更多信息。AppDeploymentController 实例包含基于 XML 的部署描述符中定义的元数据,以及输入模块的Java 类中定义的注释。
//After you collect all the binding information, save it in the module.
controller.saveAndClose();
//Create the notification filter.
NotificationFilterSupport myFilter = new NotificationFilterSupport();
myFilter.enableType (NotificationConstants.TYPE_APPMANAGEMENT);
//Add the listener.
NotificationListener listener = new AListener(_soapClient, myFilter,
"Install: " + appName, AppNotification.UPDATE);
//Get the installation options.
Hashtable options = controller. getAppDeploymentSavedResults();
//Update the existing application by adding the module.
options.put (AppConstants.APPUPDATE_CONTENTTYPE,
AppConstants. APPUPDATE_CONTENT_MODULEFILE);
//Specify the target for the new module
Hashtable mod2svr = new Hashtable();
options.put (AppConstants.APPDEPL_MODULE_TO_SERVER, mod2svr);
mod2svr.put (uniquemoduleURI, target);
proxy.updateApplication ( appName,
moduleURI,
moduleName,
AppConstants.APPUPDATE_UPDATE,
options,
null);
// Wait. The installation application programming interface (API) is
// asynchronous and so returns immediately.
// If the program does not wait here, the program ends.
Thread.sleep(300000); // Wait so that the program does not end.
}
catch (Exception e) {
e.printStackTrace();
}
}
}
// Specify the Java Management Extensions (JMX) notification listener for JMX events.
class AListener implements NotificationListener
{
AdminClient _soapClient;
NotificationFilterSupport myFilter;
Object handback;
ObjectName on;
String eventTypeToCheck;
public AListener(AdminClient cl, NotificationFilterSupport fl,
Object h, String eType) throws Exception
{
_soapClient = cl;
myFilter = fl;
handback = h;
eventTypeToCheck = eType;
Iterator iter = _soapClient.queryNames (new ObjectName(
"WebSphere:type=AppManagement,*"), null).iterator();
on = (ObjectName)iter.next();
System.out.println ("ObjectName: " + on);
_soapClient.addNotificationListener (on, this, myFilter, handback);
}
public void handleNotification (Notification notf, Object handback)
{
AppNotification ev = (AppNotification) notf.getUserData();
System.out.println ("!! JMX event Recd: (handback obj= " + handback+ "): " + ev);
//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);
}
System.exit (0);
}
}
}