简体   繁体   中英

Cant configure Spring Batch to run jobs consequently

I use the following job launcher to launch my spring batch jobs:

  <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository"/>
    <property name="taskExecutor">
      <bean class="org.springframework.core.task.SimpleAsyncTaskExecutor"/>
    </property>
  </bean>

and my job definition is

  <job id="bulkExportJob" restartable="false" xmlns="http://www.springframework.org/schema/batch">
    <description>Exports an application to pdf in a bulk operation</description>

    <step id="startExport" next="exportFileTree">
      <description>Do something to start the export</description>
      <tasklet ref="startBulkActionTasklet"/>
    </step>

    <step id="exportFileTree" next="zipFileTree">
      <description>Export the application</description>
      <tasklet>
        <chunk reader="bulkActionTargetReader" writer="bulkExportFileTreeWriter" commit-interval="1" skip-limit="100000000">
          <skippable-exception-classes>
            <!-- Exceptions are handled internally to the Writer so exception should not be treated as failures -->
            <include class="java.lang.Exception"/>
          </skippable-exception-classes>
        </chunk>
      </tasklet>
      <listeners>
        <listener ref="promotionListener"/>
      </listeners>
    </step>

    <step id="zipFileTree" next="sendEmail">
      <description>Creates a zip file</description>
      <tasklet ref="bulkExportZipWriter"/>
    </step>

    <step id="sendEmail" next="finishExport">
      <description>Send notification email</description>
      <tasklet ref="bulkExportSendNotification"/>
    </step>


    <step id="finishExport">
      <description>Finalise the export</description>
      <tasklet ref="finishBulkActionTasklet"/>
    </step>

  </job>

My intention was to run one job at a time and queue all other incoming jobs. But from the log files I can see that all jobs run in parallel. As you can see from the the code snippets I don't have any additional code to make spring batch in parallel and yet it still does. Can you please point what am I doing wrong?

You are using SimpleAsyncTaskExecutor which is running jobs async and creating new thread for each job:

TaskExecutor implementation that fires up a new Thread for each task, executing it asynchronously.

Supports limiting concurrent threads through the "concurrencyLimit" bean property. By default, the number of concurrent threads is unlimited.

NOTE: This implementation does not reuse threads! Consider a thread-pooling TaskExecutor implementation instead, in particular for executing a large number of short-lived tasks.

As suggested if you absolutely need SimpleAsyncTaskExecutor you can set concurrencyLimit to 1 (with throttle-limit="1" property) and have 1 job at time, but then you can use default SyncTaskExecutor which will run jobs sequentially and when one finishes other will be run (guess based on explanation that is what you want).

Try adding throttle-limit="1" to the tasklet definitions. The documentation at http://docs.spring.io/spring-batch/trunk/reference/html/scalability.html indicates it defaults to 4.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM