Spring batch - running multiple jobs in parallelRun two jobs in parallel using spring batchMultiple jobs in spring batch not working with xml setupWhat is the difference between concurrency and parallelism?What's the difference between @Component, @Repository & @Service annotations in Spring?Recommended approach for parallel spring batch jobsSpring Batch Integration - Multiple files as single MessageSpring Integration poller starting same Spring Batch job multiple timesHow to run parallel jobs in spring batchSpring Batch Job Running in Infinite loopDynamically create Job in Spring Batchhow to trigger spring batch from spring integration java dsl Integration file poller integrationflowsSpring integration with spring batch
New Order #2: Turn My Way
Calculate Pi using Monte Carlo
Showing mass murder in a kid's book
How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?
Can a Knock spell open the door to Mordenkainen's Magnificent Mansion?
Trouble reading roman numeral notation with flats
Mortal danger in mid-grade literature
Why didn’t Eve recognize the little cockroach as a living organism?
What is it called when someone votes for an option that's not their first choice?
Output visual diagram of picture
Are hand made posters acceptable in Academia?
Can you take a "free object interaction" while incapacitated?
If the Dominion rule using their Jem'Hadar troops, why is their life expectancy so low?
Is this saw blade faulty?
How to test the sharpness of a knife?
What is the meaning of "You've never met a graph you didn't like?"
Are all namekians brothers?
Relations between homogeneous polynomials
Derivative of an interpolated function
Why does the frost depth increase when the surface temperature warms up?
How do I prevent inappropriate ads from appearing in my game?
Checking @@ROWCOUNT failing
Why can't I get pgrep output right to variable on bash script?
Do people actually use the word "kaputt" in conversation?
Spring batch - running multiple jobs in parallel
Run two jobs in parallel using spring batchMultiple jobs in spring batch not working with xml setupWhat is the difference between concurrency and parallelism?What's the difference between @Component, @Repository & @Service annotations in Spring?Recommended approach for parallel spring batch jobsSpring Batch Integration - Multiple files as single MessageSpring Integration poller starting same Spring Batch job multiple timesHow to run parallel jobs in spring batchSpring Batch Job Running in Infinite loopDynamically create Job in Spring Batchhow to trigger spring batch from spring integration java dsl Integration file poller integrationflowsSpring integration with spring batch
I am new to Spring batch and couldn't figure out how to do this..
Basically I have a spring file poller which runs every N mins to look for files with some name (ex: A.txt & B.txt) in certain directory. At any moment in time, there could be max 2 files in this directory (A and B). Through Spring Batch Job, these two files will be processed and persisted to 2 different DB tables.
These files are somewhat similar, so the same processor/writer is used.
Right now the way I set up, every polling cycle 1 file is picked up and job is ran.
Let's say there are 2 files in the directory (A.txt and B.txt), is there a way to create 2 jobs so that both jobs can be run in parallel?
spring parallel-processing spring-integration spring-batch
add a comment |
I am new to Spring batch and couldn't figure out how to do this..
Basically I have a spring file poller which runs every N mins to look for files with some name (ex: A.txt & B.txt) in certain directory. At any moment in time, there could be max 2 files in this directory (A and B). Through Spring Batch Job, these two files will be processed and persisted to 2 different DB tables.
These files are somewhat similar, so the same processor/writer is used.
Right now the way I set up, every polling cycle 1 file is picked up and job is ran.
Let's say there are 2 files in the directory (A.txt and B.txt), is there a way to create 2 jobs so that both jobs can be run in parallel?
spring parallel-processing spring-integration spring-batch
add a comment |
I am new to Spring batch and couldn't figure out how to do this..
Basically I have a spring file poller which runs every N mins to look for files with some name (ex: A.txt & B.txt) in certain directory. At any moment in time, there could be max 2 files in this directory (A and B). Through Spring Batch Job, these two files will be processed and persisted to 2 different DB tables.
These files are somewhat similar, so the same processor/writer is used.
Right now the way I set up, every polling cycle 1 file is picked up and job is ran.
Let's say there are 2 files in the directory (A.txt and B.txt), is there a way to create 2 jobs so that both jobs can be run in parallel?
spring parallel-processing spring-integration spring-batch
I am new to Spring batch and couldn't figure out how to do this..
Basically I have a spring file poller which runs every N mins to look for files with some name (ex: A.txt & B.txt) in certain directory. At any moment in time, there could be max 2 files in this directory (A and B). Through Spring Batch Job, these two files will be processed and persisted to 2 different DB tables.
These files are somewhat similar, so the same processor/writer is used.
Right now the way I set up, every polling cycle 1 file is picked up and job is ran.
Let's say there are 2 files in the directory (A.txt and B.txt), is there a way to create 2 jobs so that both jobs can be run in parallel?
spring parallel-processing spring-integration spring-batch
spring parallel-processing spring-integration spring-batch
asked Aug 16 '17 at 16:38
ljustinljustin
2319
2319
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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:
Compare it with the synchronous execution flow:
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.
Awesome example. It would also be highly beneficial if you could list the method on how to capture the completion of the job in the main thread. Just to complete the great answer! :)
– Abhi
Mar 12 at 6:40
add a comment |
There are very good approaches in order to run jobs in async mode with Spring, it is just a matter of how is configured the JobLauncher
. The JobLauncher
has a taskExecutor
property and the asynchronous execution could be activated depending on the implementation that is assigned to that property.
You can find all the TaskExecutor
types that Spring can provide and depending on your needs select the best approach to accomplish your batch asynchronous jobs. Task Executors Types in Spring
For example SimpleAsyncTaskExecutor
is a task executor that will create a new Thread
on any invocation and that could generate a performance issue if the execution runs with high frequency. In the other hand there are also TaskExecutors
types that provides pooling features in order to reuse resources and maximize the efficiency of the system.
Here is an small example of how configure a ThreadPoolTaskExecutor
:
A) Configure ThreadPoolTaskExecutor Bean
@Bean
public ThreadPoolTaskExecutor taskExecutor()
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(15);
taskExecutor.setMaxPoolSize(20);
taskExecutor.setQueueCapacity(30);
return taskExecutor;
B) Configure JobLauncher Bean
@Bean
public JobLauncher jobLauncher(ThreadPoolTaskExecutor taskExecutor, JobRepository jobRepository)
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setTaskExecutor(taskExecutor);
jobLauncher.setJobRepository(jobRepository);
return jobLauncher;
C) Inject your JobLauncher
and your Jobs
configuration
@Autowired
private JobLauncher jobLauncher;
@Autowired
@Qualifier("job1-file-A")
private Job job1;
@Autowired
@Qualifier("job2-file-B")
private Job job2;
D) Schedule the jobs
@Scheduled(cron = "*/1 * * * * *")
public void run1()
Map<String, JobParameter> confMap = new HashMap<>();
confMap.put("time", new JobParameter(System.currentTimeMillis()));
JobParameters jobParameters = new JobParameters(confMap);
try
jobLauncher.run(job1, jobParameters);
catch (Exception ex)
logger.error(ex.getMessage());
@Scheduled(cron = "*/1 * * * * *")
public void run2()
Map<String, JobParameter> confMap = new HashMap<>();
confMap.put("time", new JobParameter(System.currentTimeMillis()));
JobParameters jobParameters = new JobParameters(confMap);
try
jobLauncher.run(job2, jobParameters);
catch (Exception ex)
logger.error(ex.getMessage());
E) Finally on your SpringBoot Class @EnableBatchProcessing
and @EnableScheduling
@EnableBatchProcessing
@EnableScheduling
@SpringBootApplication
public class MyBatchApp {
It should be the accepted answer.
– davidxxx
Mar 7 at 20:40
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45718888%2fspring-batch-running-multiple-jobs-in-parallel%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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:
Compare it with the synchronous execution flow:
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.
Awesome example. It would also be highly beneficial if you could list the method on how to capture the completion of the job in the main thread. Just to complete the great answer! :)
– Abhi
Mar 12 at 6:40
add a comment |
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:
Compare it with the synchronous execution flow:
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.
Awesome example. It would also be highly beneficial if you could list the method on how to capture the completion of the job in the main thread. Just to complete the great answer! :)
– Abhi
Mar 12 at 6:40
add a comment |
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:
Compare it with the synchronous execution flow:
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.
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:
Compare it with the synchronous execution flow:
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.
edited Aug 16 '17 at 19:29
answered Aug 16 '17 at 18:52
Lachezar BalevLachezar Balev
7,02843052
7,02843052
Awesome example. It would also be highly beneficial if you could list the method on how to capture the completion of the job in the main thread. Just to complete the great answer! :)
– Abhi
Mar 12 at 6:40
add a comment |
Awesome example. It would also be highly beneficial if you could list the method on how to capture the completion of the job in the main thread. Just to complete the great answer! :)
– Abhi
Mar 12 at 6:40
Awesome example. It would also be highly beneficial if you could list the method on how to capture the completion of the job in the main thread. Just to complete the great answer! :)
– Abhi
Mar 12 at 6:40
Awesome example. It would also be highly beneficial if you could list the method on how to capture the completion of the job in the main thread. Just to complete the great answer! :)
– Abhi
Mar 12 at 6:40
add a comment |
There are very good approaches in order to run jobs in async mode with Spring, it is just a matter of how is configured the JobLauncher
. The JobLauncher
has a taskExecutor
property and the asynchronous execution could be activated depending on the implementation that is assigned to that property.
You can find all the TaskExecutor
types that Spring can provide and depending on your needs select the best approach to accomplish your batch asynchronous jobs. Task Executors Types in Spring
For example SimpleAsyncTaskExecutor
is a task executor that will create a new Thread
on any invocation and that could generate a performance issue if the execution runs with high frequency. In the other hand there are also TaskExecutors
types that provides pooling features in order to reuse resources and maximize the efficiency of the system.
Here is an small example of how configure a ThreadPoolTaskExecutor
:
A) Configure ThreadPoolTaskExecutor Bean
@Bean
public ThreadPoolTaskExecutor taskExecutor()
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(15);
taskExecutor.setMaxPoolSize(20);
taskExecutor.setQueueCapacity(30);
return taskExecutor;
B) Configure JobLauncher Bean
@Bean
public JobLauncher jobLauncher(ThreadPoolTaskExecutor taskExecutor, JobRepository jobRepository)
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setTaskExecutor(taskExecutor);
jobLauncher.setJobRepository(jobRepository);
return jobLauncher;
C) Inject your JobLauncher
and your Jobs
configuration
@Autowired
private JobLauncher jobLauncher;
@Autowired
@Qualifier("job1-file-A")
private Job job1;
@Autowired
@Qualifier("job2-file-B")
private Job job2;
D) Schedule the jobs
@Scheduled(cron = "*/1 * * * * *")
public void run1()
Map<String, JobParameter> confMap = new HashMap<>();
confMap.put("time", new JobParameter(System.currentTimeMillis()));
JobParameters jobParameters = new JobParameters(confMap);
try
jobLauncher.run(job1, jobParameters);
catch (Exception ex)
logger.error(ex.getMessage());
@Scheduled(cron = "*/1 * * * * *")
public void run2()
Map<String, JobParameter> confMap = new HashMap<>();
confMap.put("time", new JobParameter(System.currentTimeMillis()));
JobParameters jobParameters = new JobParameters(confMap);
try
jobLauncher.run(job2, jobParameters);
catch (Exception ex)
logger.error(ex.getMessage());
E) Finally on your SpringBoot Class @EnableBatchProcessing
and @EnableScheduling
@EnableBatchProcessing
@EnableScheduling
@SpringBootApplication
public class MyBatchApp {
It should be the accepted answer.
– davidxxx
Mar 7 at 20:40
add a comment |
There are very good approaches in order to run jobs in async mode with Spring, it is just a matter of how is configured the JobLauncher
. The JobLauncher
has a taskExecutor
property and the asynchronous execution could be activated depending on the implementation that is assigned to that property.
You can find all the TaskExecutor
types that Spring can provide and depending on your needs select the best approach to accomplish your batch asynchronous jobs. Task Executors Types in Spring
For example SimpleAsyncTaskExecutor
is a task executor that will create a new Thread
on any invocation and that could generate a performance issue if the execution runs with high frequency. In the other hand there are also TaskExecutors
types that provides pooling features in order to reuse resources and maximize the efficiency of the system.
Here is an small example of how configure a ThreadPoolTaskExecutor
:
A) Configure ThreadPoolTaskExecutor Bean
@Bean
public ThreadPoolTaskExecutor taskExecutor()
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(15);
taskExecutor.setMaxPoolSize(20);
taskExecutor.setQueueCapacity(30);
return taskExecutor;
B) Configure JobLauncher Bean
@Bean
public JobLauncher jobLauncher(ThreadPoolTaskExecutor taskExecutor, JobRepository jobRepository)
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setTaskExecutor(taskExecutor);
jobLauncher.setJobRepository(jobRepository);
return jobLauncher;
C) Inject your JobLauncher
and your Jobs
configuration
@Autowired
private JobLauncher jobLauncher;
@Autowired
@Qualifier("job1-file-A")
private Job job1;
@Autowired
@Qualifier("job2-file-B")
private Job job2;
D) Schedule the jobs
@Scheduled(cron = "*/1 * * * * *")
public void run1()
Map<String, JobParameter> confMap = new HashMap<>();
confMap.put("time", new JobParameter(System.currentTimeMillis()));
JobParameters jobParameters = new JobParameters(confMap);
try
jobLauncher.run(job1, jobParameters);
catch (Exception ex)
logger.error(ex.getMessage());
@Scheduled(cron = "*/1 * * * * *")
public void run2()
Map<String, JobParameter> confMap = new HashMap<>();
confMap.put("time", new JobParameter(System.currentTimeMillis()));
JobParameters jobParameters = new JobParameters(confMap);
try
jobLauncher.run(job2, jobParameters);
catch (Exception ex)
logger.error(ex.getMessage());
E) Finally on your SpringBoot Class @EnableBatchProcessing
and @EnableScheduling
@EnableBatchProcessing
@EnableScheduling
@SpringBootApplication
public class MyBatchApp {
It should be the accepted answer.
– davidxxx
Mar 7 at 20:40
add a comment |
There are very good approaches in order to run jobs in async mode with Spring, it is just a matter of how is configured the JobLauncher
. The JobLauncher
has a taskExecutor
property and the asynchronous execution could be activated depending on the implementation that is assigned to that property.
You can find all the TaskExecutor
types that Spring can provide and depending on your needs select the best approach to accomplish your batch asynchronous jobs. Task Executors Types in Spring
For example SimpleAsyncTaskExecutor
is a task executor that will create a new Thread
on any invocation and that could generate a performance issue if the execution runs with high frequency. In the other hand there are also TaskExecutors
types that provides pooling features in order to reuse resources and maximize the efficiency of the system.
Here is an small example of how configure a ThreadPoolTaskExecutor
:
A) Configure ThreadPoolTaskExecutor Bean
@Bean
public ThreadPoolTaskExecutor taskExecutor()
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(15);
taskExecutor.setMaxPoolSize(20);
taskExecutor.setQueueCapacity(30);
return taskExecutor;
B) Configure JobLauncher Bean
@Bean
public JobLauncher jobLauncher(ThreadPoolTaskExecutor taskExecutor, JobRepository jobRepository)
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setTaskExecutor(taskExecutor);
jobLauncher.setJobRepository(jobRepository);
return jobLauncher;
C) Inject your JobLauncher
and your Jobs
configuration
@Autowired
private JobLauncher jobLauncher;
@Autowired
@Qualifier("job1-file-A")
private Job job1;
@Autowired
@Qualifier("job2-file-B")
private Job job2;
D) Schedule the jobs
@Scheduled(cron = "*/1 * * * * *")
public void run1()
Map<String, JobParameter> confMap = new HashMap<>();
confMap.put("time", new JobParameter(System.currentTimeMillis()));
JobParameters jobParameters = new JobParameters(confMap);
try
jobLauncher.run(job1, jobParameters);
catch (Exception ex)
logger.error(ex.getMessage());
@Scheduled(cron = "*/1 * * * * *")
public void run2()
Map<String, JobParameter> confMap = new HashMap<>();
confMap.put("time", new JobParameter(System.currentTimeMillis()));
JobParameters jobParameters = new JobParameters(confMap);
try
jobLauncher.run(job2, jobParameters);
catch (Exception ex)
logger.error(ex.getMessage());
E) Finally on your SpringBoot Class @EnableBatchProcessing
and @EnableScheduling
@EnableBatchProcessing
@EnableScheduling
@SpringBootApplication
public class MyBatchApp {
There are very good approaches in order to run jobs in async mode with Spring, it is just a matter of how is configured the JobLauncher
. The JobLauncher
has a taskExecutor
property and the asynchronous execution could be activated depending on the implementation that is assigned to that property.
You can find all the TaskExecutor
types that Spring can provide and depending on your needs select the best approach to accomplish your batch asynchronous jobs. Task Executors Types in Spring
For example SimpleAsyncTaskExecutor
is a task executor that will create a new Thread
on any invocation and that could generate a performance issue if the execution runs with high frequency. In the other hand there are also TaskExecutors
types that provides pooling features in order to reuse resources and maximize the efficiency of the system.
Here is an small example of how configure a ThreadPoolTaskExecutor
:
A) Configure ThreadPoolTaskExecutor Bean
@Bean
public ThreadPoolTaskExecutor taskExecutor()
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(15);
taskExecutor.setMaxPoolSize(20);
taskExecutor.setQueueCapacity(30);
return taskExecutor;
B) Configure JobLauncher Bean
@Bean
public JobLauncher jobLauncher(ThreadPoolTaskExecutor taskExecutor, JobRepository jobRepository)
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setTaskExecutor(taskExecutor);
jobLauncher.setJobRepository(jobRepository);
return jobLauncher;
C) Inject your JobLauncher
and your Jobs
configuration
@Autowired
private JobLauncher jobLauncher;
@Autowired
@Qualifier("job1-file-A")
private Job job1;
@Autowired
@Qualifier("job2-file-B")
private Job job2;
D) Schedule the jobs
@Scheduled(cron = "*/1 * * * * *")
public void run1()
Map<String, JobParameter> confMap = new HashMap<>();
confMap.put("time", new JobParameter(System.currentTimeMillis()));
JobParameters jobParameters = new JobParameters(confMap);
try
jobLauncher.run(job1, jobParameters);
catch (Exception ex)
logger.error(ex.getMessage());
@Scheduled(cron = "*/1 * * * * *")
public void run2()
Map<String, JobParameter> confMap = new HashMap<>();
confMap.put("time", new JobParameter(System.currentTimeMillis()));
JobParameters jobParameters = new JobParameters(confMap);
try
jobLauncher.run(job2, jobParameters);
catch (Exception ex)
logger.error(ex.getMessage());
E) Finally on your SpringBoot Class @EnableBatchProcessing
and @EnableScheduling
@EnableBatchProcessing
@EnableScheduling
@SpringBootApplication
public class MyBatchApp {
answered Aug 17 '17 at 2:45
Daniel C.Daniel C.
2,81721019
2,81721019
It should be the accepted answer.
– davidxxx
Mar 7 at 20:40
add a comment |
It should be the accepted answer.
– davidxxx
Mar 7 at 20:40
It should be the accepted answer.
– davidxxx
Mar 7 at 20:40
It should be the accepted answer.
– davidxxx
Mar 7 at 20:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45718888%2fspring-batch-running-multiple-jobs-in-parallel%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown