当需要统计应用的试试在线人数的时候,统计成功登录后创建的session的数量是最为准确的数据。SpringBoot通过SessionListener可以很方便的监听session的生命周期。在SpringBoot中监听session的步骤如下:
1、创建session监听器.
package com.sds.listenner;
import java.util.HashSet;
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
@WebListener
public class SessionListener implements HttpSessionListener, HttpSessionAttributeListener {
public Logger logger = new Logger();
@Override
public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
logger.info("--attributeAdded--");
@SuppressWarnings("unused")
HttpSession session = httpSessionBindingEvent.getSession();
logger.info("key----:" + httpSessionBindingEvent.getName());
logger.info("value---:" + httpSessionBindingEvent.getValue());
}
@Override
public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
logger.info("--attributeRemoved--");
}
@Override
public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
logger.info("--attributeReplaced--");
}
@Override
public void sessionCreated(HttpSessionEvent event) {
logger.info("---sessionCreated----");
HttpSession session = event.getSession();
ServletContext application = session.getServletContext();
// 在application范围由一个HashSet集保存所有的session
@SuppressWarnings("unchecked")
HashSet<HttpSession> sessions = (HashSet<HttpSession>) application.getAttribute("sessions");
if (sessions == null) {
sessions = new HashSet<HttpSession>();
application.setAttribute("sessions", sessions);
}
// 新创建的session均添加到HashSet集中
sessions.add(session);
// 可以在别处从application范围中取出sessions集合
// 然后使用sessions.size()获取当前活动的session数,即为“在线人数”
//添加新建的session到MySessionContext中;
MySessionContext.AddSession(event.getSession());
}
@Override
public void sessionDestroyed(HttpSessionEvent event) throws ClassCastException {
logger.info("---sessionDestroyed----");
HttpSession session = event.getSession();
logger.info("deletedSessionId: " + session.getId());
System.out.println(session.getCreationTime());
System.out.println(session.getLastAccessedTime());
ServletContext application = session.getServletContext();
HashSet<?> sessions = (HashSet<?>) application.getAttribute("sessions");
// 销毁的session均从HashSet集中移除
sessions.remove(session);
//添加新建的session到MySessionContext中;
MySessionContext.DelSession(session);
}
}
2、创建session处理工具类,里面需要一个静态的HashMap存储应用中登录后创建的有效的session.
package com.sds.listenner;
import java.util.HashMap;
import javax.servlet.http.HttpSession;
public class MySessionContext {
private static HashMap<String, HttpSession> mymap = new HashMap<String, HttpSession>();
public static synchronized void AddSession(HttpSession session) {
if (session != null) {
mymap.put(session.getId(), session);
}
}
public static synchronized void DelSession(HttpSession session) {
if (session != null) {
mymap.remove(session.getId());
}
}
public static synchronized HttpSession getSession(String session_id) {
if (session_id == null)
return null;
return mymap.get(session_id);
}
}
Logger.java
package com.sds.listenner;
public class Logger {
public void info(String info) {
System.out.println(info);
}
}
3、添加SessionListener到系统配置中:
package com.sds.cfg;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class SessionConfiguration extends WebMvcConfigurerAdapter{
//注册session监听器;
@Bean
public ServletListenerRegistrationBean<SessionListener> servletListenerRegistrationBean() {
ServletListenerRegistrationBean<SessionListener> slrBean = new ServletListenerRegistrationBean<SessionListener>();
slrBean.setListener(new SessionListener());
return slrBean;
}
}
原理:通过SessionListener监听session的创建和销毁,并在对应的session生命周期中从静态的HashMap【自定义的session容器】中添加或者删除对应的session,从而实现session的动态管理,提供准确的统计数据。
注意:在本例中提供了根据sessionId获取session的方法。