web项目简单的项目启动时执行定时任务

版权声明:本文为博主原创文章,随意转载。 https://blog.csdn.net/king13127/article/details/75230287

在spring的xml配置

	<bean id="casePlanSyncTimer" class="cn.com.chnsys.imp.dcs.ledict.http.CasePlanSyncTimer"
		init-method="startSyncTimer">
		<property name="pushTrialSchemaTest" ref="pushTrialSchemaTest" />

间隔n分钟定时类

public class CasePlanSyncTimer {

	/**
	 * LOG
	 */
	private Logger log = LoggerFactory.getLogger(CasePlanSyncTimer.class);

	/**
	 * 定时器
	 */
	private Timer timer = new Timer("CasePlanTimer");

	/**
	 * 类名
	 */
	private String className = getClass().getName();

	/**
	 * 案件排期查询条件规则
	 */
	private PushTrialSchemaTest pushTrialSchemaTest;

	/**
	 * 启动定时同步案件任务
	 */
	public void startSyncTimer() {
		String timerStr = DcsConfig.getConfig("LEDICT_SYNCHR_DATE_TIMER");
		int timeInt = StringUtils.isBlank(timerStr) ? 10 : Integer.parseInt(timerStr);//每次循环执行任务执行间隔时间默认10秒

		timer.scheduleAtFixedRate(new TimerTask() {
			public void run() {    //要执行的定时任务
				LogMessageFormatPrint.errorLogFormatPrint(log, className, "run", "0", "start Timer.......");
				pushTrialSchemaTest.push();
				LogMessageFormatPrint.errorLogFormatPrint(log, className, "run", "0", "end Timer.......");
			}
		}, 30000, 1000 * timeInt);// 这里设定将系统启动后延时30秒固定执行
	}

	/**
	 * @return the pushTrialSchemaTest
	 */
	public PushTrialSchemaTest getPushTrialSchemaTest() {
		return pushTrialSchemaTest;
	}

	/**
	 * @param pushTrialSchemaTest the pushTrialSchemaTest to set
	 */
	public void setPushTrialSchemaTest(PushTrialSchemaTest pushTrialSchemaTest) {
		this.pushTrialSchemaTest = pushTrialSchemaTest;
	}
	
	
}

每天定时任务

package cn.com.chnsys.imp.dcs.checkdataconnect;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.chnsys.imp.common.LogMessageFormatPrint;
import cn.com.chnsys.imp.dcs.sysconf.pojo.CourtRoomType;
import cn.com.chnsys.imp.dcs.sysconf.service.CourtRoomTypeService;
import cn.com.chnsys.imp.dcs.util.ConstantUtil;

/**
 * Description: 
 *<p>
 *定时连接数据库查询数据。防止数据库连接池连接超时
 *</p>
 *
 * @author yinpf
 * @version 1.0   
 *
 */
public class RegularCheckDataConnect {

	/**
	 * LOG
	 */
	private Logger log = LoggerFactory.getLogger(RegularCheckDataConnect.class);
	/**
	 * 类名
	 */
	private String className = getClass().getName();
	/**
	 * 类名
	 */
	private CourtRoomTypeService courtRoomTypeService;
	/**
	 * 时间间隔(默认一天)
	 */
    private static final long PERIOD_DAY = 24 * 60 * 60 * 1000;
    /**
     * 查询时间(默认凌晨两点)
     */
    private static final int REGULAR_CHECK_TIME = 2;
	/**
	 * 启动定时连接数据库任务
	 */
	public void startRegularCheckDataConnect() {
		//定时同步所有未同步排期并同步
		 Calendar calendar = Calendar.getInstance();  
	        calendar.set(Calendar.HOUR_OF_DAY, REGULAR_CHECK_TIME); //具体执行时间点  
	        calendar.set(Calendar.MINUTE,ConstantUtil.ZERO_VALUE);  
	        calendar.set(Calendar.SECOND,ConstantUtil.ZERO_VALUE);  
	        Date date=calendar.getTime(); //第一次执行定时任务的时间  
	        //如果第一次执行定时任务的时间 小于当前的时间  
	        //此时要在 第一次执行定时任务的时间加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。  
	        if (date.before(new Date())) {  
	            date = this.addDay(date, 1);  
	        }  
	        Timer timer = new Timer();  
	        TimerTask task =new TimerTask() {
				public void run() {
					LogMessageFormatPrint.errorLogFormatPrint(log, className, "run", "0", "start regular check data connect.......");
					courtRoomTypeService.getCourtRoomTypeTotalNum(new CourtRoomType());
					LogMessageFormatPrint.errorLogFormatPrint(log, className, "run", "0", "end  regular check data connect.......");
				}
			};  
	        //安排指定的任务在指定的时间开始进行重复的固定延迟执行。  
	        timer.schedule(task,date,PERIOD_DAY);
	}
	/**
	 * 增加或减少天数 
	 */
    private Date addDay(Date date, int num) {  
        Calendar startDT = Calendar.getInstance();  
        startDT.setTime(date);  
        startDT.add(Calendar.DAY_OF_MONTH, num);  
        return startDT.getTime();  
    }
    
	/**
	 * @return the courtRoomTypeService
	 */
	public CourtRoomTypeService getCourtRoomTypeService() {
		return courtRoomTypeService;
	}
	/**
	 * @param courtRoomTypeService the courtRoomTypeService to set
	 */
	public void setCourtRoomTypeService(CourtRoomTypeService courtRoomTypeService) {
		this.courtRoomTypeService = courtRoomTypeService;
	} 
    
}




猜你喜欢

转载自blog.csdn.net/king13127/article/details/75230287
今日推荐