Scripting best practices
Scripting enables you to extend IBM® Maximo® Manage business logic by using Python, JavaScript, or any other JSR223-compliant scripting language. All the script code gets compiled to Java™ bytecode and is cached as part of Maximo Manage runtime caches. When the script is started, it is the cached bytecode that is run by the Java virtual machine by using the JSR223 bridge. Since the script code runs in the same thread as other Maximo Manage business logic written in Java, poorly written script code can negatively impact the performance of the system. Follow the Maximo Manage performance guidelines because scripting is equivalent to Maximo Manage custom code.
Choosing the correct launch point and event
thisvalue is the current attribute init
value:if priority is not None:
thisvalue=2*priorityThe MBO framework starts this script only when this attribute is referred to by the code or the user interface.
if service.getMbo().getString("status")=="APPR":
evalresult=False
evalresult=TrueAvoid costly object init events if invoked from List tab
from psdi.common.context import UIContext
if UIContext.getCurrentContext() is not None and UIContext.isFromListTab()==False:
..costly initialization..Watch out for conflicting launch point event scripts
The scripting framework allows you to attach multiple scripts to the same launch point event. This poses a problem if the script code expects to run it in a certain order before or after certain other scripts in the same launch point event. Since the Maximo event topic is an unordered map, the events are triggered without a fixed order. This can potentially cause issues if the order dependency is not managed properly. You should evaluate the reason to attach multiple scripts for the same launch point event and evaluate whether it makes more sense to combine them into one script. The other option is to make sure there is no dependency between the scripts.
Avoid calling save in the middle of a transaction
mbo.getMXTransaction().add(<newly created a mboset>)Calling MboSet.count() many times
cnt = mboset.count()
if cnt<=1:
service.log(“skipping this as count is “+cnt)if mboset.count()<=1:
service.log(“skipping this as count is “+mboset.count())Closing the MboSet
try:
..some code..
finally:
mboset.cleanup()If you do not clear the MboSets, out-of-memory errors may result.
Avoid the Mozilla compatibility script for Nashorn
Moving from Rhino and Java 7 to Nashorn and Java 8 is recommended for performance reasons. Nashorn performs better in Java 8 than Rhino does. Using the Mozilla compatibility script with Nashorn can result in poor performance in Java 8.
Check whether logging is enabled before logging
service.log("count of mbos "+mboset.count())This code unfortunately results in mboset.count() being called even though script logging is disabled.
from psdi.util.logging import MXLoggerFactory
logger = MXLoggerFactory.getLogger("maximo.script");
debugEnabled = logger.isDebugEnabled()
if debugEnabled:
service.log("count of mbos "+mboset.count())service variable enables you to check whether
logging is enabled:if service.isLoggingEnabled():
service.log(“count of mbos “+mboset.count())Avoid accessing script cache
Accessing the script cache from the automation script code results in circular dependency and causes instability when the script cache is being loaded, that is, partial load. When you write a script, use library scripts for modular script development and thereby avoid accessing the script from the cache.