Hi,

Jay SenSharma

Jay SenSharma

It is not a good approach to deploy an application using JMX as already there are various useful tools available to do the same like ANT, WLST or weblogic.Deployer. Still due to certain specific requirements we may want to deploy it through a Plain Java code. So we developed an article sometimes back for that http://middlewaremagic.com/weblogic/?p=483 . But as the previous code needed some enhancement to deploy the applications on the selective targets.

So based on the comment of Mr. Ram Bonala http://middlewaremagic.com/weblogic/?p=483#comment-4286 here we are going to see how we can use JMX code to deploy an application on a particular Managed Server or to a Chosen target.

NOTE:

We need to make sure that the  ”Lock & Edit” button is present in the AdminConsole. By default when we run our Server on Production mode then we can see the Change Center butttone (Lock & Edit).   If you are running in Development Mode then please make sure that the “Lock & Edit” button is present in the admin console. If it is not present then Please login to admin-console  and then click on “Preferences —> Automatically Acquire Lock and Activate Changes (UNCHECK)” this check box .

Step1). Now Suppose we want too deploy two EAR files on WebLogic Servers “FirstEAR.ear” and “SecondEAR.ear” using the JMX Code then write the following jmx code in your local or remote file system .
“ApplicationDeployment_ManagedServer.java”

import java.io.*;
import weblogic.deploy.api.tools.*;  //SesionHelper
import weblogic.deploy.api.spi .*;  //WebLogicDeploymentManager
import weblogic.deploy.api.spi.DeploymentOptions;
import javax.enterprise.deploy.spi.TargetModuleID;
import javax.enterprise.deploy.spi.status.ProgressObject;
import javax.enterprise.deploy.spi.status.DeploymentStatus;
import javax.enterprise.deploy.shared.ModuleType;
import javax.enterprise.deploy.spi.Target;

import javax.enterprise.deploy.shared.*;

public class ApplicationDeployment_ManagedServer
{
     public static void main(String ar[]) throws Exception
		{
			ApplicationDeployment_ManagedServer appDeploy=new ApplicationDeployment_ManagedServer();
			String protocol="t3";
			String hostName="10.10.10.10";
			String portString="7001";
			String adminUser="weblogic";
			String adminPassword="weblogic";

			WebLogicDeploymentManager deployManager=SessionHelper.getRemoteDeploymentManager( protocol,hostName,portString,adminUser,adminPassword);
			System.out.println("\n\t WebLogicDeploymentManager: "+deployManager);
			DeploymentOptions options = new DeploymentOptions();
                        options.setRemote(true);
                        options.setStageMode(DeploymentOptions.NOSTAGE);
                        options.setTimeout(10000);   //Deployment will timeput in 10 seconds...change it accordingly
			System.out.println("\n\t DeploymentOptions: "+options);

			Target targets[]=deployManager.getTargets();
			System.out.println("------------------------------------------------------------");
			System.out.println("\t Application Can be Deployed in the Following targets:");
			int i=0;
			for (i=0;i<targets.length;i++)
					{
					   System.out.println("\n"+i+"   => Available Deployment Targets Are Below : "+targets[i]);
				        }
                        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                        System.out.print("\n\n Please Enter the Target Number in which you want to deploy Your Application: ");
                        int targetNumber=Integer.parseInt(br.readLine());
			Target deployTargets[]=new Target[1];
			//SUPPOSE You Chose it TO BE DEPLOYWD ON Managed Server ...
			deployTargets[0]=targets[targetNumber];
                        br.close();
            //-------------DEPLOYING FIRST APPLICATION-------------//
			String applicationDisplayName="FirstEARApplication";
			options.setName(applicationDisplayName);
			ProgressObject processStatus=deployManager.distribute(deployTargets, new File("FirstEAR.ear"), null,options);
			Thread.sleep(500);
			processStatus=deployManager.deploy(deployTargets, new File("FirstEAR.ear"), null,options);
			DeploymentStatus deploymentStatus=processStatus.getDeploymentStatus() ;
			System.out.println("\n\n\t For FirstEAR.ear DeploymentStayus.getState(): "+deploymentStatus.getState() +"\n\n");

			System.out.println("Sleeping for atleast 5-Seconds. Sothat the Deployment Activation gets Completed Successfully.");
			Thread.sleep(5000);

            //-------------DEPLOYING SECOND APPLICATION-------------//
			deployManager=null;
			deployManager=SessionHelper.getRemoteDeploymentManager( protocol,hostName,portString,adminUser,adminPassword);
			applicationDisplayName="SecondEARApplication";
			options.setName(applicationDisplayName);
			processStatus=deployManager.distribute(deployTargets, new File("SecondEAR.ear"), null,options);
			Thread.sleep(500);
			Thread.sleep(5000);
			processStatus=deployManager.deploy(deployTargets, new File("SecondEAR.ear"), null,options);
			deploymentStatus=processStatus.getDeploymentStatus() ;
			System.out.println("\n\n\t For SecondEAR.ear DeploymentStayus.getState(): "+deploymentStatus.getState() +"\n\n");
                        deployManager.release();
		}
}

Step2). Now open a Shell prompt then run “. ./setWLSEnv.sh” or “setWLSEnv.cmd” to make sure that PATH and CLASSPATH are set properly. then compile and run the following program:

[UserName@localhost Deploy_WLS]$ javac ApplicationDeployment_ManagedServer.java
[UserName@localhost Deploy_WLS]$ java ApplicationDeployment_ManagedServer

	 WebLogicDeploymentManager: weblogic.deploy.api.spi.deploy.WebLogicDeploymentManagerImpl@2b3d9460

	 DeploymentOptions: {isRetireGracefully=true,isGracefulProductionToAdmin=false,isGracefulIgnoreSessions=false,rmiGracePeriod=-1,retireTimeoutSecs=-1,undeployAllVersions=false,archiveVersion=null,planVersion=null,isLibrary=false,libSpecVersion=null,libImplVersion=null,stageMode=nostage,clusterTimeout=3600000,altDD=null,altWlsDD=null,name=null,securityModel=null,securityValidationEnabled=false,versionIdentifier=null,isTestMode=false,forceUndeployTimeout=0,defaultSubmoduleTargets=true,timeout=10000deploymentPrincipalName=null}
------------------------------------------------------------
	 Application Can be Deployed in the Following targets:

0   => Available Deployment Targets Are Below : AdminServer/server
1   => Available Deployment Targets Are Below : MS-1/server
2   => Available Deployment Targets Are Below : MS-2/server
3   => Available Deployment Targets Are Below : FrontEnd_MS/server
4   => Available Deployment Targets Are Below : TestCluster/cluster

 Please Enter the Target Number in which you want to deploy Your Application: 1
<Jun 21, 2011 4:48:03 PM IST> <Info> <J2EE Deployment SPI> <BEA-260121> <Initiating distribute operation for application, FirstEARApplication [archive: FirstEAR.ear], to MS-1 .>
<Jun 21, 2011 4:48:04 PM IST> <Info> <J2EE Deployment SPI> <BEA-260121> <Initiating deploy operation for application, FirstEARApplication [archive: FirstEAR.ear], to MS-1 .>

	 For FirstEAR.ear DeploymentStayus.getState(): running

Sleeping for atleast 5-Seconds. Sothat the Deployment Activation gets Completed Successfully.
<Jun 21, 2011 4:48:09 PM IST> <Info> <J2EE Deployment SPI> <BEA-260121> <Initiating distribute operation for application, SecondEARApplication [archive: SecondEAR.ear], to MS-1 .>
<Jun 21, 2011 4:48:15 PM IST> <Info> <J2EE Deployment SPI> <BEA-260121> <Initiating deploy operation for application, SecondEARApplication [archive: SecondEAR.ear], to MS-1 .>

	 For SecondEAR.ear DeploymentStayus.getState(): running

<Jun 21, 2011 4:48:15 PM IST> <Warning> <JNDI> <BEA-050001> <WLContext.close() was called in a different thread than the one in which it was created.>

.
.
Thanks
Jay SenSharma