Quartz 与 Spring Batch 集成时出现的问题

发布于 2024-11-29 06:12:41 字数 3466 浏览 0 评论 0原文

我正在尝试使用 spring Batch &项目中的石英。目标是使用 Quartz 安排春季批量作业。我有以下 bean -

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恰似旧人归 2024-12-06 06:12:41

我得到了这个工作。我不知道具体原因&如果有人可以发表评论,我将不胜感激 -

我在 applicationContext.xml 中添加了以下 bean 配置

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

现在我没有得到该异常 & quartz 能够调用 spring 批处理作业

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

乖乖公主 2024-12-06 06:12:41

Spring批处理借助提供的作业参数来区分同一作业的不同执行。

如代码示例中所示,您没有传递任何 Job 参数,因此根据 Spring Batch,所有作业执行都将相同,并且任何陷入运行状态的实例都将导致 Spring Batch 抛出 JobAlreadyRunningException。

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.

凉城已无爱 2024-12-06 06:12:41

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

  1. 添加作业参数以使作业唯一
  2. 将静态变量放入具有 isRunning 的 Javaquartz 触发器中因此,我们不会让作业在一个 JVM 中运行两个实例,以防时间过紧。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文