Skip to content

Commit 345f4a1

Browse files
refactoring
1 parent c792c1d commit 345f4a1

File tree

5 files changed

+48
-65
lines changed

5 files changed

+48
-65
lines changed

dbAndCsvBatch/src/main/java/com/example/batch/job/CsvToDbJob.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,22 @@
1818
@Configuration
1919
public class CsvToDbJob {
2020

21+
private final JobRepository jobRepository;
22+
private final PlatformTransactionManager transactionManager;
2123
private final CsvToDbLogic logic;
2224
private final BatchNotificationListener listener;
2325

2426
private static final String BATCH_NAME = "CSV_TO_DB";
2527

2628
@Bean
27-
public Job createCsvToDbJob(
28-
JobRepository jobRepository, PlatformTransactionManager transactionManager) {
29-
log.info("Registering job: {}", BATCH_NAME);
30-
31-
Job myJob =
32-
new JobBuilder(BATCH_NAME, jobRepository)
33-
.incrementer(new RunIdIncrementer())
34-
.listener(listener)
35-
.start(
36-
new StepBuilder(BATCH_NAME + "-step", jobRepository)
37-
.tasklet(logic, transactionManager)
38-
.build())
39-
.build();
40-
41-
log.info("Job registered successfully: {} ", myJob.getName());
42-
43-
return myJob;
29+
public Job createCsvToDbJob() {
30+
return new JobBuilder(BATCH_NAME, jobRepository)
31+
.incrementer(new RunIdIncrementer())
32+
.listener(listener)
33+
.start(
34+
new StepBuilder(BATCH_NAME + "-step", jobRepository)
35+
.tasklet(logic, transactionManager)
36+
.build())
37+
.build();
4438
}
4539
}

dbAndCsvBatch/src/main/java/com/example/batch/job/DbToCsvJob.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,23 @@
1818
@Configuration
1919
public class DbToCsvJob {
2020

21+
private final JobRepository jobRepository;
22+
private final PlatformTransactionManager transactionManager;
2123
private final DbToCsvLogic logic;
2224
private final BatchNotificationListener listener;
2325

2426
private static final String BATCH_NAME = "DB_TO_CSV";
2527

2628
@Bean
27-
public Job createDbToCsvJob(
28-
JobRepository jobRepository, PlatformTransactionManager transactionManager) {
29-
log.info("Registering job: {}", BATCH_NAME);
30-
31-
Job myJob =
32-
new JobBuilder(BATCH_NAME, jobRepository)
33-
.incrementer(new RunIdIncrementer())
34-
.listener(listener)
35-
.start(
36-
new StepBuilder(BATCH_NAME + "-step", jobRepository)
37-
.tasklet(logic, transactionManager)
38-
.build())
39-
.build();
40-
41-
log.info("Job registered successfully: {} ", myJob.getName());
42-
43-
return myJob;
29+
public Job createDbToCsvJob() {
30+
31+
return new JobBuilder(BATCH_NAME, jobRepository)
32+
.incrementer(new RunIdIncrementer())
33+
.listener(listener)
34+
.start(
35+
new StepBuilder(BATCH_NAME + "-step", jobRepository)
36+
.tasklet(logic, transactionManager)
37+
.build())
38+
.build();
4439
}
4540
}

skeletonBatch/src/main/java/com/example/batch/job/SampleJob.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@
99
import org.springframework.batch.core.repository.JobRepository;
1010
import org.springframework.batch.core.step.builder.StepBuilder;
1111
import org.springframework.context.annotation.Bean;
12-
import org.springframework.stereotype.Component;
12+
import org.springframework.context.annotation.Configuration;
1313
import org.springframework.transaction.PlatformTransactionManager;
1414

1515
@Slf4j
1616
@RequiredArgsConstructor
17-
@Component
17+
@Configuration
1818
public class SampleJob {
1919

20+
private final JobRepository jobRepository;
21+
private final PlatformTransactionManager transactionManager;
2022
private final SampleLogic logic;
2123

22-
private static final String BATCH_NAME = "sample";
23-
2424
@Bean
25-
public Job createSampleJob(
26-
JobRepository jobRepository, PlatformTransactionManager transactionManager) {
27-
log.info("----------- Registering job: {} -----------", BATCH_NAME);
25+
public Job createSampleJob() {
2826

27+
// Create a step for processing
2928
Step myStep =
30-
new StepBuilder(BATCH_NAME + "-step", jobRepository)
29+
new StepBuilder("processing-step", jobRepository)
3130
.tasklet(logic, transactionManager)
3231
.build();
3332

34-
Job myJob = new JobBuilder(BATCH_NAME + "-job", jobRepository).start(myStep).build();
35-
36-
log.info("----------- Job registered successfully: {} -----------", myJob.getName());
37-
return myJob;
33+
// Build and return the job
34+
return new JobBuilder("sample-job", jobRepository)
35+
.start(myStep)
36+
.build();
3837
}
3938
}

skeletonBatch/src/main/java/com/example/batch/logic/SampleLogic.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ public class SampleLogic implements Tasklet {
1616
private final SampleService sampleService;
1717

1818
@Override
19-
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
20-
throws Exception {
19+
public RepeatStatus execute(StepContribution contribution,
20+
ChunkContext chunkContext) throws Exception {
2121

22-
log.info("----------- START ----------- SampleLogic ----------- START -----------");
2322
try {
24-
sampleService.execute();
23+
// Execute business logic
24+
sampleService.process();
25+
log.info("Processing completed successfully");
2526
} catch (Exception e) {
26-
log.error("Job failure", e);
27+
log.error("Error occurred during processing", e);
2728
throw e;
2829
}
29-
log.info("----------- END ----------- SampleLogic ----------- END -----------");
3030

3131
return RepeatStatus.FINISHED;
3232
}

skeletonBatch/src/main/java/com/example/batch/service/SampleService.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,15 @@
99
@Service
1010
public class SampleService {
1111

12-
public void execute() throws Exception {
13-
log.info("----------- START ----------- SampleService ----------- START -----------");
12+
public void process() throws Exception {
13+
log.info("--- Starting batch process ---");
1414

15-
log.info(
16-
System.lineSeparator().repeat(3)
17-
+ "***************************************************************************************"
18-
+ System.lineSeparator()
19-
+ "*** ここにバッチの業務ロジックを記述する !!! ***"
20-
+ System.lineSeparator()
21-
+ "*** Write your batch business logic here! ***"
22-
+ System.lineSeparator()
23-
+ "***************************************************************************************"
24-
+ System.lineSeparator().repeat(3));
15+
// Add your business logic here
16+
// Example:
17+
// - Data validation
18+
// - Data transformation
19+
// - External system integration
2520

26-
log.info("----------- END ----------- SampleService ----------- END -----------");
21+
log.info("--- Batch process completed ---");
2722
}
2823
}

0 commit comments

Comments
 (0)