Announcement Announcement Module
Collapse
No announcement yet.
scheduling a batch job Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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.

      Comment


      • #4
        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"

        Comment


        • #5
          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"


          ???

          Comment


          • #6
            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

            Comment


            • #7
              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.

              Comment


              • #8
                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?

                Comment


                • #9
                  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)

                  Comment


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

                    Comment


                    • #11
                      Originally posted by viper View Post
                      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"

                      ???
                      If you don't know what this is, don't worry.

                      It is a sample entry ("crontab" entry) to configure the built-in *NIX scheduling utility known as "cron" -- this is perhaps the simplest scheduler available to non-Microsoft users so I felt it would be a good example. Again, don't worry if it doesn't apply to you.
                      Last edited by dkaminsky; Jan 15th, 2008, 02:48 PM. Reason: added original post

                      Comment


                      • #12
                        Classpath...

                        Originally posted by robert.kasanicky View Post
                        Note you need to have classpath configured first and only then you can use the crontab one-liner...
                        Could someone show and example how to setup the classpath that includes all the proper deployment artifacts to make a batch applicaiton run under crontab?

                        thanks,
                        james

                        Comment


                        • #13
                          Originally posted by jdepaul View Post
                          Could someone show and example how to setup the classpath that includes all the proper deployment artifacts to make a batch applicaiton run under crontab?

                          thanks,
                          james
                          One way is to use maven to launch the job - just add the snippet below to your pom.xml and use "mvn exec:exec -Djob.name=myJob -Djob.configuration.path=path/to/myJob.xml" as command (or you can hardcode the parameters).

                          Code:
                          <plugin>
                                            <groupId>org.codehaus.mojo</groupId>
                                            <artifactId>exec-maven-plugin</artifactId>
                                            <executions>
                                                <execution>
                                                    <goals>
                                                        <goal>exec</goal>
                                                    </goals>
                                                </execution>
                                            </executions>
                                            <configuration>
                                                <executable>java</executable>
                                                <arguments>
                                                    <argument>-classpath</argument>
                                                    <!-- automatically creates the classpath using all project dependencies, 
                                                        also adding the project build directory -->
                                                    <classpath />
                                                    <!-- job configuration file -->
                                                    <argument>-Djob.configuration.path=${job.configuration.path}</argument>
                                                    <!-- job name -->
                                                    <argument>-Djob.name=${job.name}</argument>
                                                    <argument>
                                                        org.springframework.batch.execution.bootstrap.support.BatchCommandLineLauncher
                                                    </argument>
                                                </arguments>
                                            </configuration>
                                        </plugin>
                          Last edited by robert.kasanicky; Jan 17th, 2008, 04:03 AM. Reason: removed redundant arguments from plugin config

                          Comment


                          • #14
                            Could someone show and example how to setup the classpath that includes all the proper deployment artifacts to make a batch applicaiton run under crontab?
                            Maybe the simplest way is to ask maven to create appropriate application jar file:

                            Code:
                             <plugin>
                                            <groupId>org.apache.maven.plugins</groupId>
                                            <artifactId>maven-jar-plugin</artifactId>
                                            <configuration>
                                                <archive>
                                                    <manifest>
                                                        <!-- mainClass>fully.qualified.MainClass</mainClass -->
                                                        <addClasspath>true</addClasspath>
                                                    </manifest>
                                                </archive>
                                            </configuration>
                                        </plugin>

                            Comment


                            • #15
                              you could also use maven to copy all of the jars into a lib directory using the maven dependency plugin to copy them. For example:

                              mvn dependency:copy-dependencies -DoutputDirectory=../Lib

                              http://maven.apache.org/plugins/mave...cies-mojo.html

                              From there you could add the lib directory to your classpath when executing:

                              java -jar ...BatchCommandLineLauncher -cp ../Lib

                              Comment

                              Working...
                              X