准备模块并通过编程将它添加至现有的应用程序
您可以通过管理控制台、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.
![[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";
//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);
AppDeploymentController controller = AppManagementFactory.readArchiveForUpdate(
moduleName,
moduleURI,
AppConstants.APPUPDATE_ADD,
preferences,
null);
如果您添加到应用程序的模块缺少任何绑定,那么添加绑定以便模块的添加能够起作用。通过使用与产品一起提供的公共 API 来收集并添加绑定。请参阅有关 com.ibm.websphere.management.application.client.AppDeploymentController 实例的 Java 文档, 以了解有关如何收集特定于WebSphere Application Server 的绑定信息并使用它来填充任务的更多信息。AppDeploymentController 实例包含基于 XML 的部署描述符中定义的元数据,以及输入模块的Java 类中定义的注释。
//After you collect all the binding information, save it in the module.
controller.saveAndClose();
//Get the installation options.
Hashtable options = controller.getAppDeploymentSavedResults();
//Connect the administrative client, AdminClient, to WebSphere Application Server.
AdminClient client = ...;
//Create the application management proxy.
AppManagement proxy = AppManagementProxy.getJMXProxyForClient (client);
//Update the existing application, MyApp, by adding the module.
String appName = "MyApp";
options.put (AppConstants.APPUPDATE_CONTENTTYPE,
AppConstants. APPUPDATE_CONTENT_MODULEFILE);
//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);
//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_ADD,
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(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);
}
}
}
下一步做什么
成功添加模块后,请移除侦听器,然后退出。