package com.hellospringboot.entity;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
public class Counter {
public Counter() {
//开启线程定时处理过期访问记录
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
while(true){
//休眠500毫秒降低资源消耗
try {
Thread.sleep(500L);
} catch (InterruptedException e) {}
//对遍历map集合中过期访问记录进行清理
for (Map.Entry<String, LinkedList<Date>> entry : map.entrySet()) {
for (int i = entry.getValue().size()-1; i >=0; i--) {
//判断是否过期,对过期的数据进行处理
if ((System.currentTimeMillis()-entry.getValue().getLast().getTime())>60*1000) {
entry.getValue().removeLast();
}
}
}
}
}
}).start();
}
//请求路径
private HttpServletRequest request;
// 分类存储API访问路径及其访问时间
private Map<String, LinkedList<Date>> map = new HashMap<>();
// 验证位,校验是否开启访问记数
private boolean flag = false;
/**
* API访问累加,进行线程同步处理
*/
public synchronized void add() {
if (flag) {
//LinkedList实现增删快
LinkedList<Date> list = map.get(request.getRequestURL().toString());
//初始化linkdList
if (list == null) {
list = new LinkedList<>();
}
//将访问时间存入list集合
list.addFirst(new Date());
//将访问时间的集合放入map
map.put(request.getRequestURL().toString(), list);
}
}
/**
* API访问累加开启
*/
public void start() {
flag = true;
}
/**
* API访问累加停止
*/
public void stop() {
flag = false;
}
/**
* API访问重置
*/
public void reset() {
stop();
map.clear();
}
/*
* getter 与 setter
*/
public Map<String, Integer> getMap() {
HashMap<String, Integer> hashMap = new HashMap<>();
for (Map.Entry<String, LinkedList<Date>> entry : map.entrySet()) {
hashMap.put(entry.getKey()+" 的总访问此数为:", entry.getValue().size());
}
return hashMap;
}
public void setMap(Map<String, LinkedList<Date>> map) {
this.map = map;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
springboot记录api的访问次数
共59个文件
xml:37个
class:6个
java:5个
需积分: 40 92 下载量 183 浏览量
2018-03-12
19:18:08
上传
评论 2
收藏 43KB ZIP 举报
温馨提示
springboot入门工程,可是开始计数,终止计数,清空计数.只保存最近一分钟以内的访问次数
资源推荐
资源详情
资源评论
收起资源包目录
springboot-test.zip (59个子文件)
springboot-test
src
main
resources
application.properties 35B
java
com
hellospringboot
entity
Counter.java 3KB
controller
VisiterCounterController2.java 1KB
AccessController.java 768B
config
BeanConfig.java 474B
Application.java 370B
test
resources
java
.classpath 658B
.settings
org.eclipse.wst.common.project.facet.core.xml 145B
org.eclipse.m2e.core.prefs 90B
org.eclipse.core.resources.prefs 191B
org.eclipse.jdt.core.prefs 664B
pom.xml 988B
target
classes
com
hellospringboot
entity
Counter.class 4KB
Counter$1.class 2KB
controller
VisiterCounterController2.class 2KB
AccessController.class 1KB
Application.class 698B
config
BeanConfig.class 730B
application.properties 35B
generated-sources
annotations
test-classes
maven-status
maven-compiler-plugin
compile
default-compile
inputFiles.lst 540B
createdFiles.lst 283B
testCompile
default-testCompile
inputFiles.lst 0B
.idea
misc.xml 446B
encodings.xml 172B
libraries
Maven__ch_qos_logback_logback_classic_1_1_9.xml 568B
Maven__org_springframework_boot_spring_boot_starter_web_1_4_4_RELEASE.xml 720B
Maven__org_hibernate_hibernate_validator_5_2_4_Final.xml 634B
Maven__org_apache_tomcat_embed_tomcat_embed_websocket_8_5_11.xml 660B
Maven__org_springframework_spring_expression_4_3_6_RELEASE.xml 658B
Maven__ch_qos_logback_logback_core_1_1_9.xml 547B
Maven__com_fasterxml_classmate_1_3_3.xml 522B
Maven__com_fasterxml_jackson_core_jackson_annotations_2_8_6.xml 644B
Maven__org_springframework_spring_beans_4_3_6_RELEASE.xml 623B
Maven__org_springframework_spring_context_4_3_6_RELEASE.xml 637B
Maven__org_slf4j_jul_to_slf4j_1_7_22.xml 534B
Maven__org_springframework_boot_spring_boot_autoconfigure_1_4_4_RELEASE.xml 734B
Maven__org_apache_tomcat_embed_tomcat_embed_core_8_5_11.xml 625B
Maven__org_springframework_boot_spring_boot_starter_tomcat_1_4_4_RELEASE.xml 741B
Maven__org_springframework_boot_spring_boot_starter_logging_1_4_4_RELEASE.xml 748B
Maven__org_springframework_spring_aop_4_3_6_RELEASE.xml 609B
Maven__org_slf4j_jcl_over_slf4j_1_7_22.xml 548B
Maven__org_jboss_logging_jboss_logging_3_3_0_Final.xml 608B
Maven__org_springframework_boot_spring_boot_starter_1_4_4_RELEASE.xml 692B
Maven__org_yaml_snakeyaml_1_17.xml 495B
Maven__com_fasterxml_jackson_core_jackson_databind_2_8_6.xml 623B
Maven__com_fasterxml_jackson_core_jackson_core_2_8_6.xml 595B
Maven__org_springframework_spring_web_4_3_6_RELEASE.xml 609B
Maven__org_springframework_spring_webmvc_4_3_6_RELEASE.xml 630B
Maven__org_springframework_spring_core_4_3_6_RELEASE.xml 616B
Maven__javax_validation_validation_api_1_1_0_Final.xml 611B
Maven__org_apache_tomcat_embed_tomcat_embed_el_8_5_11.xml 611B
Maven__org_springframework_boot_spring_boot_1_4_4_RELEASE.xml 636B
Maven__org_slf4j_slf4j_api_1_7_22.xml 513B
Maven__org_slf4j_log4j_over_slf4j_1_7_22.xml 562B
workspace.xml 31KB
inspectionProfiles
compiler.xml 642B
modules.xml 270B
.project 981B
springboot-test.iml 5KB
共 59 条
- 1
资源评论
qq_38227226
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功