本文主要是介绍Spring4 quartz job xml configuration,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 定义job details
public class NotifyJobProcessor extends JobProcessor {private static final Log LOG = LogFactory.getLog(NotifyJobProcessor.class);public NotifyJobProcessor() {}@Overridepublic void execute(JobExecutionContext context) throws JobExecutionException {String jobName = UploadType.NOTIFY.name();LOG.debug("%s Job is running ......", jobName);Channel channel = getChannelFromContext(context);try {if (channel != null && channel.isWritable()) {UploadHeartbeat heartbeat = new UploadHeartbeat(UploadType.NOTIFY);UploadFunction notifyFunction = new UploadFunction(heartbeat);UploadFrame frame = new UploadFrame(notifyFunction);channel.writeAndFlush(frame);return;}}catch (Exception e) {LOG.error("Send %s Message failed !!!", jobName);e.printStackTrace();}LOG.info("Channel is not active or null for %s Job, not need run it any more.", jobName);// channel is not active, then disable triggerdisableTrigger(jobName);}}
jobprocessor定义:
public abstract class JobProcessor implements Job {private static final Log LOG = LogFactory.getLog(JobProcessor.class);protected Channel getChannelFromContext(JobExecutionContext context) {Channel channel = null;try {Scheduler schedule = context.getScheduler();SchedulerContext scheduleContext = schedule.getContext();channel = (Channel) scheduleContext.get("channel");}catch (SchedulerException e) {LOG.error("get Channel in Job Execution Context failed !!!", e);e.printStackTrace();}return channel;}protected void disableTrigger(String jobName) {try {LOG.info("Going to remove %s Job", jobName);UploadJobManager.getInstance().unTrigger(jobName);}catch (SchedulerException e) {LOG.error("Untrigger %s Job failed !!!", jobName);e.printStackTrace();}}
}
2. 配置job details
<!-- For times when you need more complex processing, passing data to the scheduled job --><bean name="notify.job.processor.detail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"><property name="jobClass" value="com.eifesun.monitor.upload.uploader.processor.NotifyJobProcessor" /><property name="durability" value="true" /></bean>
3.配置job trigger
变量
<bean id="heartbeatSecond" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"><property name="targetObject" ref="upload.setting"/><property name="targetMethod" value="getSendHeartBeatSecond"/></bean>
trigger
<bean id="notify.job.processor.trigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"><property name="jobDetail" ref="notify.job.processor.detail" /><property name="startDelay" value="5" /><property name="repeatInterval" value="#{heartbeatSecond}" /></bean>
4.配置job scheduler
<bean id="job.processor.scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="autoStartup" value="false"/><property name="jobDetails"><list><ref bean="notify.job.processor.detail" /></list></property><property name="triggers"><list><ref bean="notify.job.processor.trigger" /></list></property></bean>
最后,
如果job中有外部依赖,可以这样配置
<bean name="report.job.processor.detail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"><property name="jobClass"value="com.eifesun.monitor.upload.uploader.processor.ReportJobProcessor" /><property name="jobDataMap"><map><entry key="inverterRepo" value-ref="inverter.repo" /><entry key="deviceRepo" value-ref="device.repo" /></map></property><property name="durability" value="true" /></bean>
这篇关于Spring4 quartz job xml configuration的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!