Sun Java System Application Server Enterprise Edition 8.1 2005Q2 Developer's Guide

- Previous: Starting an Application Server
- Next: Displaying the AMX MBean Hierarchy

## Deploying an Archive

The `uploadArchive()` and `deploy()` methods demonstrate how to upload and deploy a J2EE archive file.

##### Example 16–4 Uploading an archive

    [...]
    uploadArchive ( final File archive  ) throws IOException
    {
        final FileInputStream input = new FileInputStream( archive );
        final long length = input.available();
        final DeploymentMgr mgr = getDomainRoot().getDeploymentMgr();
        final Object uploadID = mgr.initiateFileUpload( length );
        try
        {
            [...]
        }
        finally
        {
            input.close();
        }
        return( uploadID );
    }
    [...]

The `uploadArchive()` method creates a standard Java `FileInputStream` instance called `input`, to upload the archive `archive`. It then obtains the AMX deployment manager running in the application server domain, by calling the `DomainRoot.getDeploymentMgr()` method.

A call to `com.sun.appserv.management.deploy.initiateFileUpload` starts the upload of `archive`. The `initiateFileUpload()` method automatically issues an upload ID, that `uploadArchive()` returns when it is called by `deploy()`.

##### Example 16–5 Deploying an archive

    [...]
    deploy ( final File archive ) throws IOException
    {
        final Object uploadID = uploadArchive(archive);
        final DeploymentMgr mgr    = getDomainRoot().getDeploymentMgr();
        final Object deployID = mgr.initDeploy( );
        final DeployNotificationListener myListener =
            new DeployNotificationListener( deployID);
        mgr.addNotificationListener( myListener, null, null);
        try
        {
            final Map    options  = new HashMap();
            options.put( DeploymentMgr.DEPLOY_OPTION_VERIFY_KEY,
                Boolean.TRUE.toString() );
            options.put( DeploymentMgr.DEPLOY_OPTION_DESCRIPTION_KEY,
                "description" );
            mgr.startDeploy( deployID, uploadID, null, null);
            while ( ! myListener.isCompleted() )
            {
                try
                {
                    println( "deploy: waiting for deploy of " + archive);
                    Thread.sleep( 1000 );
                }
                catch( InterruptedException e )
                {
                }
            }
            final DeploymentStatus status = myListener.getDeploymentStatus();
            println( "Deployment result: " + getStageStatusString(
                status.getStageStatus() ) );
            if ( status.getStageThrowable() != null )
            {
                status.getStageThrowable().printStackTrace();
            }
        }
        finally
        {
            try
            {
                mgr.removeNotificationListener( myListener );
            }
            catch( Exception e )
            {
            }
        }
    }
    [...]

The `deploy()` method calls `uploadArchive` to get the upload ID for `archive`. It then identifies the deployment manager by calling `DomainRoot.getDeploymentMgr()`. A call to `DeploymentMgr.initDeploy()` initializes the deployment and obtains a deployment ID, which is used to track the progress of the deployment.

A JMX notification listener, `myListener`, is created and activated to listen for notifications regarding the deployment of `deployID`.

Deployment is started by calling the `DeploymentMgr.startDeploy()` method and providing it with the `deployID` and `uploadID`.

While the deployment is continuing, `myListener` listens for the completion notification and `DeploymentStatus` keeps you informed of the status of the deployment by regularly calling its `getStageStatus()` method. Once the deployment is complete, the listener is closed down.

Caution –

Some of the behavior of the `com.sun.appserv.management.deploy` API is unpredictable, and it should be used with caution.

- Previous: Starting an Application Server
- Next: Displaying the AMX MBean Hierarchy

- © 2010, Oracle Corporation and/or its affiliates