Problem in Integrating Quartz with Spring Batch

非 Y 不嫁゛ 提交于 2019-12-11 02:19:19

问题


I am trying to use spring batch & quartz in a project. The objective is to schedule a spring-batch job using Quartz. I have the following beans -

bean id="updateDataFeedJob"  class="package.UpdateDataFeedJob" />     

<bean id="UpdaterOnScheduleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="updateDataFeedJob"/>
    <!-- the method to call inside of com.siemens.scr.schedule.UpdateDataFeedJob -->
    <property name="targetMethod" value="execute"/>
</bean> 

<bean id="cronTriggerId" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="UpdaterOnScheduleJobDetail" />
    <!-- run every morning at 3AM -->
    <!--  <property name="cronExpression" value="0 0 3 * * ?" /> -->

    <!-- Fires every five minutes -->
    <property name="cronExpression" value="0 0/5 * * * ?" />
</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="cronTriggerId" />
        </list>
    </property>
</bean>

The UpdateDataFeedJob is -

public class UpdateDataFeedJob  {   
  public void execute() throws Exception {
    System.out.println("Hello World. Running the job");
    CommandLineJobRunner.main(new String[]{"GetFleetUpdatesJob.xml", "getFleetUpdatesJob"});
  }  
}

Sample Calling is -

public class Client {
    public static void main(String[] args){
        //CommandLineJobRunner.main(new String[]{"GetFleetUpdatesJob.xml", "getFleetUpdatesJob"});
        new ClassPathXmlApplicationContext("GetFleetUpdatesJob.xml");
    }   
}

I keep getting the following exception -

ERROR - Job Terminated in error: A job execution for this job is already running: JobInstance: id=0, JobParameters=[{}], Job=[getFleetUpdatesJob]
org.springframework.batch.core.repository.JobExecutionAlreadyRunningException: A job execution for this job is already running: JobInstance: id=0, JobParameters=[{}], Job=[getFleetUpdatesJob]
    at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:112)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:98)
    at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:291)
    at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:448)
    at com.siemens.scr.schedule.UpdateDataFeedJob.execute(UpdateDataFeedJob.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:276)
    at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:260)
    at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)

回答1:


I got this working. I am not aware of the exact reason & will appreciate if someone can comment -

I added the following bean config in the applicationContext.xml

<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>

Now i dont get that exception & quartz is able to call the spring batch job




回答2:


Spring batch differentiates between different executions of same job with the help of Job parameters supplied.

As seen in your code sample, you are passing no Job parameters hence all job executions will be same according to spring batch and any instance which is stuck in running state will cause spring batch to throw JobAlreadyRunningException.




回答3:


We had same issue. We were worried someone might tighten the quartz window and quartz does not have the ScheduleExector.scheduleAtFixedDelay. It only has the shceduleAtFixedRate so batch jobs "could" overlap so we:

  1. add job parameters to make the job unique
  2. put a static variable in our Java quartz trigger that has an isRunningfield so we don't let the job run two instances in one JVM in case they put the period to be too tight.


来源:https://stackoverflow.com/questions/7046639/problem-in-integrating-quartz-with-spring-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!