Springboot 用session监听器统计在线用户数量

今天给大家分享这个吧。

利用Springboot中的session监听器去实现统计在线用户数量的需求(当然其实用shiro或者security是框架自己带有会话管理的,用起来更加方便)。

但是, 接下来这个是非常简单直接快速的实现这个需求,不废话了

上代码:

第一步  . 既然用监听器实现,那肯定得创建监听器了。 

创建SessionListener.class

我用的是最直接的注解方式,图方便。   

这边的关键是两点,①@WebListener  ②implements HttpSessionListener

其他的思路看代码就能看懂,而且也做了注释。

import javax.servlet.annotation.WebListener;

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


/**
 * @Author : JCccc
 * @CreateTime : 2018-11-15
 * @Description :
 * @Point: Keep a good mood
 **/

@WebListener
public class SessionListener implements HttpSessionListener{

    private int onlineCount = 0;//记录session的数量

    /**
     * session创建后执行
     */
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        onlineCount++;
        System.out.println("【HttpSessionListener监听器】 sessionCreated, onlineCount:" + onlineCount);
       //将最新的onlineCount值存起来
        se.getSession().getServletContext().setAttribute("onlineCount", onlineCount);

    }

    /**
     * session失效后执行
     */
    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        if (onlineCount > 0) {
            onlineCount--;
        }
        System.out.println("【HttpSessionListener监听器】 sessionDestroyed, onlineCount:" + onlineCount);
        //将最新的onlineCount值存起来
        se.getSession().getServletContext().setAttribute("onlineCount", onlineCount);
    }

}

第二步. 好了其实已经完成了。

 

接下来就是单纯的校验:

首先模拟一个系统的登录接口,

@GetMapping("/login")
 public String login(HttpSession session){
    //模拟一个用户调用了登录接口,进入系统
    return "用户登录";
}

然后在浏览器访问一下,假装登录:

这时候,你可以看到控制台输出了:

是的,已经统计到一个了。

然后你可以继续用浏览器访问,你会发现控制台不会继续输出,因为你的ip对应的这个session还没过期,这就避免了重复统计。

然后,你把你的这个/login丢给你身边的小伙伴测试下,你就会发现控制台又输出了,而且 onlineCount变成2了。

 

最后,再写个获取这个统计值 onlineCount吧:

 

@GetMapping("/getOnlineCount")
public String getOnlineCount(HttpServletRequest httpServletRequest){
    HttpSession  session = httpServletRequest.getSession();
    //将session监听器的统计在线人数给拿出来~
    Object onlineCount=session.getServletContext().getAttribute("onlineCount");
    //展示一下,看看
    return "onlineCount : "+onlineCount;
}

 

再贴一个图,session过期了就会这样~

 

 

好了,就到此吧,顺便一提,在监听器那边是可以每次统计之后,不止set进session里面,还可以存数据库。

 

 

 

  • 5
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
Spring Boot 默认使用的是嵌入式的Servlet容器,如Tomcat或Jetty。因此,您可以使用Servlet API中提供的session机制来实现Session统计页面连接数量。 具体实现步骤如下: 1. 在Spring Boot应用程序中,创建一个名为"SessionListener"的类来实现HttpSessionListener接口,并在其中编写sessionCreated()和sessionDestroyed()方法。 ```java import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class SessionListener implements HttpSessionListener { private static int activeSessions = 0; public static int getActiveSessions() { return activeSessions; } @Override public void sessionCreated(HttpSessionEvent se) { activeSessions++; } @Override public void sessionDestroyed(HttpSessionEvent se) { activeSessions--; } } ``` 2. 将SessionListener注册到Spring Boot应用程序中的Servlet容器中。 ```java import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public ServletListenerRegistrationBean<SessionListener> sessionListener() { ServletListenerRegistrationBean<SessionListener> listener = new ServletListenerRegistrationBean<>(); listener.setListener(new SessionListener()); return listener; } } ``` 3. 通过调用SessionListener.getActiveSessions()方法来获取当前活动的Session数量,从而实现Session统计页面连接数量。 ```java @GetMapping("/active-sessions") public String activeSessions() { int activeSessions = SessionListener.getActiveSessions(); return "当前活动Session数量:" + activeSessions; } ``` 当用户访问应用程序中的任何页面时,SessionListener.sessionCreated()方法将会被调用,从而增加活动Session的数量。当用户关闭浏览器或Session过期时,SessionListener.sessionDestroyed()方法将会被调用,从而减少活动Session的数量。通过SessionListener.getActiveSessions()方法,我们可以获取当前活动的Session数量并展示给用户,从而实现Session统计页面连接数量的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小目标青年

对你有帮助的话,谢谢你的打赏。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值