Monday, March 2, 2009

Check for resource/configuration existence [WLST scripting Best Practices (Part-3)]

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 to create new resource/configuration in WebLogic domain then check for its existence. If the resource/configuration already exists then you might get an error similar to the following:

weblogic.descriptor.BeanAlreadyExistsException: Bean already exists

A simple approach is to use the "cd" command to navigate into that configuration MBean. If the "cd" command throws an exception then you can safely assume that the resource/configuration doesn't exists and you can continue creating.

...
### Check for the server
try:
cd('/Servers/' + mgdServerName)
print '===> Server \"' +mgdServerName+'\" already exists'
print '===> No action was performed'
exit()
except:
pass
...


The other approach is to catch the "BeanAlreadyExistsException" while creating the resource and safely exiting the script.


...
### Create the Managed Server
edit()
startEdit()
try:
create(mgdServerName, resourceType)
print '===> Created Managed Server - ' + mgdServerName
pass
except BeanAlreadyExistsException:
print '===> Server \"' +mgdServerName+'\" already exists'
print '===> No action was performed'
cancelEdit('y')
exit()
...

But with this approach you are starting an edit session and creating the server to find out it's existence. So you have to cancel the edit session once you find out that the resource that you are creating already exists if not you might get a warning message similar to the following:

You have an edit session open and you will lose all outstanding changes and your edit session will be stopped if you exit. Are you sure you would like to exit? (y/n)

So cancelling the edit session with a responce 'y' will make WLST not to prompt for user response. I shall discuss how to streamline the outputs from WLST and redirect them to an output file or log file in a later post.

WLST-BP-3 Check for the resource or configuration existence before creating them

8 comments:

  1. Bala, this is really useful stuff! Thanks for taking the time to post it!!

    (This is Del Simmons from your class in Dallas at the end of last year)

    ReplyDelete
  2. This is very usefull info.I am looking for creation of domain script which can read from .csv file and create a domain.

    This is raja from chicago class
    october 08.
    THanks for helping.
    Raja

    ReplyDelete
  3. Hi Bala,

    I am getting the same error that you have mentioned here, but I am not sure what to do with the code snippet that you have provided. Am using basic UNIX commands to configure my domain.

    ReplyDelete
  4. Hi Swagat,

    If you get that error then you are creating something that already exists. Try to change the name of the resource and rerun your script.

    ReplyDelete
  5. Hi Bala,

    Thanks for posting very good articles.

    Vijay Bheemineni.

    ReplyDelete
  6. Rather nice site you've got here. Thanks for it. I like such topics and anything that is connected to them. I would like to read more soon.

    Best wishes
    Jeph Normic

    ReplyDelete
  7. Hi Bala,

    Here is a 3rd method to check for resource existence. This is the one I prefer
    because it has no side-effect (neither change cmo, nor need edit session), but
    it only works for online WLST.

        ### Check for the server
        ...
        myMBean = getMBean('/Servers/' + mgdServerName) # WLST Online only
        if myMBean is None:
            # resource does not exist, create it
            myMBean = create(mgdServerName, 'Server')
            ...
        else:
            # resource already exists, nothing to do
        # use myMBean, no matter it was existing or has just been created
        ...

    Thanks for your blog!
    Regards,
    Gregoire Vatry

    ReplyDelete