之前有寫過通過MethodInvokingJobDetailFactoryBean來實現定時任務
還有一種方式是JobDetailBean來實現定時任務
下面來看怎么配置
1.application.xml文件中(任務調度配置觸發器)
<import resource="job/clearing/merfile/create_merfile.xml"/>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="reconFileCheckTrigger"/> <!-- 檢查是否生成商戶對賬文件 -->
</list>
</property>
<property name="autoStartup" value="true"/>
</bean>
<!-- SpringBatch定時任務配置 -->
<batch:job-repository id="jobRepository" data-source="dataSource" transaction-manager="transactionManagerClear" isolation-level-for-create="REPEATABLE_READ" table-prefix="BATCH_" max-varchar-length="1000" />
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
2.create_merfile.xml
<!-- 調度觸發器 -->
<bean id="reconFileCheckTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="reconFileCheckJobDetail" />
<property name="cronExpression" value="0 45 10 ? * *" />
</bean>
<!-- 配置調度程序quartz -->
<bean id="reconFileCheckJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.ninefbank.smallpay.clear.JobLauncherDetailsCommon" />
<property name="jobDataAsMap">
<map>
<entry key="job" value-ref="reconFileCheckJob" />
<entry key="jobLauncher" value-ref="jobLauncher" />
<entry key="jobParameterCommon" value-ref="jobParamsCheckFile" />
</map>
</property>
</bean>
<batch:job id="reconFileCheckJob" job-repository="jobRepository">
<batch:step id="reconFileCheckStep1">
<batch:tasklet ref="reconFileCheck"/>
</batch:step>
</batch:job>
<bean id="jobParamsCheckFile" class="com.ninefbank.smallpay.clear.JobParameterCommonChild" />
3.JobLauncherDetailsCommon.java
1 private static Logger logger = LoggerFactory.getLogger(JobLauncherDetailsCommon.class); 2 private JobLauncher jobLauncher; 3 private Job job; 4 private JobParameterCommon jobParameterCommon; 5 6 public void setJobLauncher(JobLauncher jobLauncher) { 7 this.jobLauncher = jobLauncher; 8 } 9 public void setJob(Job job) { 10 this.job = job; 11 } 12 public void setJobParameterCommon(JobParameterCommon jobParameterCommon) { 13 this.jobParameterCommon = jobParameterCommon; 14 } 15 16 protected void executeInternal(JobExecutionContext context) { 17 18 JobExecution result = null; 19 try { 20 result = jobLauncher.run(job, jobParameterCommon.getJobParametersFromJobMap()); 21 22 } catch (Exception e) { 23 logger.error("執行job失敗,job名稱:{}", new Object[]{job.getName()}, e); 24 throw new ApplicationException("執行job失敗"); 25 } 26 27 ExitStatus es = result.getExitStatus(); 28 String exitCode = es.getExitCode(); 29 if (ExitStatus.COMPLETED.getExitCode().equals(exitCode)) { 30 logger.info("任務執行完成,job名稱:{};exitCode={};exitDesc={}", new Object[]{job.getName(), exitCode, es.getExitDescription()}); 31 32 } else { 33 logger.debug("任務執行失敗,job名稱:{};exitCode={};exitDesc={}", new Object[]{job.getName(), exitCode, es.getExitDescription()}); 34 } 35 }
其實吧這兩種呢都可以實現定時任務就是看執行的一個實在RAM中(第一種)一個是運行在RDB中(第二種),鄙人動手能力也不強,我也沒試過只是看別人有這么解析的,參考文章可以看:
http://donald-draper.iteye.com/blog/2323591
