Hello,
Can someone provide a sample for scheduling a batch job with Quartz or any other method.
Thanks in Advance
Printable View
Hello,
Can someone provide a sample for scheduling a batch job with Quartz or any other method.
Thanks in Advance
There used to be a Quartz sample job before M2, but it was really much more an example of Spring-Quartz integration rather than anything Spring Batch specific.
If you need something quickly take a look at the Quartz chapter in Spring documentation and it should be mostly a copy-paste issue. Or you can start a Jira issue - I should be able to attach an example file within next week.
Thanks for the reply. I will refer to that document on Quartz and try to implement in batch envirnment.
I have created JIRA issue and the link is
http://jira.springframework.org/browse/BATCH-276
I would be glad if you can suggest an sample code or example.
Here is a sample crontab entry (assuming classpath and beanRefContext are set up properly):
At 6AM every weekday morning, launch "myJob" from file "myJob.xml"Code:0 6 * * 1-5 java org.springframework.batch.execution.bootstrap.support.BatchCommandLineLauncher -Djob.configuration.path=jobs/myJob.xml -Djob.name=myJob
Note you need to have classpath configured first and only then you can use the crontab one-liner.
Update: sorry the original entry states that already, overlooked it
Writing a job in Quartz would be the same procedure as the crontab above, only you would use the quartz Native job as your quartz job, with the spring batch command line launcher as it's process to be executed.
You could also use Spring's quartz integration to inject a Spring Batch JobLauncher, although every job launched this way will be in the same jvm.
Here is an example for both local jvm and native job variants:
The QuartzBatchJob class is basicly a copy-paste of BatchCommandLineLauncher#start method - maybe it is worth to extract this logic from the "command line" class?Code:<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- QUARTZ JOBS -->
<bean id="localJvmJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="org.springframework.batch.sample.launch.QuartzBatchJob" />
<property name="jobDataAsMap">
<map>
<entry key="jobName" value="fixedLengthImportJob" />
<entry key="jobConfigurationPath" value="jobs/fixedLengthImportJob.xml" />
</map>
</property>
</bean>
<bean id="nativeJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="org.quartz.jobs.NativeJob" />
<property name="jobDataAsMap">
<map>
<entry key="command" value="java org.springframework.batch.execution.bootstrap.support.BatchCommandLineLauncher -Djob.configuration.path=jobs/fixedLengthImportJob.xml -Djob.name=fixedLengthImportJob" />
<entry key="jobConfigurationPath" value="jobs/fixedLengthImportJob.xml" />
</map>
</property>
</bean>
<!-- TRIGGERS -->
<bean id="exampleCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="nativeJob" />
<!-- run every morning at 6 AM -->
<property name="cronExpression" value="0 0 6 * * ?" />
</bean>
<bean id="exampleSimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="localJvmJob" />
<property name="startDelay" value="1000" />
<property name="repeatInterval" value="30000" />
</bean>
<!-- SCHEDULER -->
<bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="exampleCronTrigger" />
<ref bean="exampleSimpleTrigger" />
</list>
</property>
</bean>
</beans>
Code:Exception in thread "Sched1_QuartzSchedulerThread" java.lang.NoSuchMethodError: org.quartz.SchedulerException: method <init>(Ljava/lang/String;Ljava/lang/Throwable;)V not found
at org.springframework.scheduling.quartz.AdaptableJobFactory.newJob(AdaptableJobFactory.java:41)
at org.quartz.core.JobRunShell.initialize(JobRunShell.java:133)
at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:353)
Which version of Quartz do you have - Spring Core removed supporyt for older versions in the latest release, I believe.