Préparation et mise à jour d'un module par programmation
Vous pouvez mettre à jour un module pour une application existante via la console d'administration, l'outil wsadmin ou la programmation. Au moment de la mise à jour d'un module, celui existant est remplacé par une nouvelle version. Cet exemple permet de mettre à jour un module par programmation.
Avant de commencer
Cette tâche suppose une connaissance de base de la programmation MBean. Pour plus d'informations sur la programmation MBean, voir la documentation sur l'API (Application Programming Interface) Java™ MBean. Dans le centre de documentation, cliquez sur .
Avant de pouvoir mettre à jour un module sur WebSphere Application Server, vous devez tout d'abord installer l'application.
Pourquoi et quand exécuter cette tâche
Procédez comme suit pour mettre à jour un module par programmation.
Procédure
Résultats
Une fois le code exécuté, le module existant est remplacé par le nouveau.
Exemple
L'exemple qui suit explique comment ajouter un module à une application en exécutant les procédures précédentes. Certaines instructions sont présentées sur plusieurs lignes à des fins d'affichage.
//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);
Si le module mis à jour pour l'application ne possède pas certaines liaisons, ajoutez-les afin que la mise à jour aboutisse. Rassemblez et ajoutez les liaisons à l'aide des API publiques fournies avec le produit. Reportez-vous à la documentation Java relative à l'instance AppDeploymentController, pour en savoir plus sur la collecte et le remplissage de tâches avec des informations de liaisons spécifiques à WebSphere Application Server. L'instance AppDeploymentController contient des métadonnées définies dans les descripteurs de déploiement XML ainsi que des annotations définies dans les classes Java au sein du module d'entrée.
//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);
}
}
}