使用Plotly Dash for Python进行JBeans(JMX)监视

问题描述 投票:1回答:1

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

  • Python 3.6
  • 短跑1.0.2

我连接到Jconsole。引用Jbean属性和提取值的工作原理。它甚至可以在我第一次启动dash应用程序时运行,但是当Interval被触发时,dash程序关闭。

  • 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 MODULE
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:\Users\m011472\Documents\Python Scripts\Web>cd "c:\Users\m011472\Documents\Python Scripts\Web" && cmd /C "set "PYTHONIOENCODING=UTF-8" && set "PYTHONUNBUFFERED=1" && C:\Python36\python.exe C:\Users\m011472\.vscode\extensions\ms-python.python-2018.4.0\pythonFiles\PythonTools\visualstudio_py_launcher.py "c:\Users\m011472\Documents\Python Scripts\Web" 53564 34806ad9-833a-4524-8cd6-18ca4aa74f14 RedirectOutput,RedirectOutput "c:\Users\m011472\Documents\Python Scripts\Web\TEST_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:\Users\m011472\Documents\Python Scripts\Web>

---仪表板输出:

工作,试用:0; 418

b)预期结果

---命令行输出:

程序应该每10秒执行一次POST,永远不会结束

---仪表板输出:

数字应每10秒更新一次

工作,试用:0; XXX(任意号码)工作,试用:1; XXY WORKING,试用:2; XXZ

python jmx plotly-dash jconsole
1个回答
0
投票

如果有人感兴趣 - 我找到了部分解决方案。由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在每个时间间隔创建新线程?

© www.soinside.com 2019 - 2024. All rights reserved.