Monday, June 1, 2009

Use definitions for Reuse [WLST scripting Best Practices (Part-4)]

This post is a continuation of my previous post - in a series of posts about WLST scripting best practices. When you are authoring WLST scripts for automating WebLogic administration tasks, you can reuse or extend an existing logic written in WLST or Jython scripts. You can also leverage or extend logic written in Java which I will discuss later in a different post. Definition are reusable code blocks in Jython. Think of definitions as methods or functions. You can pass parameters into definitions. You can reuse definitions from the same script or from a different script. A script that contains definitions for other scripts to use is usually called as a 'Module'. If you are using a definition from a module then you should import it in your script. The definition should be defined before they get called in the script.

Let us see an example of a using definition in the same script.


#Definition for connecting to a server
def connectToServer():
username = 'weblogic'
password = 'weblogic'
url = 't3://egAdm:7001'
connect(username, password, url)

#Definition to print a running servers heap details
def printHeapDetails(server_name):
domainRuntime()
cd('/')
cd('ServerRuntimes/'+server_name+'/JVMRuntime/'+server_name)
hf = float(get('HeapFreeCurrent'))/1024
hs = float(get('HeapSizeCurrent'))/1024
hf = hf/1024
hs = hs/1024
print 'HeapFreeCurrent - ' + `hf` + 'MB'
print 'HeapSizeCurrent - ' + `hs` + 'MB'

#Definition to disconnect from a server
def disconnectFromServer():
disconnect()
exit()

#Calling connectToServer definition with no arguments
connectToServer()

#Calling printHeapDetails with arguments
printHeapDetails('AdminServer')
printHeapDetails('mgds1')
printHeapDetails('mgds2')

#Calling disconnectFromServer definition with no arguments
disconnectFromServer()


In the above script we are connecting to the administration server of a domain and then printing the heap details for the admin server and 2 other managed servers. The logic to navigate to the respective runtime mbean and printing the heap related attributes is being defined as reusable definition and called multiple times.

WLST-BP-4 Use definitions for Reuse

7 comments:

  1. hi bala really your posts are too good i need your gmail id . i try to find your mail id but i can't , this is kamalakkannana from trichy . working as a weblogic administrator in Butp. plz give me your gmail id , now i am working on JMX and WLST i need some help from your plz give me your mail id . thank u

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Charan RamireddyJuly 12, 2009 at 8:10 AM

    Hi bala,

    What tool are you using for writing scripts. Could you tell me how do you do that?
    I was trying WLST in Eclipse IDE with JyDT plugin but I was unable to use it.
    Could you tell me how can I use it if you know that

    Thanks
    Charan
    charan314@gmail.com

    ReplyDelete
  4. I'm stuck when trying to put these methods in a module.

    The WLST methods and names like connect() are not understood by the module.

    I have a mywlst.py file that contains the same imports as the main file and with a:
    def connectToAdminServer(host, port, username, password):
    URL="t3://"+host+":"+port
    connect(username, password, URL)

    In the main file, I have:
    import mywlst

    connectToAdminServer("foo", ...)

    The result is:

    Problem invoking WLST - Traceback (innermost last):
    File "...main.py", line 15, in ?
    File "mywlst.py", line 16, in connectToAdminServer
    NameError: connect

    How am I supposed to get the module to recognize the WLST methods ?

    ReplyDelete
  5. Hi Atul,
    I desperately need ur help
    I are doing OIM 9101 installation on Red Hat Linux 5.4 64-bit version. I’m using Oracle Database 11g Release 1 and weblogic 10.3.0. The OIM installation fails with the following error.

    Error Detais

    weblogic-setup.xml - line 196

    ***************Exact Error from setup_weblogic.log**********************
    BUILD FAILED
    /oracle/OIM_Server/xellerate/setup/setup.xml:443: The following error occurred while executing this line:
    /oracle/OIM_Server/xellerate/setup/weblogic-setup.xml:196: Could not create task or type of type: wlst.
    Ant could not find the task or a class this task relies upon.

    This is common and has a number of causes; the usual
    solutions are to read the manual pages then download and
    install needed JAR files, or fix the build file:
    - You have misspelt ‘wlst’.
    Fix: check your spelling.
    - The task needs an external JAR file to execute
    and this is not found at the right place in the classpath.
    Fix: check the documentation for dependencies.
    Fix: declare the task.
    - The task is an Ant optional task and the JAR file and/or libraries
    implementing the functionality were not found at the time you
    yourself built your installation of Ant from the Ant sources.
    Fix: Look in the ANT_HOME/lib for the ‘ant-’ JAR corresponding to the
    task and make sure it contains more than merely a META-INF/MANIFEST.MF.
    If all it contains is the manifest, then rebuild Ant with the needed
    libraries present in ${ant.home}/lib/optional/ , or alternatively,
    download a pre-built release version from apache.org
    - The build file was written for a later version of Ant
    Fix: upgrade to at least the latest release version of Ant
    - The task is not an Ant core or optional task
    and needs to be declared using .
    - You are attempting to use a task defined using
    or but have spelt wrong or not
    defined it at the point of use

    Remember that for JAR files to be visible to Ant tasks implemented
    in ANT_HOME/lib, the files must be in the same directory or on the
    classpath
    ********************************************

    I have set all the needed envi variables, such as JAVA_HOME, ORACLE_HOME, ANT_HOME and much more before running install_server.sh.

    I can understand that the problem lies with the ANT version and JAR files. The Ant version We are using is 1.6.5. But, our earlier attempts at resolving the issue has mostly ended in vain.

    Kindly Help me resolve the issue.

    Regards
    Srini

    ReplyDelete
  6. Bala

    can put the "def XXX().." in an external file and include them at runtime

    thanks

    ReplyDelete
  7. Bala

    can put the "def XXX().." in an external file and include them at runtime

    thanks

    ReplyDelete