我正在尝试编写脚本来测试WebSphere Cell/Node/Cluster的所有数据源。尽管可以从管理控制台执行此操作,但对于某些受众而言,脚本更好。
因此,我从IBM https://www.ibm.com/support/knowledgecenter/en/SSAW57_8.5.5/com.ibm.websphere.nd.multiplatform.doc/ae/txml_testconnection.html中找到了以下文章,该文章看起来很有希望,因为它确实满足了我的需求。
基本脚本如下:
ds_ids = AdminConfig.list("DataSource").splitlines()
for ds_id in ds_ids:
AdminControl.testConnection(ds_id)
我遇到了一些未记录的行为。与上面的
testConnection
函数相反,该函数并不总是返回String,但是可能还会引发异常。所以我只使用try-catch块:
try:
AdminControl.testConnection(ds_id)
except: # it actually is a com.ibm.ws.scripting.ScriptingException
exc_type, exc_value, exc_traceback = sys.exc_info()
现在,当我打印
exc_value
时,将得到以下结果:com.ibm.ws.scripting.ScriptingException: com.ibm.websphere.management.exception.AdminException: javax.management.MBeanException: Exception thrown in RequiredModelMBean while trying to invoke operation testConnection
现在,无论出什么问题,此错误消息始终是相同的。我测试了身份验证错误,缺少WebSphere变量和缺少驱动程序类。
管理控制台会打印合理的消息,而脚本会继续打印相同的无意义的消息。
很奇怪的是,只要我没有捕获到异常并且脚本只是错误退出,就会显示一条描述性错误消息。
访问Java异常会导致
exc_value.getCause()
给出None
。我也看过DataSource MBean,但是由于它们仅在启动服务器时存在,因此我很快放弃了它们。
我希望有人知道如何在未捕获到异常时访问错误消息。
提前致谢
最佳答案
毕竟,对AdminControl的研究和测试似乎不过是某些常用MBean的便利外观。
因此,我尝试发布了测试连接服务(如此处的Java示例https://www.ibm.com/support/knowledgecenter/en/SSEQTP_8.5.5/com.ibm.websphere.base.doc/ae/cdat_testcon.html
)直接:
ds_id = AdminConfig.list("DataSource").splitlines()[0]
# other queries may be 'process=server1' or 'process=dmgr'
ds_cfg_helpers = __wat.AdminControl.queryNames("WebSphere:process=nodeagent,type=DataSourceCfgHelper,*").splitlines()
try:
# invoke MBean method directly
warning_cnt = __wat.AdminControl.invoke(ds_cfg_helpers[0], "testConnection", ds_id)
if warning_cnt == "0":
print = "success"
else:
print "%s warning(s)" % warning_cnt
except ScriptingException as exc:
# get to the root of all evil ignoring exception wrappers
exc_cause = exc
while exc_cause.getCause():
exc_cause = exc_cause.getCause()
print exc_cause
这按我希望的方式工作。缺点是,如果需要测试在所有范围(单元/节点/集群/服务器/应用程序)上定义的数据源,则代码将变得更加复杂。
我不需要这个,所以我省略了,但是我仍然希望这个例子对其他人也有用。
关于error-handling - WebSphere wsadmin testConnection错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56232681/