Skip to content

Instantly share code, notes, and snippets.

@fmbenhassine
Created August 9, 2018 14:54
Show Gist options
  • Save fmbenhassine/fb729bd25c75667c8dc24c9c4d9d6b1d to your computer and use it in GitHub Desktop.
Save fmbenhassine/fb729bd25c75667c8dc24c9c4d9d6b1d to your computer and use it in GitHub Desktop.
/*
* 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();
}
}
@fmbenhassine
Copy link
Author

Output:

taskExecutor-2: working on foo2
taskExecutor-15: working on foo15
taskExecutor-14: working on foo14
taskExecutor-13: working on foo13
taskExecutor-12: working on foo12
taskExecutor-11: working on foo11
taskExecutor-10: working on foo10
taskExecutor-9: working on foo9
taskExecutor-8: working on foo8
taskExecutor-7: working on foo7
taskExecutor-6: working on foo6
taskExecutor-5: working on foo5
taskExecutor-1: working on foo1
taskExecutor-3: working on foo3
taskExecutor-4: working on foo4
taskExecutor-15: work on foo15 done
taskExecutor-14: work on foo14 done
taskExecutor-13: work on foo13 done
taskExecutor-12: work on foo12 done
taskExecutor-2: work on foo2 done
taskExecutor-11: work on foo11 done
taskExecutor-10: work on foo10 done
taskExecutor-9: work on foo9 done
taskExecutor-8: work on foo8 done
taskExecutor-7: work on foo7 done
taskExecutor-6: work on foo6 done
taskExecutor-5: work on foo5 done
taskExecutor-1: work on foo1 done
taskExecutor-3: work on foo3 done
taskExecutor-4: work on foo4 done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment