package com.steering.jobface.qcm.task;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.Logger;

public class MainTask extends HttpServlet {
	private static final long serialVersionUID = -6478402808156044583L;
	private final Logger logger = Logger.getLogger(MainTask.class);
	
	private class AuditMeltingDataTaskThread extends Thread{
		@Override
		public void run(){
			AuditMeltingDataTask auditMelting = new AuditMeltingDataTask();
			while(true){
        		try{
        			logger.info("AuditMeltingDataTaskThread");
        			Thread.sleep(1000 * 2);
        			auditMelting.run();
        		}catch(Exception ex){
        			logger.error(ex);
        		}
        	}
		}
	}
	
	private AuditMeltingDataTaskThread _auditMeltingDataTaskThread = null;
	
	private class JhyCpChemToQcmChemTaskThread extends Thread{
		@Override
		public void run(){
			while(true){
        		try{
        			logger.info("JhyCpChemToQcmChemTaskThread");
        			Thread.sleep(1000 * 2);
        			JhyCpChemToQcmChemTask.getInstance().run();
        		}catch(Exception ex){
        			logger.error(ex);
        		}
        	}
		}
	}
	
	private JhyCpChemToQcmChemTaskThread _jhyCpChemToQcmChemTaskThread = null;
	
	private class JhyLgChemToQcmChemTaskThread extends Thread{
		@Override
		public void run(){
			while(true){
        		try{
        			logger.info("JhyLgChemToQcmChemTaskThread");
        			Thread.sleep(1000 * 2);
        			JhyLgChemToQcmChemTask.getInstance().run();
        		}catch(Exception ex){
        			logger.error(ex);
        		}
        	}
		}
	}
	
	private JhyLgChemToQcmChemTaskThread _jhyLgChemToQcmChemTaskThread = null;
	
	private class SteelMakingAdvanceJudgeTaskThread extends Thread{
		@Override
		public void run(){
			while(true){
        		try{
        			logger.info("SteelMakingAdvanceJudgeTaskThread");
        			Thread.sleep(1000 * 2);
        			SteelMakingAdvanceJudgeTask.getInstance().run();
        		}catch(Exception ex){
        			logger.error(ex);
        		}
        	}
		}
	}
	
	private SteelMakingAdvanceJudgeTaskThread _steelMakingAdvanceJudgeTaskThread = null;
	
	private class SteerPhyDbToQcmLgPhyTaskThread extends Thread{
		@Override
		public void run(){
			while(true){
        		try{
        			logger.info("SteerPhyDbToQcmLgPhyTaskThread");
        			Thread.sleep(1000 * 2);
        			SteerPhyDbToQcmLgPhyTask.getInstance().run();
        		}catch(Exception ex){
        			logger.error(ex);
        		}
        	}
		}
	}
	
	private SteerPhyDbToQcmLgPhyTaskThread _steerPhyDbToQcmLgPhyTaskThread = null;
	
	private class JhyPhyDbBaseInfoToQcmPhyEntrustThread extends Thread{
		@Override
		public void run(){
			while(true){
        		try{
        			logger.info("JhyPhyDbBaseInfoToQcmPhyEntrustThread");
        			Thread.sleep(1000 * 2);
        			JhyPhyDbBaseInfoToQcmPhyEntrust.getInstance().run();
        		}catch(Exception ex){
        			logger.error(ex);
        		}
        	}
		}
	}
	
	private JhyPhyDbBaseInfoToQcmPhyEntrustThread _jhyPhyDbBaseInfoToQcmPhyEntrustThread = null;
	
	private class JhyCpPhyToQcmPhyTaskThread extends Thread{
		@Override
		public void run(){
			while(true){
        		try{
        			logger.info("JhyCpPhyToQcmPhyTaskThread");
        			Thread.sleep(1000 * 2);
        			JhyCpPhyToQcmPhyTask.getInstance().run();
        		}catch(Exception ex){
        			logger.error(ex);
        		}
        	}
		}
	}
	
	private JhyCpPhyToQcmPhyTaskThread _jhyCpPhyToQcmPhyTaskThread = null;
	
	private class BcPhyJudgeTaskThread extends Thread{
		@Override
		public void run(){
			while(true){
        		try{
        			logger.info("BcPhyJudgeTaskThread");
        			Thread.sleep(1000 * 2);
        			BcPhyJudgeTask.getInstance().run();
        		}catch(Exception ex){
        			logger.error(ex);
        		}
        	}
		}
	}
	
	private BcPhyJudgeTaskThread _bcPhyJudgeTaskThread = null;
	
	private ScheduledExecutorService _ses = null;
	
    /**
     * 初始化
     * @throws ServletException 
     */
	@Override
	public void init() throws ServletException {
		super.init();
		
		_auditMeltingDataTaskThread = new AuditMeltingDataTaskThread();
		_jhyCpChemToQcmChemTaskThread = new JhyCpChemToQcmChemTaskThread();
		_jhyLgChemToQcmChemTaskThread = new JhyLgChemToQcmChemTaskThread();
		_steelMakingAdvanceJudgeTaskThread = new SteelMakingAdvanceJudgeTaskThread();
		_steerPhyDbToQcmLgPhyTaskThread = new SteerPhyDbToQcmLgPhyTaskThread();
		_jhyPhyDbBaseInfoToQcmPhyEntrustThread = new JhyPhyDbBaseInfoToQcmPhyEntrustThread();
		_jhyCpPhyToQcmPhyTaskThread = new JhyCpPhyToQcmPhyTaskThread();
		_bcPhyJudgeTaskThread = new BcPhyJudgeTaskThread();
		
		//--------------------设定线程数量8个 服务启动30秒后执行
		_ses = Executors.newScheduledThreadPool(8);
		_ses.schedule(_auditMeltingDataTaskThread, 2000, TimeUnit.MILLISECONDS);
		_ses.schedule(_jhyCpChemToQcmChemTaskThread, 2000, TimeUnit.MILLISECONDS);
		_ses.schedule(_jhyLgChemToQcmChemTaskThread, 2300, TimeUnit.MILLISECONDS);
		_ses.schedule(_steelMakingAdvanceJudgeTaskThread, 23000, TimeUnit.MILLISECONDS);
		_ses.schedule(_steerPhyDbToQcmLgPhyTaskThread, 2600, TimeUnit.MILLISECONDS);
		_ses.schedule(_jhyPhyDbBaseInfoToQcmPhyEntrustThread, 26000, TimeUnit.MILLISECONDS);
		_ses.schedule(_jhyCpPhyToQcmPhyTaskThread, 3000, TimeUnit.MILLISECONDS);
		_ses.schedule(_bcPhyJudgeTaskThread, 30000, TimeUnit.MILLISECONDS);
	}
	
	@Override
	public void destroy() {
		super.destroy();
		
		_auditMeltingDataTaskThread.interrupt();
		_jhyCpChemToQcmChemTaskThread.interrupt();
		_jhyLgChemToQcmChemTaskThread.interrupt();
		_steelMakingAdvanceJudgeTaskThread.interrupt();
		_steerPhyDbToQcmLgPhyTaskThread.interrupt();
		_jhyPhyDbBaseInfoToQcmPhyEntrustThread.interrupt();
		_jhyCpPhyToQcmPhyTaskThread.interrupt();
		_bcPhyJudgeTaskThread.interrupt();
		_ses.shutdown();
	}
}
