SpringBoot integrated SessionListener statistics the number of online users

Copyright: https://blog.csdn.net/xichengqc/article/details/87368213

Today, the difference between research and interceptor's filter, see springboot integrated listener, the way to write a small demo statistical functions to achieve a number of online users.
Code management tools: springboot + maven3

  1. pom file
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. Write listener
package com.xicheng.listener.listener;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author xichengxml
 * @date 2019/2/15 14:44
 */
public class MySessionListener implements HttpSessionListener {

    public static AtomicInteger userCount = new AtomicInteger(0);

    /**
     * 用户登录,创建session,用户数增加
     * @param event
     */
    @Override
    public void sessionCreated(HttpSessionEvent event) {
        userCount.getAndIncrement();
    }

    /**
     * 用户下线,销毁session,用户数减少
     * @param event
     */
    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        userCount.getAndDecrement();
    }
}
  1. Registration listener
package com.xicheng.listener.config;

import com.xicheng.listener.listener.MySessionListener;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author xichengxml
 * @date 2019/2/15 14:42
 */
@Configuration
public class SpringMvcConfig implements WebMvcConfigurer{

    @Bean
    public ServletListenerRegistrationBean getListener() {
        ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
        servletListenerRegistrationBean.setListener(new MySessionListener());
        return servletListenerRegistrationBean;
    }
}
  1. Write controller
package com.xicheng.listener.controller;

import com.xicheng.listener.listener.MySessionListener;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author xichengxml
 * @date 2019/2/15 14:51
 */
@Controller
public class LoginController {

    @RequestMapping("/login")
    @ResponseBody
    public String login(String userName, HttpServletRequest request) {
        HttpSession httpSession = request.getSession(true);
        httpSession.setAttribute(userName, userName);
        AtomicInteger userCount = MySessionListener.userCount;
        return userName + "上线成功!当前在线人数: " + userCount;
    }

    @RequestMapping("/logout")
    @ResponseBody
    public String logout(String userName, HttpServletRequest request) {
        HttpSession httpSession = request.getSession(true);
        httpSession.removeAttribute(userName);
        httpSession.invalidate();
        AtomicInteger userCount = MySessionListener.userCount;
        return userName + "下线成功!当前在线人数:" + userCount;
    }
}

Start the project, visit: HTTP: // localhost:? 8080 / userName = the Login test01
Here Insert Picture Description
open another browser: HTTP: // localhost: 8080 / userName = Test02 the Login?
Here Insert Picture Description
Refresh the first page
Here Insert Picture Description
of code github Address: HTTPS: // github.com/xichengxml/woodencottage/tree/master/190125-listener

Guess you like

Origin blog.csdn.net/xichengqc/article/details/87368213