* SecondBatch 작성 완료 close #1
This commit is contained in:
@@ -11,11 +11,13 @@ import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.item.data.RepositoryItemReader;
|
||||
import org.springframework.batch.item.data.RepositoryItemWriter;
|
||||
import org.springframework.batch.item.data.builder.RepositoryItemReaderBuilder;
|
||||
import org.springframework.batch.item.data.builder.RepositoryItemWriterBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
@@ -44,6 +46,9 @@ public class SecondBatch {
|
||||
|
||||
return new StepBuilder("secondStep", jobRepository)
|
||||
.<WinEntity, WinEntity> chunk(10, platformTransactionManager)
|
||||
.reader(winReader())
|
||||
.processor(trueProcessor())
|
||||
.writer(winWriter())
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -54,6 +59,7 @@ public class SecondBatch {
|
||||
.name("winReader")
|
||||
.pageSize(10)
|
||||
.methodName("findByWinGreaterThanEqual")
|
||||
.arguments(Collections.singletonList(10L))
|
||||
.repository(winRepository)
|
||||
.sorts(Map.of("id", Sort.Direction.ASC))
|
||||
.build();
|
||||
@@ -62,12 +68,18 @@ public class SecondBatch {
|
||||
@Bean
|
||||
public ItemProcessor<WinEntity, WinEntity> trueProcessor() {
|
||||
|
||||
|
||||
return item -> {
|
||||
item.setReward(true);
|
||||
return item;
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RepositoryItemWriter<WinEntity> winWriter() {
|
||||
|
||||
|
||||
return new RepositoryItemWriterBuilder<WinEntity>()
|
||||
.repository(winRepository)
|
||||
.methodName("save")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,5 +37,17 @@ public class MainController {
|
||||
return "ok";
|
||||
}
|
||||
|
||||
@GetMapping("/second")
|
||||
private String secondApi(@RequestParam("value") String value) throws Exception {
|
||||
|
||||
JobParameters jobParameters = new JobParametersBuilder()
|
||||
.addString("date", value)
|
||||
.toJobParameters();
|
||||
|
||||
jobLauncher.run(jobRegistry.getJob("secondJob"), jobParameters);
|
||||
|
||||
return "ok";
|
||||
}
|
||||
|
||||
//https://docs.spring.io/spring-batch/reference/job/configuring-launcher.html
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.example.springbatch.repository;
|
||||
|
||||
import com.example.springbatch.entity.WinEntity;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface WinRepository extends JpaRepository<WinEntity, Long> {
|
||||
|
||||
Optional<WinEntity> findByWinGreaterThanEqual(Long win);
|
||||
Page<WinEntity> findByWinGreaterThanEqual(Long win, Pageable pageable);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user