统计在线人数
parent
260581b550
commit
3801ef6ee0
@ -0,0 +1,67 @@
|
||||
package com.ibeetl.jlw.job;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.ibeetl.jlw.entity.SysLog;
|
||||
import com.ibeetl.jlw.service.SysLogService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.quartz.Job;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.quartz.JobExecutionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Set;
|
||||
|
||||
import static cn.hutool.core.date.DatePattern.NORM_DATETIME_MINUTE_PATTERN;
|
||||
import static com.ibeetl.admin.core.service.CorePlatformService.TOKEN_KEY;
|
||||
|
||||
/**
|
||||
* 在线人数统计脚本
|
||||
*
|
||||
* 每过15分钟 执行一次任务
|
||||
* 0 0/15 * * * ?
|
||||
* 00:15 00:30 00:45
|
||||
* @author lx
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class OnLineRecordJob implements Job {
|
||||
|
||||
// 存到日志中方法名
|
||||
public final static String ON_LINE_METHOD_NAME = "com.ibeetl.jlw.job.OnLineRecordJob.execute";
|
||||
|
||||
@Autowired
|
||||
private SysLogService sysLogService;
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Override
|
||||
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
|
||||
try {
|
||||
|
||||
// 查询redis中现在还存在的session Key数量
|
||||
Set<String> keys = CollUtil.emptyIfNull(stringRedisTemplate.keys(TOKEN_KEY + ":*"));
|
||||
|
||||
SysLog model = new SysLog();
|
||||
// 格式化到分钟
|
||||
Date nowDate = DateUtil.parse(DateUtil.now(), NORM_DATETIME_MINUTE_PATTERN);
|
||||
|
||||
// 这里固定该接口的请求路径
|
||||
model.setMethod(ON_LINE_METHOD_NAME);
|
||||
model.setRequestUrl("在线人数统计");
|
||||
model.setParams("");
|
||||
model.setResult(keys.size() + "");
|
||||
model.setIp("127.0.0.1");
|
||||
model.setCreateTime(nowDate);
|
||||
model.setResultTime(nowDate);
|
||||
sysLogService.insert(model);
|
||||
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
log.error(e.toString());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue