spring-定时器-JobDetailBean jobDataAsMap

最近在项目中用到 了Spring的JobDetailBean,可以在JobDetailBean中调用QuartzJobBean,可以通过jobDataAsMap传递参数:

 

如:QuartzJobBean的实例TestTimeOutJob :

 

 

import java.util.HashMap;

import java.util.Map;

 

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.quartz.SchedulerException;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.ApplicationContext;

import org.springframework.scheduling.quartz.QuartzJobBean;

 

 

public class TestTimeOutJob extends QuartzJobBean {

 

private int id;

private String name;

public void setId(int id) {

this.id = id;

}

 

public void setname(String name) {

this.name = name;

}

 

@Override

protected void executeInternal(JobExecutionContext context) throws JobExecutionException {

try {

System.out.println("---------id: " + id);

System.out.println("---------name: " + name);

} catch (BeansException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SchedulerException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

}


spring bean.xml中配置:

<bean id="testTimeOutJob" class="org.springframework.scheduling.quartz.JobDetailBean" scope="prototype">

<property name="jobClass" value="xx.xx.xx.TeployTimeOutJob"></property>

</bean>



启动job,通过jobDataAsMap传递参数:


//通过spring的注入获取TestTimeOutJob实例
JobDetailBean testJob = context.getBean("testTimeOutJob", JobDetailBean.class);
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("id", "1");
jobDataMap.put("name", "test");
testJob.setJobDataMap(jobDataMap);
schedulerUtil.deployTimeOut(scheduler, deployJob);


//Util类,启动job
public class SchedulerUtil {
public void deployTimeOut(Scheduler scheduler, JobDetailBean job) {
try {
job.setName("test_timeout");
//10分钟调用一次
Trigger trigger = new SimpleTrigger(job.getName() + "_trigger", new Date(Calendar.getInstance().getTimeInMillis() + (1000 * 60 * 10)));
//调用job(TestTimeOutJob)
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

你可能感兴趣的:(spring-定时器-JobDetailBean jobDataAsMap)