调度单例
import cn.hutool.extra.spring.SpringUtil;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class AsyncManager {
/**
* 操作延迟10毫秒
*/
private final int OPERATE_DELAY_TIME = 10;
/**
* 异步操作任务调度线程池
*/
private ScheduledExecutorService executor = SpringUtil.getBean(ScheduledExecutorService.class);
private AsyncManager(){}
private static AsyncManager me = new AsyncManager();
public static AsyncManager me() {return me;}
public void execute(TimerTask task) {
executor.schedule(task,OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
}
}
任务类
import cn.hutool.extra.spring.SpringUtil;
import com.flydiy.sample.auto.dao.OperatorLogMapper;
import com.flydiy.sample.auto.pojo.po.OperatorLogPO;
import java.util.TimerTask;
public class AsyncFactory {
/**
* 操作日志保存异步方法
* @return
*/
public static TimerTask recordOper(OperatorLogPO operatorLog) {
return new TimerTask() {
@Override
public void run() {
OperatorLogMapper mapper = SpringUtil.getBean(OperatorLogMapper.class);
mapper.insert(operatorLog);
}
};
}
}