开发定期删除线程池历史运行数据. (#33)

pull/39/head
chen.ma 3 years ago
parent af4904bb46
commit 17f457c944

@ -70,4 +70,6 @@ public class Constants {
public static final int AVAILABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();
public static final String DEFAULT_GROUP = "default group";
}

@ -0,0 +1,57 @@
package cn.hippo4j.config.monitor;
import cn.hippo4j.common.executor.ExecutorFactory;
import cn.hippo4j.config.model.HisRunDataInfo;
import cn.hippo4j.config.service.biz.HisRunDataService;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import static cn.hippo4j.common.constant.Constants.DEFAULT_GROUP;
/**
* Regularly clean up the historical running data of thread pool.
*
* @author chen.ma
* @date 2021/12/17 20:13
*/
@Component
@RequiredArgsConstructor
public class TimeCleanHistoryDataTask implements Runnable, InitializingBean {
@Value("${clean.history.data.period:30}")
private Long cleanHistoryDataPeriod;
@NonNull
private final HisRunDataService hisRunDataService;
private final ScheduledExecutorService cleanHistoryDataExecutor = ExecutorFactory.Managed
.newSingleScheduledExecutorService(DEFAULT_GROUP, r -> new Thread(r, "clean-history-data"));
@Override
public void run() {
Date currentDate = new Date();
DateTime offsetMinuteDateTime = DateUtil.offsetMinute(currentDate, (int) -cleanHistoryDataPeriod);
LambdaQueryWrapper<HisRunDataInfo> queryWrapper = Wrappers.lambdaQuery(HisRunDataInfo.class)
.le(HisRunDataInfo::getTimestamp, offsetMinuteDateTime.getTime());
hisRunDataService.remove(queryWrapper);
}
@Override
public void afterPropertiesSet() throws Exception {
cleanHistoryDataExecutor.scheduleWithFixedDelay(this, 0, 1, TimeUnit.MINUTES);
}
}

@ -14,7 +14,7 @@ import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Date;
@ -29,13 +29,15 @@ import static cn.hutool.core.date.DatePattern.NORM_TIME_PATTERN;
* @date 2021/12/10 21:28
*/
@Service
@AllArgsConstructor
public class HisRunDataServiceImpl extends ServiceImpl<HisRunDataMapper, HisRunDataInfo> implements HisRunDataService {
@Value("${clean.history.data.period:30}")
private Long cleanHistoryDataPeriod;
@Override
public List<MonitorRespDTO> query(MonitorQueryReqDTO reqDTO) {
Date currentDate = new Date();
DateTime dateTime = DateUtil.offsetMinute(currentDate, -30);
DateTime dateTime = DateUtil.offsetMinute(currentDate, (int) -cleanHistoryDataPeriod);
long startTime = dateTime.getTime();
List<HisRunDataInfo> hisRunDataInfos = this.lambdaQuery()
@ -53,7 +55,7 @@ public class HisRunDataServiceImpl extends ServiceImpl<HisRunDataMapper, HisRunD
@Override
public MonitorActiveRespDTO queryInfoThreadPoolMonitor(MonitorQueryReqDTO reqDTO) {
Date currentDate = new Date();
DateTime dateTime = DateUtil.offsetMinute(currentDate, -60);
DateTime dateTime = DateUtil.offsetMinute(currentDate, (int) -cleanHistoryDataPeriod);
long startTime = dateTime.getTime();
List<HisRunDataInfo> hisRunDataInfos = this.lambdaQuery()

@ -6,6 +6,8 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import static cn.hippo4j.common.constant.Constants.DEFAULT_GROUP;
/**
* Config executor.
*
@ -15,7 +17,7 @@ import java.util.concurrent.TimeUnit;
public class ConfigExecutor {
private static final ScheduledExecutorService LONG_POLLING_EXECUTOR = ExecutorFactory.Managed
.newSingleScheduledExecutorService("default group", r -> new Thread(r, "long-polling"));
.newSingleScheduledExecutorService(DEFAULT_GROUP, r -> new Thread(r, "long-polling"));
public static void executeLongPolling(Runnable runnable) {
LONG_POLLING_EXECUTOR.execute(runnable);

@ -16,13 +16,18 @@ server.tomcat.basedir=
### Custom Logging Tenant
tenant=hippo4j
### Regularly clean up the historical running data of thread pool. unit: minute.
clean.history.data.period=30
#*************** Config Module Related Configurations ***************#
### Hikari Datasource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
### Data source customization section
spring.datasource.url=jdbc:mysql://localhost:3306/hippo4j_manager?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
### Hikari Datasource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.hikari.pool-name=Hikari
spring.datasource.hikari.connectionTimeout=30000
spring.datasource.hikari.idleTimeout=30000

@ -1,19 +1,33 @@
#*************** Spring Boot Related Configurations ***************#
### Default Web Context Path
# server.servlet.contextPath=/hippo4j
### Server Startup Port
server.port=6691
### Server Tomcat
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i %{Request-Source}i
### Default Current Work Dir
server.tomcat.basedir=
#*************** Dynamic Thread Pool Custom Configuration ***************#
### Custom Logging Tenant
tenant=hippo4j
### Regularly clean up the historical running data of thread pool. unit: minute.
clean.history.data.period=30
#*************** Config Module Related Configurations ***************#
### Hikari Datasource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
### Data source customization section
spring.datasource.url=jdbc:mysql://localhost:3306/hippo4j_manager?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
### Hikari Datasource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.hikari.pool-name=Hikari
spring.datasource.hikari.connectionTimeout=30000
spring.datasource.hikari.idleTimeout=30000

Loading…
Cancel
Save