How to Run Multiple Jobs in Spring Batch Using Annotations

How to run multiple jobs in spring batch using annotations

The names of the Beans have to be unique in the whole Spring Context.

In both jobs, you are instantiating the reader, writer and processor with the same methodname. The methodname is the name that is used to identifiy the bean in the context.

In both job-definitions, you have reader(), writer() and processor(). They will overwrite each other. Give them unique names like readerEmployee(), readerSalary() and so on.

That should solve your problem.

Spring batch - running multiple jobs in parallel

I believe that you can. Since you are new in spring batch (just like me) I would recommend that you go through the domain language of a batch if you haven't done so already.

Then you may start by configuring your own asynchronous JobLauncher. For example:

  @Bean
public JobLauncher jobLauncher() throws Exception
{
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();

jobLauncher.setJobRepository(jobRepository);
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
jobLauncher.afterPropertiesSet();

return jobLauncher;
}

Pay special attention to SimpleAsyncTaskExecutor (the job repo can be autowired). This configuration will allow asynchronous execution as visualized next:

Asynchronous

Compare it with the synchronous execution flow:

enter image description here

Maybe it would additionally help to quote the SimpleJobLauncher java doc:

Simple implementation of the JobLauncher interface. The Spring Core
TaskExecutor interface is used to launch a Job. This means that the
type of executor set is very important. If a SyncTaskExecutor is used,
then the job will be processed within the same thread that called the
launcher. Care should be taken to ensure any users of this class
understand fully whether or not the implementation of TaskExecutor
used will start tasks synchronously or asynchronously. The default
setting uses a synchronous task executor.

More details and configuration options - here.

At the end just create the jobs with different names and/or launch them with different parameter set. Naive example would be:

  @Autowired
public JobBuilderFactory jobBuilderFactory;

public Job createJobA() {
return jobBuilderFactory.get("A.txt")
.incrementer(new RunIdIncrementer())
.flow(step1())
.next(step2())
.end()
.build();
}

public Job createJobB() {
return jobBuilderFactory.get("B.txt")
.incrementer(new RunIdIncrementer())
.flow(step1())
.next(step2())
.end()
.build();
}

Executing these jobs with your asynchronous job launcher will create two job instances that which will execute in parallel. This is just one option, that may or may not be suitable for your context.

How to have multiple spring boot batch Configuration class

I think that the problem is that both job beans have the same name processJob, so one overwrites the other when the application context gets built. The simplest solution would be to use unique bean names like the names you are giving the Jobs: processJob1, processJob2.

Spring batch - How to run multiple jobs in parallel

this configuration works for me:

import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;import org.springframework.batch.core.launch.JobLauncher;import org.springframework.batch.core.launch.support.SimpleJobLauncher;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

@Configurationpublic class TaskExecutorBatchConfigurer extends DefaultBatchConfigurer {
@Bean public ThreadPoolTaskScheduler batchTaskScheduler() { ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler(); threadPoolTaskScheduler.setPoolSize(10); threadPoolTaskScheduler.afterPropertiesSet(); return threadPoolTaskScheduler; }
@Override protected JobLauncher createJobLauncher() throws Exception { SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); jobLauncher.setJobRepository(super.getJobRepository()); jobLauncher.setTaskExecutor(batchTaskScheduler()); jobLauncher.afterPropertiesSet(); return jobLauncher; }}

Multiple spring batch jobs

You can define your JobLauncher in some config file and set SimpleAsyncTaskExecutor as task executor which will run jobs async (not waiting for one to complete in order to start next one).

Here is code snippet from :

@Bean
public JobLauncher jobLauncher() {
final SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
final SimpleAsyncTaskExecutor simpleAsyncTaskExecutor = new SimpleAsyncTaskExecutor();
jobLauncher.setTaskExecutor(simpleAsyncTaskExecutor);
return jobLauncher;
}


Related Topics



Leave a reply



Submit