Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: scheduling a batch job

  1. #1
    Join Date
    Dec 2007
    Posts
    25

    Default scheduling a batch job

    Hello,
    Can someone provide a sample for scheduling a batch job with Quartz or any other method.

    Thanks in Advance

  2. #2

    Default

    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.

  3. #3
    Join Date
    Dec 2007
    Posts
    25

    Default

    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.

  4. #4

    Default crontab example

    Here is a sample crontab entry (assuming classpath and beanRefContext are set up properly):

    Code:
    0 6 * * 1-5 java org.springframework.batch.execution.bootstrap.support.BatchCommandLineLauncher -Djob.configuration.path=jobs/myJob.xml -Djob.name=myJob
    At 6AM every weekday morning, launch "myJob" from file "myJob.xml"

  5. #5
    Join Date
    Jan 2008
    Location
    Thailand
    Posts
    2

    Default

    Quote Originally Posted by dkaminsky View Post
    Here is a sample crontab entry (assuming classpath and beanRefContext are set up properly):

    Code:
    0 6 * * 1-5 java org.springframework.batch.execution.bootstrap.support.BatchCommandLineLauncher -Djob.configuration.path=jobs/myJob.xml -Djob.name=myJob
    At 6AM every weekday morning, launch "myJob" from file "myJob.xml"


    ???

  6. #6

    Default

    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

  7. #7
    Join Date
    Dec 2006
    Posts
    1,061

    Default

    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.

  8. #8

    Default

    Here is an example for both local jvm and native job variants:

    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>
    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?

  9. #9
    Join Date
    Jan 2008
    Location
    Thailand
    Posts
    2

    Default Error

    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)

  10. #10
    Join Date
    Jun 2005
    Posts
    4,231

    Default

    Which version of Quartz do you have - Spring Core removed supporyt for older versions in the latest release, I believe.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •