Préparation d'un module et ajout à une application existante par programmation

Vous pouvez ajouter un module à une application existante via la console d'administration, l'outil wsadmin ou la programmation. Cet exemple permet d'ajouter 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 Référence > Interfaces Mbean.

Avant de pouvoir ajouter un module à une application sur WebSphere Application Server, vous devez installer cette dernière.

Pourquoi et quand exécuter cette tâche

Procédez comme suit pour ajouter un module à une application par programmation :

Procédure

  1. Créez une instance de contrôleur de déploiement d'application pour insérer dans le fichier de module les informations de liaison.
  2. Enregistrez ces informations dans le module.
  3. Définissez les options d'installation.
  4. Si la phase de préparation (remplissage du fichier EAR) n'a pas lieu, procédez comme suit :
    1. Créez un tableau d'options à transmettre à l'API MBean updateApplication.
    2. Créez un tableau pour les relations module/serveur et ajoutez-le à celui d'options.
  5. Connectez-vous à WebSphere Application Server.
  6. Créez le proxy de gestion d'applications.
  7. Créez le filtre de notification.
  8. Ajoutez le module d'écoute.
  9. Ajoutez le module à l'application.
  10. Indiquez la cible pour le nouveau module.
  11. Patientez un instant pour que le programme ne prenne pas fin.
  12. Ecoutez les notifications JMX (Java Management Extensions) pour connaître la fin de l'opération.

Résultats

Une fois le code exécuté, le module est ajouté à l'application.

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.
[Windows]
String moduleName = "C:\apps\foo.jar";
[Linux][HP-UX][Solaris][AIX][z/OS][IBM i]
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);

Si le module ajouté à l'application ne possède pas de liaisons, ajoutez-les afin que l'ajout aboutisse. Rassemblez et ajoutez les liaisons à l'aide des API publiques fournies avec le produit. Voir la documentation Java au sujet de l'instance com.ibm.websphere.management.application.client.AppDeploymentController, afin d'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();

//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);
        }
    }
}

Que faire ensuite

Une fois le module ajouté avec succès, supprimez le programme d'écoute et quittez l'application.


Icône indiquant le type de rubrique Rubrique de tâche



Icône d'horodatage Dernière mise à jour: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=tjmx_add_module
Nom du fichier : tjmx_add_module.html