使用Java获取Tomcat中的活动会话列表

ram*_*tel 23 java tomcat

我正在开发一个Java项目,我希望在Tomcat中计算所有活动会话.基于此,我想看看有多少用户是活跃的并且实际使用该应用程序.

Jan*_*ing 19

您应该使用JMX(Java Managemnet eXtension)并查询以下内容

jmxObjectName:    Catalina:host=localhost,path=/,type=Manager
jmxAttributeName: activeSessions
Run Code Online (Sandbox Code Playgroud)

您可以使用jconsole访问此数据.要获得jmx运行,请参阅http://tomcat.apache.org/tomcat-6.0-doc/monitoring.html

使用JMX有很多优点,因为您也可以获得大量其他数据.你可以将它放在一个munin插件中,让munin监控它并绘制漂亮的图形来查看.

  • 任何人都可以发布一个代码片段,演示如何从JVM本身通过JMX检索activeSessions吗? (3认同)

Ami*_*far 15

没有任何方法可以直接从tomcat获取会话数.但是,您可以创建并注册会话侦听器,并在创建时更新计数.这是一个例子:

http://tomcat-configure.blogspot.com/2009/01/tomcat-session-listener-example.html

public class SessionCounter implements HttpSessionListener {

  private static int activeSessions = 0;

  public void sessionCreated(HttpSessionEvent se) {
    activeSessions++;
  }

  public void sessionDestroyed(HttpSessionEvent se) {
    if(activeSessions > 0)
      activeSessions--;
    }

  public static int getActiveSessions() {
     return activeSessions;
  }
}
Run Code Online (Sandbox Code Playgroud)


Mik*_*van 5

下面是 Java 7 风格的 JMX 代码片段(basZero 所要求的,并且确实完成了 Janning 所描述的工作):

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi");
try(JMXConnector jmxc = JMXConnectorFactory.connect(url)) {
  MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
  ObjectName mbeanName = new ObjectName("Catalina:type=Manager,context=/,host=localhost");
  Object value = mbsc.getAttribute(mbeanName, "activeSessions");
}
Run Code Online (Sandbox Code Playgroud)

当然,如果未部署在根上下文中,则需要将 ObjectName 中的根上下文 (/) 替换为您的应用程序上下文字符串。请参阅我对 Catalina JMX 问题的详细解释:Accessingbuilt-in MBeans in Tomcatprogramatically


Yas*_*hah 5

一个简单的教程,演示如何确定 Java Web 应用程序中的活动用户/会话。

package com.hubberspot.javaee.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class OnlineUsersCounter implements HttpSessionListener {

private static int numberOfUsersOnline;

 public OnlineUsersCounter() {
  numberOfUsersOnline = 0;
 }

 public static int getNumberOfUsersOnline() { 
  return numberOfUsersOnline;
 }

    public void sessionCreated(HttpSessionEvent event) {

     System.out.println("Session created by Id : " + event.getSession().getId());
     synchronized (this) {
   numberOfUsersOnline++;
  }

    }

    public void sessionDestroyed(HttpSessionEvent event) {

     System.out.println("Session destroyed by Id : " + event.getSession().getId());
     synchronized (this) {
   numberOfUsersOnline--;
  }

    }

}
Run Code Online (Sandbox Code Playgroud)

在三个不同的浏览器上运行以下 servlet 将提供如下输出:(参见下图)

package com.hubberspot.javaee;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hubberspot.javaee.listener.OnlineUsersCounter;

// @WebServlet annotation has a initParams field which takes
// in initialization parameters for a servlet.
// @WebInitParam annotation takes in a name and value for the
// initialization parameters for the current Servlet.

@WebServlet(name = "HelloWorldServlet" , urlPatterns = { "/HelloWorldServlet" }
, initParams = { @WebInitParam(name = "user" , value = "Jonty") })
public class HelloWorldServlet extends HttpServlet {

 protected void doGet(
   HttpServletRequest request, 
   HttpServletResponse response
   ) throws ServletException, IOException {

  response.setContentType("text/html");

  PrintWriter out = response.getWriter();

  // sessionCreated method gets executed
  HttpSession session = request.getSession();

  session.setMaxInactiveInterval(60);

  try {
   out.println("<html>");
   out.println("<body>");
   out.println("<h2>Number of Users Online : "
      + OnlineUsersCounter.getNumberOfUsersOnline() 
      + "</h2>");
   out.println("</body>");
   out.println("</html>");
  } finally {
   out.close();
  }

 }

}
Run Code Online (Sandbox Code Playgroud)

程序的输出:

  1. Eclipse 浏览器 ->

蚀

  1. 火狐浏览器 ->

火狐浏览器

  1. Internet Explorer 浏览器 ->

IE

  1. 控制台输出->

安慰

欲了解更多信息:http://www.hubberspot.com/2013/09/how-to-define-active-users-sessions.html