核心代码展示:
@Component
public class AsyncExecutorPoolConfig {
private static final Logger LOG = LoggerFactory.getLogger(AsyncExecutorPoolConfig.class);
@Value("${peroninfo.async.log.queueSize:30000}")
private int queueSize;
@Value("${peroninfo.async.log.queueSize:200}")
private int batchSize;
@Value("${peroninfo.async.log.queueSize:10}")
private int threadPoolSize;
/**
* 批量执行标志
*/
private volatile boolean runBatch = false;
private ExecutorService threadPool;
private ArrayBlockingQueue<Count> blockingQueue;
private final CountManagementService countManagementService;
@Autowired
public AsyncExecutorPoolConfig(CountManagementService countManagementService) {
this.countManagementService = countManagementService;
}
@PostConstruct
public void init() {
blockingQueue = new ArrayBlockingQueue<>(queueSize);
threadPool = Executors.newFixedThreadPool(threadPoolSize);
}
@PreDestroy
public void destroy() {
// 批量保存日志
batchSave();
// 关闭线程池
if (threadPool != null) {
threadPool.shutdown();
}
}
/**
* 添加到异步日志队列中
*
* @param
*/
public void put(Count count) {
// // 如果队列满了,主动调用批量方法
if (blockingQueue.size() >= queueSize) {
runBatch();
}
try {
blockingQueue.put(count);
} catch (InterruptedException e) {
LOG.warn("Interrupted!", e);
}
}
@Scheduled(cron = "${peroninfo.async.log.cron:0/5 * * * * ?}")
public void runBatch() {
if (runBatch) {
LOG.info("batchSave Is already running.");
return;
}
runBatch = true;
try {
// 批量保存日志
batchSave();
} catch (Throwable e) {
runBatch = false;
throw e;
}
runBatch = false;
}
/**
* 批量保存日志
*/
private void batchSave() {
List<Count> buffer = new ArrayList<>(batchSize);
do {
// 获取日志对象
Count poll = blockingQueue.poll();
if (poll == null) {
break;
}
buffer.add(poll);
if (buffer.size() >= batchSize) {
// 保存日志
save(buffer);
// 构建新的日志缓冲
buffer = new ArrayList<>(batchSize);
}
} while (true);
// 保存日志
save(buffer);
}
/**
* 保存日志
*
* @param buffer 日志缓冲
*/
private void save(List<Count> buffer) {
if (buffer.size() > 0) {
threadPool.execute(() -> countManagementService.saveLog(buffer));
}
}
}
service接口类:
public interface CountManagementService extends IService<Count> {
void saveLog(List<Count> buffer);
}
实现类:
@Override
public void saveLog(List<Count> buffer) {
try (SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false)) {
// 保存主表 RuleflowExecuteLog
for (Count tbPersonInterfacelog : buffer) {
// insert
sqlSession.insert("cc.xx.dao.CountMapper.saveCount", tbPersonInterfacelog);
}
// 提交事务
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
cc.xx.dao.CountMapper.saveCount:
public interface CountMapper extends MyMapper<Count> {
int saveCount( Count count);
}
插入语句实现:
<insert id="saveCount" parameterType="cc.xx.domain.Count">
insert into tableA () values ()
</insert>