JBeans (JMX) monitoring with Plotly Dash for Python



我正在尝试使用dash来实时监控JBeans属性。使用 dcc.Interval 程序在第一次开机自检/_dash-update-component 尝试后自动关闭。

  • 蟒蛇 3.6
  • 破折号 1.0.2

我连接到了Jconsole。引用 Jbean 属性并提取值有效。当我第一次启动破折号应用程序时,它甚至可以工作,但是当触发间隔时,破折号程序关闭。

  • JBean 测试 - 工作正常
  • 经过测试的达世币代码 - 它可以很好地提取n_intervals

步骤:

  1. JMX 功能模块
import jpype as jpype
from jpype import java
from jpype import javax
import sys, os
import pandas as pd
import datetime
#JMX CONNECTION FUNCTION:
def jmx_connect(HOST, USER, PORT, PASS):
URL = 'service:jmx:rmi:///jndi/rmi://'+HOST+':'+PORT+'/jmxrmi'
jpype.startJVM(jpype.get_default_jvm_path())
jhash = java.util.HashMap()
jarray=jpype.JArray(java.lang.String)([USER,PASS])
jhash.put(javax.management.remote.JMXConnector.CREDENTIALS, jarray);
jmxurl = javax.management.remote.JMXServiceURL(URL)
jmxsoc = javax.management.remote.JMXConnectorFactory.connect(jmxurl,jhash)
connection = jmxsoc.getMBeanServerConnection();
return connection
#Enter login details HERE:
HOST = 
USER = 
PORT = 
PASS = 
#initializing connection with my login details
connection = jmx_connect(HOST, USER, PORT, PASS)

#Thread Count function to extract number of active threads:
#THREAD COUNT
def jmx_ThreadCount():
object="java.lang:type=Threading"
attribute= "ThreadCount"
attr = connection.getAttribute(javax.management.ObjectName(object),attribute)
return attr
  1. DASH 模块
from TEST_jvmRun import *  #this is import of my JVM functions
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objs as go
app = dash.Dash()
app.layout = html.Div([
html.H1(id='live-update-text'),
dcc.Interval(id='interval-component', interval=10000, n_intervals=0)
])
@app.callback(Output('live-update-text', 'children'),
[Input('interval-component', 'n_intervals')])
def update_current_delay(n): 
#return f"counter: {n}"
return f"WORKING, trial: {n}  ; {jmx_ThreadCount()}" 
if __name__ == '__main__':
app.run_server()

a) 实际结果

---命令行输出:

C:Usersm011472DocumentsPython ScriptsWeb>cd "c:Usersm011472DocumentsPython ScriptsWeb" && cmd /C "set "PYTHONIOENCODING=UTF-8" && set "PYTHONUNBUFFERED=1" && C:Python36python.exe C:Usersm011472.vscodeextensionsms-python.python-2018.4.0pythonFilesPythonToolsvisualstudio_py_launcher.py "c:Usersm011472DocumentsPython ScriptsWeb" 53564 34806ad9-833a-4524-8cd6-18ca4aa74f14 RedirectOutput,RedirectOutput "c:Usersm011472DocumentsPython ScriptsWebTEST_dashboard.py" "
* Serving Flask app "TEST_dashboard" (lazy loading) * Environment: production   WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:8050/ (Press CTRL+C to quit)
127.0.0.1 - - [28/Mar/2019 14:57:02] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [28/Mar/2019 14:57:03] "GET /_dash-layout HTTP/1.1" 200 -
127.0.0.1 - - [28/Mar/2019 14:57:03] "GET /_dash-dependencies HTTP/1.1" 200 -
127.0.0.1 - - [28/Mar/2019 14:57:03] "POST /_dash-update-component HTTP/1.1" 200 -
C:Usersm011472DocumentsPython ScriptsWeb>

---仪表板输出:

工作, 试用: 0 ; 418

b) 预期成果

---命令行输出:

程序应每 10 秒执行一次开机自检,永不结束

---仪表板输出:

数字应每 10 秒更新一次

工作, 试用: 0 ;XXX(任意数字) 工作, 试用: 1 ;二十年 工作, 试用: 2 ;三十

如果有人感兴趣 - 我找到了部分解决方案。由 dcc.Interval 引起的问题,因为它在每个时间间隔创建新线程,并且 JMX 调用要求将 JVM 实例附加到当前线程。

  1. 添加了 jpype.attachThreadToJVM()
def jmx_ThreadCount():
print("State=", jpype.isThreadAttachedToJVM())
if not jpype.isThreadAttachedToJVM():
print ("Needs to attach...")
jpype.attachThreadToJVM()
print ("Check Attached=", jpype.isThreadAttachedToJVM())  
object="java.lang:type=Threading"
attribute= "ThreadCount"
attr = connection.getAttribute(javax.management.ObjectName(object),attribute)
return attr

现在的问题是:如何防止 dcc.Interval 在每个时间间隔创建新线程?

相关内容

  • 没有找到相关文章

最新更新