首页 > 编程语言 >java实现统计在线

java实现统计在线

时间:2022-11-25 14:13:17浏览次数:44  
标签:java 在线 ip request return container import public 统计

主要是实现HttpSessionBindingListener的valueBound方法和valueUnbound方法进行session的创建和销毁。
定义一个Vector容器。用于放置ip等信息。具体实现如下:


import com.larva.common.utils.IpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.util.Date;
import java.util.Hashtable;
import java.util.Vector;

/**
* @author Larva
* @Date 2022-11-24 14:28
* @describe: 监测ip在线数量
**/
@WebListener //这个注解的作用是启动监听
public class SessionBindingListener implements HttpSessionBindingListener{
  private static Logger log = LoggerFactory.getLogger(SessionBindingListener.class);

  public SessionBindingListener() {

  }
  private Vector<Hashtable> container = new Vector<Hashtable>();
  public int getCount() {
      container.trimToSize();// 调整Vector listenUser的容量为Vector listenUser的大小
      return container.capacity();// 返回listenUser的容量
  }
  public boolean existIp(String ip) {
      container.trimToSize();
      boolean existUser = false;
      for (int i = 0; i < container.capacity(); i++) {
          if (ip.equals(container.get(i).get("ip").toString())) {
              existUser = true;
              break;
          }
      }
      return existUser;
  }
  public boolean deleteIp(String ip) {
      container.trimToSize();
      if (existIp(ip)) {
          int currUserIndex = -1;
          for (int i = 0; i < container.capacity(); i++) {
              if (ip.equals(container.get(i).get("ip").toString())) {
                  currUserIndex = i;
                  break;
              }
          }
          if (currUserIndex != -1) {
              container.remove(currUserIndex);
              container.trimToSize();
              return true;
          }
      }
      return false;
  }
  public Vector<Hashtable> getOnLineUser() {
      return container;
  }
  public void valueBound(HttpSessionBindingEvent e) {
      container.trimToSize();
      log.info("ip:"+e.getName() + "    登入到系统... " + (new Date()));
      if (getCount()==0){
          log.info("当前在线人数为: " + 1+"人");
      }else {
          log.info("当前在线人数为: " +getCount()+"人");
      }
  }
  public void valueUnbound(HttpSessionBindingEvent e) {
      container.trimToSize();
      String ip = e.getName();
      deleteIp(ip);
      log.info(ip + "    退出系统 " + (new Date()));
      log.info("      当前在线用户数为:" + getCount()+"人");
  }
  //传入request将ip信息保存到容器内。
  public  boolean addIpSession(HttpServletRequest request){
      String ip= IpUtil.getIpAddr(request);
      Hashtable map = new Hashtable();
      map.put("ip",ip);
      //判断是否已经存在ip,已经存在则刷新serssion时间。
      if (existIp(ip)){
          request.getSession().setAttribute(ip,SessionBindingListener.this);
          /*默认过期时间30分钟*/
          request.getSession().setMaxInactiveInterval(3);
          return true;
      }
      request.getSession().setAttribute(ip,SessionBindingListener.this);
      /*默认过期时间30分钟*/
      request.getSession().setMaxInactiveInterval(30*60);
      Vector<Hashtable> vt = getOnLineUser();
      vt.add(map);
      return true;
  }
}

上面代码可以称之为一个配置类。用的时候直接调用就好了。下面是调用代码:

接口:


import javax.servlet.http.HttpServletRequest;

/**
 * @author Larva
 * @Date 2022-11-24 14:33
 * @describe:
 **/
public interface IService {
    boolean Login(HttpServletRequest request);
    int getOnlinePeople();
    boolean outLogin();
}```

实现层:
```package com.Larva.demo.service.impl;

import com.Larva.demo.service.IService;
import com.larva.common.config.SessionBindingListener;
import com.larva.common.utils.IpUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Larva
 * @Date 2022-11-24 14:33
 * @describe:
 **/
@Service
public class ServiceImpl implements IService {
    public static SessionBindingListener listener=new SessionBindingListener();
    @Override
    public boolean Login(HttpServletRequest request) {
        try{
        listener.addIpSession(request);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public int getOnlinePeople() {
        int count = listener.getCount();
        return count;
    }

    @Override
    public boolean outLogin() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //获取退出登录的ip
        String ip=IpUtil.getIpAddr(request);
        request.getSession().removeAttribute(ip);
        return true;
    }
}

控制层:


import com.Larva.demo.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Larva
 * @Date 2022-11-24 14:31
 * @describe:
 **/
@RestController
@RequestMapping("api")
public class Api {
    @Autowired
    ServiceImpl service;
    @RequestMapping("api1")
    public boolean Login(HttpServletRequest request){
        return this.service.Login(request);
    }

    @RequestMapping("api2")
    public int getPeople(){
        return this.service.getOnlinePeople();
    }

    @RequestMapping("api3")
    public boolean outLogin(){
        return this.service.outLogin();
    }
}

三个接口分别是登录(注册session),查询ip,退出(销毁session)

标签:java,在线,ip,request,return,container,import,public,统计
From: https://www.cnblogs.com/jingwei129/p/16924925.html

相关文章