SpringBoot基于WebListener实现实时在线人数统计

一、引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

二、application.properties文件设置

# 设置1分钟的有效时间
server.servlet.session.timeout=1m

三、计数服务

import org.springframework.stereotype.Service;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author qinxun
 * @date 2023-06-05
 * @Descripion: 计数服务
 */
@Service
public class CountService {

    static AtomicInteger atomicInteger = new AtomicInteger(0);

    /**
     * 新增在线人数
     */
    public void incr(int cnt) {
        atomicInteger.addAndGet(cnt);
    }

    /**
     * 获取当前在线的人数
     */
    public int getOnlineCnt() {
        return atomicInteger.get();
    }
}

四、session监听器

import com.example.quartzdemo.service.CountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * @author qinxun
 * @date 2023-06-05
 * @Descripion: session监听器
 */
@Component
@WebListener
public class LoginUserCountListener implements HttpSessionListener {

    @Autowired
    private CountService countService;

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("新增一个用户");
        HttpSessionListener.super.sessionCreated(se);
        countService.incr(1);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("销毁一个用户");
        HttpSessionListener.super.sessionDestroyed(se);
        countService.incr(-1);
    }
}

五、创建访问层

import com.example.quartzdemo.service.CountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author qinxun
 * @date 2023-06-05
 * @Descripion: 测试
 */
@RestController
public class WebController {

    @Autowired
    private CountService countService;

    /**
     * 登录
     */
    @RequestMapping("/login")
    public String login(String username, HttpServletRequest servletRequest) {
        HttpSession session = servletRequest.getSession();
        session.setAttribute("username", username);
        return "欢迎您:" + username + ",当前在线人数:" + countService.getOnlineCnt();
    }


    /**
     * 退出登陆
     */
    @RequestMapping("/logout")
    public String logount(HttpSession httpSession) {
        // 注销当前的session
        httpSession.invalidate();
        return "登出成功, 当前在线人数: " + countService.getOnlineCnt();
    }

}

六、测试

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
基于springboot mysql实现在线网盘管理系统是一款方便、安全的文件管理工具。用户可以通过浏览器访问系统,进行文件上传、下载、删除、分享等操作。系统通过springboot框架构建,利用mysql数据库进行数据存储,保障数据的安全性和稳定性。 该系统具有用户管理、文件管理、权限管理等功能。用户可以注册登录个人账户,使用系统进行文件管理。系统支持不同类型文件的上传和下载,用户可以通过系统方便地进行文件的备份和共享。同时,系统具有权限管理功能,可以对用户进行权限控制,确保文件的安全性。 系统通过springboot提供的各种插件和框架,实现了高效的文件操作和稳定的数据存储。同时,系统还利用mysql数据库进行数据管理,保证数据的稳定性和安全性。用户可以放心地使用系统进行文件管理,不用担心数据丢失或泄露的风险。 基于springboot mysql实现在线网盘管理系统还具有良好的扩展性和定制性。开发人员可以根据实际需求对系统进行二次开发和定制,满足不同用户的个性化需求。系统还支持多平台访问,用户可以通过PC端和移动端进行文件管理,实现了更加便捷的操作体验。 总之,基于springboot mysql实现在线网盘管理系统是一款功能强大、安全可靠的文件管理工具,为用户提供了便捷的文件管理服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qinxun2008081

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值