使用 JMX(Java 管理扩展)API 获取活动会话计数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1112381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting Active Session counts with JMX (Java Management Extensions) API
提问by codingbear
I'm trying to use JMX API to get active session counts for a web application.
我正在尝试使用 JMX API 来获取 Web 应用程序的活动会话计数。
- Is it possible to use JMX API to get this kind of information?
- If yes, how reliable would it be?
- Any example code on how to get this done?
- 是否可以使用 JMX API 来获取此类信息?
- 如果是,它的可靠性如何?
- 关于如何完成这项工作的任何示例代码?
I've been reading JMX tutorial and documentation, but they are giving me the overview of what the technology is. I just can't pinpoint to what I need, yet.
我一直在阅读 JMX 教程和文档,但它们向我提供了该技术的概述。我只是无法确定我需要什么。
采纳答案by digitalsanctum
You can accomplish this by using something like JConsole or JVisualVM once you configure your app server to expose a JMX port. You don't mention which app server you're using but for Tomcat, it's described here: http://tomcat.apache.org/tomcat-5.5-doc/monitoring.html. Once you connect with JConsole, Tomcat exposes an MBean which has session information but again it depends which container you use.
一旦您将应用程序服务器配置为公开 JMX 端口,您就可以通过使用 JConsole 或 JVisualVM 之类的工具来完成此操作。您没有提到您使用的是哪个应用程序服务器,但对于 Tomcat,它在此处进行了描述:http: //tomcat.apache.org/tomcat-5.5-doc/monitoring.html。一旦您与 JConsole 连接,Tomcat 就会公开一个包含会话信息的 MBean,但这同样取决于您使用的容器。
回答by John Doe
To track the sessions you could use an HttpSessionListener. If you want to expose the active sessions via JMX, you could register a mbean and call it from other applications(see JMX documentation).
要跟踪会话,您可以使用HttpSessionListener。如果您想通过 JMX 公开活动会话,您可以注册一个 mbean 并从其他应用程序调用它(请参阅 JMX 文档)。
回答by skaffman
JBoss already exposes the active session count via JMX, but only across the whole server, not per webapp. If you only have one webapp being used, then that should be OK for you.
JBoss 已经通过 JMX 公开活动会话计数,但仅限于整个服务器,而不是每个 web 应用程序。如果您只使用一个 web 应用程序,那么这对您来说应该没问题。
Go the JMX console on port 8080, and look for the entry called host=localhost,path=/,type=Manager
. Inside that you'll find a entry for active session count.
转到端口 8080 上的 JMX 控制台,并查找名为host=localhost,path=/,type=Manager
. 在里面你会找到一个活动会话计数的条目。
回答by skaffman
The answer given by skaffman is quite helpful, but I would amend that JBoss is able to give you the sessions per webapp by looking for:
skaffman 给出的答案非常有帮助,但我会修正 JBoss 能够通过查找为您提供每个 webapp 的会话:
host=localhost,path=/your_webapp_context,type=Manager
host=localhost,path=/your_webapp_context,type=Manager
(replace your_webapp_context with the context of the webapp you are interested in...)
(将 your_webapp_context 替换为您感兴趣的 webapp 的上下文...)
回答by Mateu
ObjectName name = new ObjectName("Catalina:type=Manager,path=/NAME_OF_APP,host=localhost");
ManagementFactory.getPlatformMBeanServer().getAttribute(name, "activeSessions");