WAS v8.5 > Administer applications and their environment > Manage applications through programming

Prepare and updating a module through programming

We can update a module for an existing application through the dmgr console, wsadmin, or programming. When you update a module, you replace the existing module with a new version. Use this example to update a module through programming.

This task assumes a basic familiarity with MBean programming. For information on MBean programming, see MBean Java API documentation. In this information center, click Reference > Mbean interfaces.

Before we can update a module on WebSphere Application Server, first install the application. Perform the following tasks to update a module through programming.

  1. Create an application deployment controller instance to populate the Java archive file with binding information.

  2. Save the binding information in the module.
  3. Get the installation options.

  4. If the preparation phase (population of the EAR file) is not performed, the do the following actions:

    1. Create an options table to be passed to the updateApplication MBean API.

    2. Create a table for module to server relations and add the table to the options table.

  5. Connect to WAS.
  6. Create the application management proxy.

  7. Create the notification filter.

  8. Add the listener.
  9. Replace the module in the application.

  10. Specify the target for the new module.
  11. Wait for some timeout so the program does not end.
  12. Listen to JMX notifications to understand completion of the operation.

  13. When the module addition is done, remove the listener and quit.


Results

After you successfully run the code, the existing module is replaced with the new one.


Example

The following example shows how to add a module to an application based on the previous steps. Some statements are split on multiple lines for printing purposes.

//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.

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 //WAS.
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 JAR file with binding information.  
//The binding information is WAS-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);

If the module that you update for the application lacks any bindings, add the bindings so the module update works. Collect and add the bindings using the public APIs provided with the product. Refer to Java documentation for the AppDeploymentController instance to learn more about how to collect and populate tasks with WAS-specific binding information. The AppDeploymentController instance contains meta-data defined in XML-based deployment descriptors as well as annotations defined in Java classes within the input module.

//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 API is 
//  asynchronous and so returns immediately. 
// If the program does not wait here, the program ends.
  Thread.sleep(300000); // Wait so the program does not end.
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }}
// Specify the 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);
        }
    }}


Related


Install an application through programming
Uninstall an application through programming
Update an application through programming
Add to, updating, or deleting part of an application through programming
Prepare a module and adding it to an existing application through programming
Delete a module through programming
Add a file through programming
Update a file through programming
Delete a file through programming


+

Search Tips   |   Advanced Search