package com.gyee.alarm.task.thread;

import com.gyee.alarm.feigns.IAlarmService;
import com.gyee.alarm.model.vo.AlarmTag;
import com.gyee.alarm.model.vo.AlarmVo;
import com.gyee.alarm.util.realtimesource.IEdosUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;

public class AlarmThread implements Runnable {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private Executor executor;

    private IEdosUtil edosUtil;


    private IAlarmService alarmService;

    private List<AlarmTag> alarmVoList;
    private String str;

    private Integer interval;
    private Integer readRows;
    private String alarmType;
    private boolean serverStarted=true;
    public AlarmThread(Executor executor, IEdosUtil edosUtil, IAlarmService alarmService, List<AlarmTag> alarmVoList, Integer interval, String str,Integer readRows,String alarmType) {
        this.executor = executor;
        this.edosUtil = edosUtil;
        this.alarmService = alarmService;
        this.alarmVoList = alarmVoList;
        this.interval = interval;
        this.str = str;
        this.readRows = readRows;
        this.alarmType=alarmType;
    }


    @Override
    public synchronized void run() {


        String task = "task@ " + str;
        logger.info("创建任务并提交到线程池中：" + task);
        FutureTask<String> futureTask = new FutureTask<>(new AlarmThreadPool(task,
				edosUtil, alarmService,  alarmVoList,interval,str,readRows,alarmType));

        executor.execute(futureTask);


        // 在这里可以做别的任何事情
        String result = null;
        try {
            // 取得结果，同时设置超时执行时间为1秒。同样可以用future.get()，不设置执行超时时间取得结果
            result = futureTask.get();
        } catch (InterruptedException e) {
            futureTask.cancel(true);
        } catch (ExecutionException e) {
            futureTask.cancel(true);
        } catch (Exception e) {
            futureTask.cancel(true);
            // 超时后，进行相应处理
        } finally {
            logger.info("完成任务并提交到线程池中：" + task);
        }
    }
}