/**
 * Copyright
 * All right reserved.
 * 项目名称:
 * 创建日期:2022/5/23
 */
package org.springblade.task;


import lombok.extern.slf4j.Slf4j;
import org.glassfish.jersey.internal.guava.ThreadFactoryBuilder;
import org.springblade.modules.baseinfo.rtu.service.IRtuBaseInfoService;
import org.springblade.modules.business.warning.service.IRtuWarningService;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.*;

/***
 * Date:2022/5/22
 * Title: 任务执行线程池
 * Description：任务执行线程池
 * @author swp
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
@Component
@EnableScheduling
public class TaskManager {

	/**
	 * 公共线程池
	 **/
	private static ThreadFactory publicThreadFactory = new ThreadFactoryBuilder().setNameFormat("task-thread-pool-%d").build();
	private static ExecutorService publicThreadPool = new ThreadPoolExecutor(1, 2,
		0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1024), publicThreadFactory, new ThreadPoolExecutor.AbortPolicy());

	@Resource
	private IRtuBaseInfoService rtuBaseInfoService;

	@Resource
	private IRtuWarningService rtuWarningService;

	//@Scheduled(cron = "0 0/20 * * * * ")
	public void warnTask() {
		try {
			WarningCheckTask warningTask = new WarningCheckTask(rtuBaseInfoService, rtuWarningService);
			FutureTask<Integer> futureTask = new FutureTask<>(warningTask);
			publicThreadPool.execute(futureTask);
		} catch (Exception e) {
			log.error("{}", e.getMessage());
		}
	}

	//@Scheduled(cron = "0 0/5 * * * * ")
	public void offlineWarnTask() {
		try {
			OfflineWarningCheckTask warningTask = new OfflineWarningCheckTask(rtuBaseInfoService, rtuWarningService);
			FutureTask<Integer> futureTask = new FutureTask<>(warningTask);
			publicThreadPool.execute(futureTask);
		} catch (Exception e) {
			log.error("{}", e.getMessage());
		}
	}

	public void submitTask(FutureTask<Integer> futureTask) {
		publicThreadPool.execute(futureTask);
	}
}
