spring 定时器-两种方式

方式一:
间隔一定时间 运行

<bean id="updateSessionIdTask" class="com.yang.iprms.common.UpdateSessionTask" autowire="byName" />

<bean id="updateSessionIdScheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="delay" value="20" />
<property name="period" value="60000" />
<property name="timerTask">
<ref local="updateSessionIdTask" />
</property>
</bean>

<bean id="updateSessionIdTimer" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref local="updateSessionIdScheduledTask" />
</list>
</property>
</bean>

java文件:

import java.util.TimerTask;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;


public class UpdateSessionTask extends TimerTask{

private Logger logger = Logger.getLogger(UpdateSessionTask.class);

@Autowired
IRecordService recordService;

@Override
public void run() {
try
{        
logger.info("进入定时更新未匹配录音数据");
       
       
        if (recordService == null)
      {
        logger.info("recordService == null");
      this.InitRecordService();
      }
          recordService.updateSessionList();     
}
catch(Exception exp)
{
logger.error("定时更新未匹配录音数据,异常");
exp.printStackTrace();
}

}

方式二: 指定corn时间运行

<bean id="recordFtpJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.yang.iprms.recordftp.task.RecordFtpTimerTask">
</property>
</bean>

<bean id="cronReportTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="recordFtpJob" />
<property name="cronExpression">
<value>0 0 11,12 * * ?</value>
</property>
</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
       <list><ref bean="cronReportTrigger"/></list>
    </property>
</bean>

11 点和 12点运行一次

java

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.channelsoft.xframe.utils.BeanFactoryUtil;


public class RecordFtpTimerTask extends QuartzJobBean{

/**
*
*/
private static final long serialVersionUID = 112212L;

private Logger logger = Logger.getLogger(this.getClass());

IRecordFtpService recordFtpService;

protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
try
{        
logger.info("进入定时上传语音文件数据");

       
        if (recordFtpService == null)
      {
        logger.info("recordService == null");
        recordFtpService = (IRecordFtpService)(BeanFactoryUtil.getBean("recordFtpService"));
      }
        recordFtpService.uploadWav();     
}
catch(Exception exp)
{
logger.error("定时更新未匹配录音数据,异常");
exp.printStackTrace();
}
}

//方式二 需要 jta-1.1.jar 和quartz-1.6.0.jar

你可能感兴趣的:(spring,quartz,定时器)