Created
August 9, 2018 14:54
-
-
Save fmbenhassine/fb729bd25c75667c8dc24c9c4d9d6b1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2018 the original author or authors. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package org.springframework.batch.sample; | |
import org.springframework.batch.core.Job; | |
import org.springframework.batch.core.JobParameters; | |
import org.springframework.batch.core.JobParametersBuilder; | |
import org.springframework.batch.core.Step; | |
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | |
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; | |
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; | |
import org.springframework.batch.core.configuration.annotation.StepScope; | |
import org.springframework.batch.core.launch.JobLauncher; | |
import org.springframework.batch.core.launch.support.SimpleJobLauncher; | |
import org.springframework.batch.core.repository.JobRepository; | |
import org.springframework.batch.core.step.tasklet.Tasklet; | |
import org.springframework.batch.repeat.RepeatStatus; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.core.task.TaskExecutor; | |
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | |
@Configuration | |
@EnableBatchProcessing | |
public class MyJob { | |
@Autowired | |
private JobBuilderFactory jobs; | |
@Autowired | |
private StepBuilderFactory steps; | |
@Autowired | |
private JobRepository jobRepository; | |
@Bean | |
@StepScope | |
public Tasklet tasklet(@Value("#{jobParameters['name']}") String name) { | |
return (contribution, chunkContext) -> { | |
System.out.println(Thread.currentThread().getName() + ": working on " + name); | |
Thread.sleep(1000 * 10); | |
System.out.println(Thread.currentThread().getName() + ": work on " + name + " done"); | |
return RepeatStatus.FINISHED; | |
}; | |
} | |
@Bean | |
public Step step() { | |
return steps.get("step") | |
.tasklet(tasklet(null)) | |
.build(); | |
} | |
@Bean | |
public Job job() { | |
return jobs.get("job") | |
.start(step()) | |
.build(); | |
} | |
@Bean | |
public ThreadPoolTaskExecutor taskExecutor() { | |
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); | |
taskExecutor.setCorePoolSize(15); | |
taskExecutor.setMaxPoolSize(15); | |
taskExecutor.setQueueCapacity(0); | |
taskExecutor.setWaitForTasksToCompleteOnShutdown(true); | |
return taskExecutor; | |
} | |
@Bean | |
public JobLauncher jobLauncher(TaskExecutor taskExecutor) { | |
SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); | |
jobLauncher.setJobRepository(jobRepository); | |
jobLauncher.setTaskExecutor(taskExecutor); | |
return jobLauncher; | |
} | |
public static void main(String[] args) throws Exception { | |
ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class); | |
JobLauncher jobLauncher = context.getBean(JobLauncher.class); | |
Job job = context.getBean(Job.class); | |
for (int i = 1; i <= 16; i++) { // submitting more jobs than the thread pool size | |
JobParameters jobParameters = new JobParametersBuilder() | |
.addString("name", "foo" + i) | |
.toJobParameters(); | |
jobLauncher.run(job, jobParameters); | |
} | |
ThreadPoolTaskExecutor taskExecutor = context.getBean(ThreadPoolTaskExecutor.class); | |
taskExecutor.shutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: