first commit
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package com.starry.admin.manager;
|
||||
|
||||
|
||||
import com.starry.common.utils.SpringUtils;
|
||||
import com.starry.common.utils.ThreadsUtils;
|
||||
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author admin
|
||||
* 异步任务管理器
|
||||
* @since 2022/7/25
|
||||
*/
|
||||
public class AsyncManager {
|
||||
|
||||
private static final AsyncManager me = new AsyncManager();
|
||||
/**
|
||||
* 操作延迟10毫秒
|
||||
*/
|
||||
private final int OPERATE_DELAY_TIME = 10;
|
||||
/**
|
||||
* 异步操作任务调度线程池
|
||||
*/
|
||||
private final ScheduledExecutorService executor = SpringUtils.getBean("scheduledExecutorService");
|
||||
|
||||
/**
|
||||
* 单例模式
|
||||
*/
|
||||
private AsyncManager() {
|
||||
}
|
||||
|
||||
public static AsyncManager me() {
|
||||
return me;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行任务
|
||||
*
|
||||
* @param task 任务
|
||||
*/
|
||||
public void execute(TimerTask task) {
|
||||
executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止任务线程池
|
||||
*/
|
||||
public void shutdown() {
|
||||
ThreadsUtils.shutdownAndAwaitTermination(executor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.starry.admin.manager;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
/**
|
||||
* 确保应用退出时能关闭后台线程
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Component
|
||||
public class ShutdownManager {
|
||||
private static final Logger logger = LoggerFactory.getLogger("sys-user");
|
||||
|
||||
@PreDestroy
|
||||
public void destroy() {
|
||||
shutdownAsyncManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止异步执行任务
|
||||
*/
|
||||
private void shutdownAsyncManager() {
|
||||
try {
|
||||
logger.info("====关闭后台任务任务线程池====");
|
||||
AsyncManager.me().shutdown();
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.starry.admin.manager.factory;
|
||||
|
||||
import cn.hutool.extra.servlet.ServletUtil;
|
||||
import cn.hutool.http.useragent.UserAgent;
|
||||
import cn.hutool.http.useragent.UserAgentUtil;
|
||||
import com.starry.admin.modules.system.entity.SysLoginLogEntity;
|
||||
import com.starry.admin.modules.system.service.ISysLoginLogService;
|
||||
import com.starry.common.constant.Constants;
|
||||
import com.starry.common.utils.ServletUtils;
|
||||
import com.starry.common.utils.SpringUtils;
|
||||
import com.starry.common.utils.ip.AddressUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.TimerTask;
|
||||
|
||||
/**
|
||||
* @author admin
|
||||
*/
|
||||
@Slf4j
|
||||
public class AsyncFactory {
|
||||
|
||||
/**
|
||||
* 记录登录信息
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param status 状态
|
||||
* @param message 消息
|
||||
* @param args 列表
|
||||
* @return 任务task
|
||||
*/
|
||||
public static TimerTask recordLoginLog(final String username, final String status, final String message, final Object... args) {
|
||||
// 客户端操作系统、浏览器等信息
|
||||
final UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent"));
|
||||
// 请求的IP地址
|
||||
final String ip = ServletUtil.getClientIP(ServletUtils.getRequest());
|
||||
return new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
String address = AddressUtils.getRealAddressByIp(ip);
|
||||
// 获取客户端操作系统
|
||||
String os = userAgent.getOs().getName();
|
||||
// 获取客户端浏览器
|
||||
String browser = userAgent.getBrowser().getName();
|
||||
// 封装对象
|
||||
SysLoginLogEntity loginLog = new SysLoginLogEntity();
|
||||
loginLog.setUserCode(username);
|
||||
loginLog.setIpaddr(ip);
|
||||
loginLog.setLoginLocation(address);
|
||||
loginLog.setBrowser(browser);
|
||||
loginLog.setOs(os);
|
||||
loginLog.setMsg(message);
|
||||
loginLog.setLoginTime(new Date());
|
||||
// 日志状态
|
||||
if (Constants.LOGIN_FAIL.equals(status)) {
|
||||
loginLog.setStatus(Integer.valueOf(Constants.FAIL));
|
||||
} else {
|
||||
loginLog.setStatus(Integer.valueOf(Constants.SUCCESS));
|
||||
}
|
||||
// 插入数据
|
||||
SpringUtils.getBean(ISysLoginLogService.class).create(loginLog);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user