[ Team LiB ] Previous Section Next Section

Using the Command Interface Commands to Build Scripts

Setting the environment and typing out the administration commands can be a test of your patience and typing skills. Luckily, we can set our environment and run our administration commands from a single command (.cmd) file or script. Scripts add greater management and administration efficiency, which we'll illustrate in an example.

For all the script examples, follow these common steps:

  1. Start the Launch Medical Records example application by navigating to All ProgramsBEA WebLogic Platform 8.1ExamplesWebLogic Server MedRecLaunch Medical Records.

  2. Open a command window and navigate to /bea/weblogic81.

    On my machine, the full path is F:\bea\weblogic81. A typical path might be C:\bea\weblogic81 or D:\bea\weblogic81.

    CAUTION

    The following example presupposes that you're running Windows. On Unix variants, your scripts will end in .sh and have different syntax.

  3. In a text editor, create a new text file and save it as getVersion.cmd.

  4. Next, we set an environment variable for the WebLogic HOME:

    
    
    @rem set environment variable for WebLogic Home
    set WL_HOME=F:\bea\weblogic81
    
  5. The following line calls the setWLSEnv.cmd file that sets our environment:

    
    
    @rem set the environment
    call %WL_HOME%\server\bin\setWLSEnv.cmd
    
  6. Finally, we can run the administration command. In this example, it will display the version of WebLogic Server software that the target server (denoted in the -url attribute) is running:

    
    
    @rem Run the Administration Command
    java weblogic.Admin -url t3://localhost:7001 -username %1 -password %2 VERSION
    

    Here %1 and %2 are parameters for the username and password, respectively.

  7. Save the file as getVersion.cmd and save it in the location F:\bea\weblogic81. The content of the getVersion.cmd file is contained in Listing 38.1.

NOTE

We have not hard-coded the username and password in the file. This is a very important practice. The authors have seen various production environments in which the username and password were hard-coded in the scripts, thereby compromising security of the system. We strongly recommend that you use parameterized scripts as a security measure.


Listing 38.1 getVersion.cmd
echo off
@rem set environment variable for WebLogic Home
set WL_HOME=F:\bea\weblogic81
@rem set the environment
call %WL_HOME%\server\bin\setWLSEnv.cmd
@rem Run the Administration Command
java weblogic.Admin -url t3://localhost:7001 -username %1 -password %2 VERSION

To run the script, navigate to the command file from the window and run the command as getVersion.cmd user pwd where user is the password for your WebLogic Server and pwd is the password.

The output returned in the command window will resemble the following:


WebLogic Server 8.1 Thu Mar 20 23:06:05 PST 2003 246620
WebLogic XMLX Module 8.1 Thu Mar 20 23:06:05 PST 2003 246620

NOTE

Your exact output will depend on which version of WebLogic Server software you're running and the variation of the date.


Listing 38.2 shows another very useful script. This script detects whether a given instance of WebLogic Server is running. You might wonder why you can't just check it under the Admin Console GUI through a browser. Well, if the administration server is down for whatever reason, the console will not run.

Open a new text file and copy the following text in a file called isServerUp.cmd.

Listing 38.2 isServerUp.cmd
@echo off
@rem set environment variable for WebLogic Home
set WL_HOME=f:\bea\weblogic81
@rem set the environment
call %WL_HOME%\server\bin\setWLSEnv.cmd
@rem Run the Administration Command
echo off
java weblogic.Admin -url t3://localhost:7001 -username %1 -password %2 CONNECT
echo off
if %ERRORLEVEL% == 1 (
echo Server is DOWN.
) else (
echo Server is UP.
)

You can run this command as


isServerUp.cmd user pwd

where user is the password for WebLogic Server and pwd is the password.

TIP

If you do manage multiple administration servers or multiple managed servers, you might want to parameterize even the server address.


Listing 38.3 presents another script that retrieves the server log file. Please note that the SERVERLOG file gets only the 500 latest entries of the current log file on the server. It does not get any archived log files.

Listing 38.3 getLog.cmd
@echo off
@rem set environment variable for WebLogic Home
set WL_HOME=f:\bea\weblogic81
@rem set the environment
call %WL_HOME%\server\bin\setWLSEnv.cmd
@rem Run the Administration Command
echo off
java weblogic.Admin -url t3://localhost:7001 -username %1 -password %2 SERVERLOG > %3
@rem echo off
echo ERRORLEVEL =  %ERRORLEVEL%
if %ERRORLEVEL%== 0 (
echo Logs retrieval SUCCESSFUL.
) else (
echo echo Logs retrieval FAILED.
del %3.
)

You can run this command as


getLog.cmd user pwd

where user is the password for your WebLogic Server and pwd is the password.

Another important place where the command-line scripts are useful is managing JDBC connection pools. In the course of an application, there might be a need to suspend and then resume a JDBC connection pool. You could do this by either running the java weblogic.Admin commands for the JDBC pool or by enclosing these commands in a script as shown in Listing 38.4.

Listing 38.4 bouncePool.cmd
@echo off
@rem set environment variable for WebLogic Home
set WL_HOME=F:\bea\weblogic81
@rem set the environment
call %WL_HOME%\server\bin\setWLSEnv.cmd
@rem Run the Administration Command
echo off
java weblogic.Admin -url t3://localhost:7001 -username %1 -password %2 SUSPEND_POOL
graphics/ccc.gif -poolName %3
pause
java weblogic.Admin -url t3://localhost:7001 -username %1 -password %2 RESUME_POOL
graphics/ccc.gif -poolName %3

You can run this command as


bouncePool.cmd user pwd poolname

where user is the password for your WebLogic Server, pwd is the password, and poolname is the connection pool.

This command suspends a JDBC connection pool and then resumes it after the user presses the Enter key. %1 and %2 are the values for the username and the password, and %3 is the name of the JDBC connection pool to be suspended (and resumed).

NOTE

The BEA documentation incorrectly mentions that SUSPEND_POOL can take an optional true or false argument to determine whether to preserve the current connection pool or to destroy and re-create it. WebLogic Server SP 1 does not support this parameter at the time of writing this book. This is a known issue and has the BEA change request number CR100625. BEA plans to fix this bug in a future release. In the meantime, you could still use the SUSPEND_POOL command which defaults to the false behavior.

The SHUTDOWN_POOL command also does not take the Boolean parameters and BEA plans to fix that in the next release.


    [ Team LiB ] Previous Section Next Section