Tại bài báo này mình sẽ thực hiện tạo một JobRepository.
JobRepository là gì ?
1. Khái niệm.
-
CRUD trong SQL:
Operation SQL Create INSERT Read SELECT Update UPDATE Delete DELETE -
JobRepository
Repository responsible for persistence of batch meta-data entities.
Nhiệm vụ thực hiện lưu trữ và đưa thông tin đôi tượng (object) vào đúng bản cài đặt của Spring Batch.
Table Object BATCH_JOB_INSTANCE JobInstance BATCH_JOB_EXECUTION JobExecution BATCH_JOB_EXECUTION_PARAMS JobParameters BATCH_STEP_EXECUTION StepExecution BATCH_JOB_EXECUTION_CONTEXT ExecutionContext BATCH_STEP_EXECUTION_CONTEXT ExecutionContext
2. Tạo một project demo.
Truy cập vào spring initializr để tạo một project
-
Add các thư viện cần thiết vào project:
-
Click chọn:
3. Cài dặt resource.
-
Tại file application.properties thực hiện config khởi tạo tables.
spring.batch.initialize-schema=always
-
Tạo một class config connect database MySQL
@Configuration public class DataSourceConfiguration { @Bean public DataSource dataSource() { return DataSourceBuilder .create() .username("root") .password("root") .url("jdbc:mysql://localhost:3306/testdb") .build(); } }
-
Tạo config cho jobRepository
@Configuration @EnableBatchProcessing @Import({DataSourceConfiguration.class}) public class BatchConfig implements BatchConfigurer { @Autowired private DataSource dataSource; @Bean public JobRepository jobRepository() throws Exception { return getJobRepository(); } @Bean public PlatformTransactionManager platformTransactionManager() throws Exception { return getTransactionManager(); } @Override public JobRepository getJobRepository() throws Exception { return createJobRepository(); } protected JobRepository createJobRepository() throws Exception { JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); factory.setDataSource(dataSource); factory.setTransactionManager(getTransactionManager()); factory.afterPropertiesSet(); return factory.getObject(); } @Override public PlatformTransactionManager getTransactionManager() throws Exception { return new DataSourceTransactionManager(this.dataSource); } .... }
-
Thực hiện run lần dầu ta được một nhóm tables hệ thống của Spring Batch
4. Class Controller
-
Tiếp theo tạo một class controller gửi một request url để thực hiện tạo một JobExecution
@Controller public class RequestRepositoryExample { @Autowired private JobRepository jobRepository; @Autowired private DefaultBatchConfigurer defaultBatchConfigurer; @RequestMapping(value = "/jobRepository", method = RequestMethod.GET) @ResponseBody public String execute() throws Exception { // Creating params JobParametersBuilder jobParametersBuilder = new JobParametersBuilder(); jobParametersBuilder.addString("firstParamter", "1"); // Register defaultBatchConfigurer.getJobRepository() .createJobExecution("jobName", jobParametersBuilder.toJobParameters()); return "<h1>Batch Success</h1>"; } }
5. Kiểm tra
-
Thực hiện gửi một request "/jobRepository"
-
Kiểm tra Job với tên "jobName" đã được đăng ký được chưa.
-
Đã đang ký params ở của job thành công chưa.
Vậy là đã thực hiện đăng ký thành thành công một Job thông qua class jobRepository.
Có gì không đúng mong mọi người góp ý.