๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring/Spring Batch

Spring Batch (7) JobRepository

by ์ฃผ๋ฐœ2 2022. 11. 8.
๋ฐ˜์‘ํ˜•

๐Ÿ“Ž JobRepository

์ง€๋‚œ ์‹œ๊ฐ„์—๋Š” Spring Batch์˜ ExecutionContext์— ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

ExecutionContext๋Š” Spring Batch ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์ง€์›ํ•˜๋Š” key/value ํ˜•์‹์˜ ๊ณต์œ  ๊ฐ์ฒด๋กœ Job์„ ์‹คํ–‰ํ•˜๋ฉด์„œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค.

์„œ๋กœ ๋‹ค๋ฅธ Job ๊ฐ„์—๋Š” ๊ณต์œ ๊ฐ€ ์•ˆ๋˜์ง€๋งŒ, ๋™์ผํ•œ Job ๋‚ด์˜ ์„œ๋กœ ๋‹ค๋ฅธ Step ์‚ฌ์ด์—๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋”ฐ๋ผ์„œ Batch Job ๋‚ด์˜ Step ์‚ฌ์ด์—์„œ ๋ฐ์ดํ„ฐ ๊ณต์œ  ๋ฐ ์กฐ์ž‘์ด ํ•„์š”ํ•˜๋‹ค๋ฉด ExecutionContext๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” Spring Batch JobRepository์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ธฐ์กด์— Job & JobParameters๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์กฐํšŒํ•œ ํ›„ JobInstance์˜ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ๊ธฐ์กด์— ์กด์žฌํ•˜๋˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๊ณ  ํ–ˆ์—ˆ๋Š”๋ฐ์š”, ์ด๋Ÿฌํ•œ ๊ณผ์ •์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด JobRepository ์ž…๋‹ˆ๋‹ค.

 

๋˜ํ•œ Job, Step ๋“ฑ์˜ ์ž‘์—…์ด ์ˆ˜ํ–‰๋  ๋•Œ Spring Batch์˜ ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ๋ฅผ CRUDํ•˜๋Š” ๊ฒƒ๋„ JobRepository์ž…๋‹ˆ๋‹ค.

 

 

๐Ÿ“Œ ํ•ด๋‹น ํฌ์ŠคํŒ…์˜ ๋ชฉํ‘œ

  • Spring Batch JobRepository ํŒŒ์•…ํ•˜๊ธฐ

 

๐Ÿ“Œ JobRepository

๊ธฐ๋ณธ ๊ฐœ๋…

  • ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๋ฐฐ์น˜ ์ž‘์—… ์ค‘ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ์ €์žฅ์†Œ ์—ญํ• 
  • JobLauncher, Job, Step ๊ตฌํ˜„์ฒด์— ๋Œ€ํ•œ CRUD ์ž‘์—…
  • Job์˜ ์ˆ˜ํ–‰๊ณผ ๊ด€๋ จ๋œ ๋ชจ๋“  meta data๋ฅผ ์ €์žฅ
  • ๊ตฌํ˜„์ฒด๋Š” SimpleJobRepository

 

SimpleJobRepository

SimpleJobRepository์—๋Š” ๊ฐ๊ฐ์˜ Dao ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•˜์—ฌ Job, Step ๋“ฑ์ด ์‹คํ–‰๋  ๋•Œ๋งˆ๋‹ค ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

์œ„ ~Dao ๊ฐ์ฒด์˜ Bean์€ JobRepositoryFactoryBean ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
ใ…‡    @Override
    protected JobInstanceDao createJobInstanceDao() throws Exception {
        JdbcJobInstanceDao dao = new JdbcJobInstanceDao();
        dao.setJdbcTemplate(jdbcOperations);
        dao.setJobIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix + "JOB_SEQ"));
        dao.setTablePrefix(tablePrefix);
        dao.afterPropertiesSet();
        return dao;
    }
 
    @Override
    protected JobExecutionDao createJobExecutionDao() throws Exception {
        JdbcJobExecutionDao dao = new JdbcJobExecutionDao();
        dao.setJdbcTemplate(jdbcOperations);
        dao.setJobExecutionIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix
                + "JOB_EXECUTION_SEQ"));
        dao.setTablePrefix(tablePrefix);
        dao.setClobTypeToUse(determineClobTypeToUse(this.databaseType));
        dao.setExitMessageLength(maxVarCharLength);
        dao.afterPropertiesSet();
        return dao;
    }
 
    @Override
    protected StepExecutionDao createStepExecutionDao() throws Exception {
        JdbcStepExecutionDao dao = new JdbcStepExecutionDao();
        dao.setJdbcTemplate(jdbcOperations);
        dao.setStepExecutionIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix
                + "STEP_EXECUTION_SEQ"));
        dao.setTablePrefix(tablePrefix);
        dao.setClobTypeToUse(determineClobTypeToUse(this.databaseType));
        dao.setExitMessageLength(maxVarCharLength);
        dao.afterPropertiesSet();
        return dao;
    }
 
    @Override
    protected ExecutionContextDao createExecutionContextDao() throws Exception {
        JdbcExecutionContextDao dao = new JdbcExecutionContextDao();
        dao.setJdbcTemplate(jdbcOperations);
        dao.setTablePrefix(tablePrefix);
        dao.setClobTypeToUse(determineClobTypeToUse(this.databaseType));
        dao.setSerializer(serializer);
 
        if (lobHandler != null) {
            dao.setLobHandler(lobHandler);
        }
 
        dao.afterPropertiesSet();
        // Assume the same length.
        dao.setShortContextLength(maxVarCharLength);
        return dao;
    }
cs

 

JobRepository ์„ค์ •

JobRepository ์„ค์ •์€ Spring Batch ์„ค์ • ์–ด๋…ธํ…Œ์ด์…˜์ธ @EnableBatchProcessing๋ฅผ ์„ ์–ธํ•˜๋ฉด ์ž๋™์œผ๋กœ JobRepository Bean์ด ์ƒ์„ฑ์ด ๋ฉ๋‹ˆ๋‹ค. 

@EnableBatchProcessing ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด ์ƒ์„ฑ๋˜๋Š” ๊ณผ์ •์€ Spring Batch ์˜์กด์„ฑ ์„ค์ • ํฌ์ŠคํŒ…์—์„œ ์ •๋ฆฌ๋ฅผ ํ–ˆ์—ˆ๋Š”๋ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • @EnableBatchProcessing -> @Import(BatchConfigurationSelector.class) -> SimpleBatchConfiguration

 

JobRepository ์ปค์Šคํ…€

๊ธฐ์กด Spring Batch๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์„ค์ • ์™ธ ์ปค์Šคํ…€ ํ•˜์—ฌ JobRepository Bean์„ ์ƒ์„ฑํ•  ๊ฒฝ์šฐ BatchConfigurer ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ฑฐ๋‚˜, BasicBatchConfigurer ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ ์ปค์Šคํ…€ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

BatchConfigurer

 

BasicBatchConfigurer

 

 

์ฐธ๊ณ  ๋ฌธ์„œ

 

๋ฐ˜์‘ํ˜•

'Spring > Spring Batch' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Spring Batch (9) JobParametersValidator  (0) 2022.11.13
Spring Batch (8) JobLauncher  (0) 2022.11.11
Spring Batch (6) ExecutionContext  (0) 2022.11.05
Spring Batch (5) Step, StepExecution  (0) 2022.11.03
Spring Batch (4) Job, JobInstance, JobParameters, JobExecution  (0) 2022.11.01

๋Œ“๊ธ€