Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Python

Create a JMS Configuration on Oracle Weblogic 11g using WSLT

5.00/5 (2 votes)
23 Jun 2019CPOL3 min read 19.1K   68  
Python Script using WSLT

Introduction

Deploying an artifact or a package is commonly a delicate work on any environment that you are willing to install your application, at the expense of optimizing your installation, a good note to your work could the use of a Script File.

Background

My brief experience in terms of application deployment is "I do not trust me or anyone else to deploy my application". It is because depending on how large your team is and the time to be scheduled, I am always susceptible to make a mistake in some part of the documentation and it could be some kind of a tragedy to me that my package could be rejected even in a production installation, furthermore an application assembler, application server administrator, database administrator could make a mistake too.

On the other hand, Application Server has included an administration tool for quick manage and configuration that runs on a script file, there is one for Websphere Application Server, Oracle Weblogic, Microsoft IIS and of course, our friendly JBOSS Application Server.

This short tutorial describes how to run a Script File make it on Python/IronPython (linux/windows) in a console using Oracle Weblogic WLST.

Using the Code

There is another method to create a Server Configuration on Oracle Weblogic. If you cannot use the console, you could execute a script with your full configuration using WSLT.

This model solves problems like deployment on other environments like Quality Assurance and Production where you don't have any permission to access from console, only a deployer will run the script and install any package on the server. You should follow the next steps:

  1. First go to your Oracle Weblogic Home, for example, the location of my Oracle Weblogic installation is:
    [root@USER25 bin]# cd /root/Oracle/Middleware/11.1.2.4.0/   
  2. Go to the next folder...
    [root@USER25 bin]# cd /root/Oracle/Middleware/11.1.2.4.0/wlserver_10.3/server/bin   
  3. Open a console and keep in mind you have to run Oracle Weblogic and then execute:
    [root@USER25 bin]# ./setWLSEnv.sh  

    Even there is another using the following sh or bat file located into:

    ../common/bin/wlst.sh 
  4. Running the above command, you will have the opportunity to run the script on Python/IronPython on your weblogic on running state, I will show you a script example that runs several tasks to create a JMS Server, JMS Module, Queue, Dead Letter Queue and DESTINATION QUEUE that is the object we need for this tutorial, so this is the code:
    Python
    # OCTAVIO SANCHEZ
    # SOFTWARE ENGINEER
     
    from java.io import FileInputStream
    from java.util import Properties
     
    def loadProperties(fileName):
        properties = Properties()
        input = FileInputStream(fileName)
        properties.load(input)
        input.close()
     
        result= {}
        for entry in properties.entrySet(): result[entry.key] = entry.value
     
        return result 
     
    properties = loadProperties("local.properties")
     
    username = properties['username']
    password = properties['password']
    url = properties['providerURL'] 
     
    connect(username, password, url)
    adminServerName = cmo.adminServerName
     
    try:
        print "Inicializando ..."
        domainName = cmo.name
        cd("Servers/%s" % adminServerName)
        adminServer = cmo 
     
        def deleteIgnoringExceptions(mbean):
            try: delete(mbean)
            except: 
                print "delete mbean"
                pass
        
        def startTransaction():
            edit()
            startEdit()
     
        def endTransaction():
            save()
            activate(block="true") 
        
        def createDLQ(qname, qjndiname,dkName):
            print "createDLQ() START"
            cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName)
            errorQueue = create(qname, "Queue")
            errorQueue.JNDIName = qjndiname
     
            errorQueue.subDeploymentName = subDeploymentName
                    print dkName
     
            cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName)                        
            cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName+'/Queues/'+qname)
            set('DestinationKeys',jarray.array([String(dkName)],String))
     
            cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName+
                                    '/Queues/'+qname+'/DeliveryFailureParams/'+qname)
            cmo.setRedeliveryLimit(1)
            cmo.setExpirationPolicy('Log')
            
     
            cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName+
                                    '/Queues/'+qname+'/DeliveryParamsOverrides/'+qname)
            cmo.setRedeliveryDelay(5000)
     
            cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName+
                                    '/Queues/'+qname+'/DeliveryFailureParams/'+qname)
            cmo.unSet('ErrorDestination') 
     
            cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName+
                                    '/Queues/'+qname+'/DeliveryParamsOverrides/'+qname)
            cmo.setPriority(-1)
            cmo.setDeliveryMode('Persistent')
            cmo.setTimeToDeliver('-1')
            cmo.setTimeToLive(-1)
            print "createDLQ() END"
        
        def createDestinationKey(dkname):
                print "createDestinationKey() START"     
            cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName)
            dk1 = create(dkname, "DestinationKey")
     
                    dk1.setProperty("JMSPriority")
            dk1.setKeyType("String")
            dk1.setSortOrder("Descending")
                print "createDestinationKey() END"
     
        def createQueue(qname, qjndiname, dlqName,dkName):
            print "createQueue() START"
            cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName)
            q1 = create(qname, "Queue")
            q1.JNDIName = qjndiname
     
            q1.subDeploymentName = subDeploymentName
     
            cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName)                        
            cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName+'/Queues/'+qname)
            set('DestinationKeys',jarray.array([String(dkName)],String)) 
     
            cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName+
                                   '/Queues/'+qname+'/DeliveryParamsOverrides/'+qname)
            cmo.setRedeliveryDelay(2000)
     
            print "Assign failed destination to queue"
     
            cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName+
                                    '/Queues/'+qname+'/DeliveryFailureParams/'+qname)
            cmo.setRedeliveryLimit(3)
     
            cmo.setExpirationPolicy('Redirect')
            erQueue = getMBean('/JMSSystemResources/'+moduleName+
                               '/JMSResource/'+moduleName+'/Queues/'+dlqName)
            cmo.setErrorDestination(erQueue) 
     
            cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName+
                                    '/Queues/'+qname+'/MessageLoggingParams/'+qname)
            cmo.setMessageLoggingEnabled(true)
            cmo.setMessageLoggingFormat('JMSDestination,JMSMessageID') 
     
            cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName+
                                    '/Queues/'+qname+'/DeliveryParamsOverrides/'+qname)
            cmo.setPriority(-1)
            cmo.setDeliveryMode('Persistent')
            cmo.setTimeToDeliver('-1')
            cmo.setTimeToLive(-1)
            print "createQueue() END" 
     
        def createCF(cfname, cfjndiname, xaEnabled):
            print "createCF() START"
            cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName)
            cf = create(cfname, "ConnectionFactory")
            cf.JNDIName = cfjndiname
            cf.subDeploymentName = subDeploymentName
     
            if (xaEnabled == "true"):
                cf.transactionParams.setXAConnectionFactoryEnabled(1)
            print "createCF() END" 
     
        def createJMSModule():
            print "createJMSModule() START"
            cd('/JMSSystemResources')
            jmsModule = create(moduleName, "JMSSystemResource")
            jmsModule.targets = (adminServer,)    
        
            cd(jmsModule.name)
     
            sd = create(subDeploymentName, "SubDeployment")    
            myJMSServer = getMBean('/JMSServers/' + jmsServerName)
            sd.targets = (myJMSServer,)
            print "createJMSModule() END"        
        
        def createJMSServer():
            print "createJMSServer() START"
            startTransaction()
     
            cd("/JMSServers")
            deleteIgnoringExceptions(jmsServerName)
            cd("/FileStores")
            deleteIgnoringExceptions(fileStoreName) 
     
            cd('/')
            filestore = cmo.createFileStore(fileStoreName)
            filestore.setDirectory(properties['fileStoreDirecory'])
            filestore.targets = (adminServer,)
            endTransaction()
            
            startTransaction()
            cd("/JMSServers")
     
            jmsServer = create(jmsServerName, "JMSServer")
            jmsServer.setPersistentStore(filestore)
            jmsServer.targets = (adminServer,)
            endTransaction()
            print "createJMSServer() END"    
        
        moduleName = 'TestJMSSModule'
        subDeploymentName = 'TestJMSSubDeployment'
        fileStoreName = 'TestFileStore'
        jmsServerName = 'TestJMSServer' 
     
        startTransaction()
     
        cd("/JMSSystemResources")
        deleteIgnoringExceptions(moduleName)
        endTransaction()
     
        print "Create JMS Server"
        createJMSServer()
        
        startTransaction()
        print "Create JMS Module"
        createJMSModule()
     
        dkName1='DefaultDestinationKey'
        print "Create Destinantion Key DefaultDestinationKey"
        createDestinationKey(dkName1) 
     
        queueEDQName1 = 'ApplicationBatchErrorQueue'
        queueEDQJNDIName1 = 'jms/ApplicationBatchErrorQueue'
        print "Create Error Destinantion ApplicationBatchErrorQueue"
        createDLQ(queueEDQName1, queueEDQJNDIName1,dkName1)
        
        queueName1 = 'ApplicationBatchReplyQueue'
        queueJNDIName1 = 'jms/ApplicationBatchReplyQueue'
        print "Create Queue ApplicationBatchReplyQueue"
        createQueue(queueName1, queueJNDIName1, queueEDQName1,dkName1)
     
        queueName2 = 'ApplicationBatchRequestQueue'
        queueJNDIName2 = 'jms/ApplicationBatchRequestQueue'
        print "Create Queue ApplicationBatchRequestQueue"
        createQueue(queueName2, queueJNDIName2, queueEDQName1,dkName1)    
     
        queueEDQName3 = 'XMLMarshallerErrorQueue'
        queueEDQJNDIName3 = 'jms/XMLMarshallerErrorQueue'
        print "Create Error Destinantion XMLMarshallerErrorQueue"
        createDLQ(queueEDQName3, queueEDQJNDIName3,dkName1)
        
        queueName5 = 'XMLMarshallerReplyQueue'
        queueJNDIName5 = 'jms/XMLMarshallerReplyQueue'
        print "Create Queue XMLMarshallerReplyQueue"
        createQueue(queueName5, queueJNDIName5, queueEDQName3,dkName1)
     
        queueName6 = 'XMLMarshallerRequestQueue'
        queueJNDIName6 = 'jms/XMLMarshallerRequestQueue'
        print "Create Queue XMLMarshallerRequestQueue"
        createQueue(queueName6, queueJNDIName6, queueEDQName3,dkName1)
     
        myCFName = 'TestConnectionFactory'
        myCFJNDIName = 'jms/TestConnectionFactory'
        print "Create JMS XA Connection Factory TestConnectionFactory"
        createCF(myCFName, myCFJNDIName, "true")    
        
        endTransaction()
    except:
        print "ocurrio un error"
        dumpStack()
        cancelEdit("y") 
     
    print "disconnect"
    disconnect()
     
    print "exit"
    
    exit()

    Properties File:

    Python
    username=weblogic
    password=weblogic1
    providerURL=t3://localhost:7101
    fileStoreDirecory=/config/jms/TestFileStore 

    If you follow the script, you will get a declaration of an array on XML, that is because if you check Weblogic documentation, a Destination Key is an Array of String, so in Python, you will have to declare your DeclarationKeys as String[] please keep in mind that note.

    The segment of code we carry on the next ...

    Python
    def createDestinationKey(dkname):  
        print "createDestinationKey() START"    
        cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName)  
        dk1 = create(dkname, "DestinationKey")  
        dk1.setProperty("JMSPriority")  
        dk1.setKeyType("String")  
        dk1.setSortOrder("Descending")  
        print "createDestinationKey() END"   

    Here, we define a part of the XML that is deployed on the server, we pass the property, keytype and sortorder that should be published on Oracle Weblogic, this procedure can be executed as a transaction as python script.

    Python
      ...
    cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName)
      cd('/JMSSystemResources/'+moduleName+'/JMSResource/'+moduleName+'/Queues/'+qname)
      set('DestinationKeys',jarray.array([String(dkName)],String))
    ...
    
  5. The command we need to execute on a terminal is:
    [root@USER25 bin]# java weblogic.WLST createJmsServer.py   

    Or you could also use wlst.sh:

    ./wlst.sh  createJmsServer.py 
  6. Finally, we will see the trace of execution, and verifying with Weblogic Console all configuration created that has been a declaration into python file, this is so much more powerful. Of course, you can create a JNDI or any task that we do over console.

Points of Interest

This kind of Installation could be done on a different Application Server and of course, it does not matter what king of operating system you are using, for example, HP-UX, Linux (any distros that could handle application server installation), and Microsoft Windows (using .NET for example IronPython).

Using Script file instead of Application Console is more useful on Production State because it runs twice or even more faster.

History

You can use a script for a lot of things.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)