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