Quartz与Spring Batch集成的问题

时间:2011-08-12 21:43:37

标签: spring quartz-scheduler spring-batch

我正在尝试使用spring batch&石英在一个项目中。目标是使用Quartz安排弹出批处理作业。我有以下豆子 -

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>

UpdateDataFeedJob是 -

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

示例调用是 -

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

我一直收到以下异常 -

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)

3 个答案:

答案 0 :(得分:1)

我得到了这个工作。我不知道确切的原因&amp;如果有人可以评论,我将不胜感激 -

我在 applicationContext.xml

中添加了以下bean配置
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>

现在我没有得到那个例外&amp; quartz可以调用spring批处理作业

答案 1 :(得分:1)

Spring批处理在提供的Job参数的帮助下区分同一作业的不同执行。

如您的代码示例所示,您没有传递任何Job参数,因此根据spring批处理,所有作业执行都是相同的,任何陷入运行状态的实例都会导致spring批处理抛出JobAlreadyRunningException。

答案 2 :(得分:0)

我们有同样的问题。我们担心有人可能会收紧石英窗口而石英没有ScheduleExector.scheduleAtFixedDelay。它只有shceduleAtFixedRate所以批处理作业“可以”重叠,所以我们:

  1. 添加作业参数以使作业唯一
  2. 在我们的Java石英触发器中放置一个静态变量,该变量具有isRunning字段,因此我们不会让作业在一个JVM中运行两个实例,以防它们使周期太紧。