ItemProcessor
ItemPorcessor๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ๊ณตํ๊ฑฐ๋ ํํฐ๋งํ๋ ์ญํ ์ ํ๋ฉฐ, ํ์๊ฐ ์๋๋ค. ์ด ์ญํ ์ ItemWriter์์๋ ๊ตฌํ์ด ๊ฐ๋ฅํ์ง๋ง, ๋ถ๋ฆฌํจ์ผ๋ก์จ ๋น์ฆ๋์ค ์ฝ๋๊ฐ ์์ด๋ ๊ฒ์ ๋ฐฉ์งํ ์ ์๋ค.
public interface ItemProcessor<I, O> {
@Nullable
O process(@NonNull I var1) throws Exception;
}I๋ ItemReader์์ ๋ฐ์ ๋ฐ์ดํฐ ํ์
์ด๋ฉฐ, O๋ ItemWriter์ ๋ณด๋ผ ๋ฐ์ดํฐ ํ์
์ด๋ค. ์ฆ, Reader์์ ์ฝ์ ๋ฐ์ดํฐ๊ฐ ItemProcessor์ process()๋ฅผ ํต๊ณผํ ํ Writer์ ์ ๋ฌ๋๋ค. ๊ตฌํํด์ผํ ๋ฉ์๋๋ processํ๋์ด๋ฉฐ, Java 8๋ถํฐ๋ ์ธํฐํ์ด์ค์ ์ถ์ ๋ฉ์๋๊ฐ 1๊ฐ์ธ ๊ฒฝ์ฐ ๋๋ค์์ ์ฌ์ฉํ ์ ์๋ค.
@Bean(BEAN_PREFIX + "processor")
@StepScope
public ItemProcessor<ReadType, WriteType> processor() {
return item -> {
item.convert();
return item;
};
}๋ถํ์ํ ์ฝ๋๊ฐ ์์ด ๊ตฌํ ์ฝ๋ ์์ด ์ ๋ค. (๋น ๋ฅด๊ฒ ๊ตฌํ ๊ฐ๋ฅ)
๊ณ ์ ๋ ํํ๊ฐ ์์ด ์ํ๋ ํํ์ ์ด๋ค ์ฒ๋ฆฌ๋ ๊ฐ๋ฅํ๋ค.
Batch Config ํด๋์ค ์์ ํฌํจ๋์ด ์์ด์ผ๋ง ํ๋ฉฐ, Batch Config ์ฝ๋ ์์ด ๋ง์์ง ์ ์๋ค.
์ฝ๋ ์์ด ๋ง์์ง๋ง ๋ณ๋ ํด๋์ค๋ก Processor๋ฅผ ๋ถ๋ฆฌํด์ ์ฌ์ฉํ๊ธฐ๋ ํ๋ค.
ํฌ๊ฒ ItemProcessor๋ ๋ค์ ์ญํ ์ ํ๋ค.
๋ณํ : Reader์์ ์ฝ์ ๋ฐ์ดํฐ๋ฅผ ์ํ๋ ํ์ ์ผ๋ก ๋ณํํ์ฌ, Writer์ ๋๊ฒจ์ค ์ ์๋ค.
ํํฐ : Reader์์ ๋๊ฒจ์ค ๋ฐ์ดํฐ๋ฅผ Writer๋ก ๋๊ฒจ์ค ๊ฒ์ธ์ง ๊ฒฐ์ ํ ์ ์์ผ๋ฉฐ,
null์ ๋ฐํํ๋ฉด Writer์ ์ ๋ฌ๋์ง ์๋๋ค.
ItemProcessor๊ฐ null์ ๋ฐํํ๋ฉด ํด๋น Item์ ๋ชจ๋ ์ดํ ์ฒ๋ฆฌ๊ฐ ์ค์ง๋๋ค. ์ด๋ null์ ๋ฐํํ๋๋ผ๋ ๋ค๋ฅธ Item ์ฒ๋ฆฌ๊ฐ ๊ณ์ ์ด๋ฃจ์ด์ง๋ค.
๊ตฌํ์ฒด
Spring Batch ์์๋ ์์ฃผ ์ฌ์ฉํ๋ ์ฉ๋์ Processor๋ฅผ ๋ฏธ๋ฆฌ ํด๋์ค๋ก ๋ง๋ค์ด์ ์ ๊ณตํด์ฃผ๊ณ ์๋ค.
ItemProcessorAdapter
ValidatingItemProcessor
CompositeItemProcessor
ํ์ง๋ง ์ต๊ทผ์๋ ๋๋ถ๋ถ processor๋ฅผ ์ง์ ๊ตฌํํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๊ณ , ๋๋ค์์ผ๋ก ๋น ๋ฅด๊ฒ ๊ตฌํํ ๋๋ ๋ง๋ค. ๊ทธ๋์ ItemProcessorAdapter์ ValidatingItemProcessor๋ ๊ฑฐ์ ์ฌ์ฉํ์ง ์๋๋ค.
์
๋ ฅ ๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฆ์ ์ฌ์ฉํ๋ ItemProcessor ๊ตฌํ์ฒด์ด๋ค. ์
๋ ฅ ์์ดํ
์ ์ ํจ์ฑ ๊ฒ์ฆ์ ์ํํ๋ ์คํ๋ง๋ฐฐ์น Validator๋ฅผ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ์ ํจ์ฑ ๊ฒ์ฆ์ด ์คํจํ๋ฉด, ValidationException์ด ๋ฐ์ํ๋ค.
org.springframework.batch.item.validator.ValidatingItemProcessor
BeanValidatingItemProcessor
JSR 303์ ๋น ์ ํจ์ฑ ๊ฒ์ฆ์ ์ํ ๊ฒ์ผ๋ก, ์คํ๋ง ๋ฐฐ์น๋ ๋ฏธ๋ฆฌ ์ ์๋ ์ ํจ์ฑ ๊ฒ์ฆ ๊ธฐ๋ฅ์ ์ด๋ ธํ ์ด์ ์ผ๋ก ์ ๊ณตํด์ค๋ค. ํด๋น ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ๋ ค๋ฉด, ๋ค์ ์์กด์ฑ์ ์ถ๊ฐํด์ค์ผํ๋ค.
@NotNull @Null
๊ฐ์ด null์ธ์ง ์๋์ง ๊ฒ์ฌ
@Size
int min : ์ต์ ํฌ๊ธฐ(default : 0) int max : ์ต๋ํฌ๊ธฐ
๊ธธ์ด๋ ํฌ๊ธฐ๊ฐ ์ง์ ํ ๊ฐ ๋ฒ์์ ์๋์ง ๊ฒ์ฌ null์ ์ ํจํ๋ค๊ณ ํ๋จ
@Pattern
String regexp = ์ ๊ทํํ์
๊ฐ์ด ์ ๊ท ํํ์์ ์ผ์นํ๋์ง ๊ฒ์ฌ null์ ์ ํจํ๋ค๊ณ ํ๋จ
@AssertTrue @AssertFalse
๊ฐ์ด true์ธ์ง false์ธ์ง ๊ฒ์ฌ null์ ์ ํจํ๋ค ํ๋จ
@DecmialMax @DecimalMin
String value: ์ต๋๊ฐ ๋๋ ์ต์๊ฐ boolean inclusive : ์ง์ ๊ฐ ํฌํจ ์ฌ๋ถ(default : true)
์ง์ ํ ๊ฐ๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์์ง ํน์ ํฌ๊ฑฐ๋ ๊ฐ์์ง ๊ฒ์ฌ null์ ์ ํจํ๋ค ํ๋จ
@Max @Min
long value
์ง์ ํ ๊ฐ๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์์ง ํน์ ํฌ๊ฑฐ๋ ๊ฐ์์ง ๊ฒ์ฌ null์ ์ ํจํ๋ค ํ๋จ
@Digits
int integer : ํ์ฉ๊ฐ๋ฅํ ์ ์ ์๋ฆฟ์ int fraction : ํ์ฉ ๊ฐ๋ฅํ ์์์ ์ดํ ์๋ฆฟ์
์๋ฆฟ์๊ฐ ์ง์ ํ ํฌ๊ธฐ๋ฅผ ๋์ง ์๋์ง ๊ฒ์ฌ null์ ์ ํจํ๋ค ํ๋จ
@NotEmpty
๋ฌธ์์ด, ๋ฐฐ์ด : null์ด ์๋๊ณ , ๊ธธ์ด๊ฐ 0์ด ์๋์ง ๊ฒ์ฌ Collection : null์ด ์๋๊ณ , ํฌ๊ธฐ๊ฐ 0์ด ์๋์ง ๊ฒ์ฌ
@NotBlank
null์ด ์๋๊ณ ์ต์ํ ํ๊ฐ ์ด์์ ๊ณต๋ฐฑ์ด ์๋ ๋ฌธ์๋ฅผ ํฌํจํ๋์ง ๊ฒ์ฌ
@Positive @PositiveOrZero
์์์ธ์ง ๊ฒ์ฌ OrZero๋ ์์ ํน์ 0์ธ์ง ๊ฒ์ฌ null์ ์ ํจํ๋ค ํ๋จ
@Negative @NegativeOrZero
์์์ธ์ง ๊ฒ์ฌ OrZero๋ ์์ ํน์ 0์ธ์ง ๊ฒ์ฌ null์ ์ ํจํ๋ค ํ๋จ
์ด๋ฉ์ผ ์ฃผ์๊ฐ ์ ํจํ์ง ๊ฒ์ฌ null์ ์ ํจํ๋ค ํ๋จ
@Future @FuterOrPresent
ํด๋น ์๊ฐ์ด ๋ฏธ๋์ธ์ง ๊ฒ์ฌ OrPresent๋ ํ์ฌ ๋๋ ๋ฏธ๋์๊ฐ์ธ์ง ๊ฒ์ฌ null์ ์ ํจํ๋ค ํ๋จ
@Past @PastOrPresent
ํด๋น ์๊ฐ์ด ๊ณผ๊ฑฐ์ธ์ง ๊ฒ์ฌ OrPresent๋ ํ์ฌ ๋๋ ๊ณผ๊ฑฐ์๊ฐ์ธ์ง ๊ฒ์ฌ null์ ์ ํจํ๋ค ํ๋จ
์ ์์ ์ ๊ฐ์ด ๊ณ ์ ํ ๋ฉ์ธ์ง๋ฅผ ์ง์ ํ ์ ์์ผ๋ฉฐ, ํ๋ ๊ฐ์ ๊ธธ์ด๊ฐ ์๋ชป๋๋์ง ํ์์ด ์๋ชป๋๋์ง ์๋ณํ ์ ์๋ค.
๋ค์๊ณผ ๊ฐ์ด ์ง์ ํ validation์ ๋ง์ง ์์ผ๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
ValidatingItemProcessor
๋ฐ์ดํฐ์
๋ด์์ ํ๊ฐ์ ํ๋์ ๊ฐ์ด ๊ณ ์ ํด์ผํ๋ ๊ฒฝ์ฐ๊ฐ ์์ ์ ์๋ค. ๊ณ ์ ํ ๊ฐ์ ํ๋๋ฅผ ItemStream ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ์ฌ, ๊ฐ ์ปค๋ฐ๊ณผ ํ๋ ๊ฐ์ ExecutionContext์ ์ ์ฅํด ์ํ๋ฅผ ์ ์งํ ์ ์๋ค.
Validator๋ฅผ ๊ตฌํํ ํ Step์ ๋ค์๊ณผ ๊ฐ์ด ๊ตฌํํ๋ฉด ๋๋ค.
ItemProcessorAdapter
org.springframework.batch.item.adapter.ItemProcessorAdapter
์๋น์ค๋ฅผ ItemProcessor ์ญํ ์ ํ๋๋ก ๋ง๋ค ์ ์๋ค.
๊ณ ๊ฐ์ ์ด๋ฆ์ ๋๋ฌธ์๋ก ๋ฐ๊ฟ์ฃผ๋ ์๋น์ค์ด๋ค. ์ด ์๋น์ค๋ฅผ ItemProcessorAdapter๋ฅผ ์ฌ์ฉํ์ฌ Processor๋ก ์ฌ์ฉํ ์ ์๋ค.
ScriptItemProcessor
Ruby, JavaScript, Groovy ๋ฑ ๋ค์ํ ์คํฌ๋ฆฝํธ ์ธ์ด๋ฅผ ์คํํ ์ ์๋ค.
org.springframework.batch.item.support.ScriptItemProcessor
์ํํ๊ณ ์ถ์ ์คํฌ๋ฆฝํธ๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ๋ฐ์ธ๋ฉํ์ฌ ์ฌ์ฉํ ์ ์๋ค.
CompositeItemProcessor

CompositeItemProcessor๋ ItemProcessor๊ฐ์ ์ฒด์ด๋์ ์ง์ํ๋ Processor์ด๋ค.
๋ค์๊ณผ ๊ฐ์ด Processor๊ฐ ์ฌ๋ฌ๊ฐ ํ์ํ ๊ฒฝ์ฐ ์ฒด์ด๋ ์์ ์ ํ ์ ์๋ค.
ํ์ง๋ง, ์ฌ๊ธฐ์ ์ ๋ค๋ฆญ ํ์
์ ์ฌ์ฉํ์ง ๋ชปํ๋ฉฐ, ๋ง์ฝ ์ ๋ค๋ฆญํ์
์ ์ฌ์ฉํ๊ฒ ๋๋ฉด delegates์ ํฌํจ๋ ItemProcessor๋ ๋ชจ๋ ๊ฐ์ ์ ๋ค๋ฆญ ํ์
์ ๊ฐ์ ธ์ผํ๋ค. ๋ง์ฝ ๊ฐ์ ์ ๋ค๋ฆญ ํ์
์ ์ฌ์ฉํ ์ ์๋ ItemProcessor๊ฐ ์ฒด์ด๋์ด๋ผ๋ฉด ์ ๋ค๋ฆญ์ ์ ์ธํ๋ ๊ฒ์ด ๋ ์์ ํ ์ฝ๋๊ฐ ๋ ์ ์๋ค.
ClassifierCompositeItemProcessor
org.springframework.batch.item.support.ClassifierCompositeItemProcessor
Classifier ๊ตฌํ์ฒด๋ก ์ฌ์ฉํ ItemProcessor๋ฅผ ์ ์ ํด classify ๋ฉ์๋๋ฅผ ์ํํด ๋ถ๋ฅ ์ฒ๋ฆฌ๋ฅผ ํ ์ ์๋ค.
๋ค์์ ์ฐํธ๋ฒํธ๋ฅผ ์ง์ ํ์๋ก ๋ถ๋ฅํ Classifier ๊ตฌํ์ฒด์ด๋ค.
๊ตฌํํ Classifier๋ฅผ ClassifierCompositeItemProcessor๋ก ๊ตฌํํ์ฌ ์ํํ ์ ์๋ค.
ํ์์ธ ๊ฒฝ์ฐ์๋ upperCase์ถ๋ ฅ, ์ง์์ธ ๊ฒฝ์ฐ lowerCase๋ก ๋ถ๋ฅํด์ ์ถ๋ ฅํ๋๋ก ๊ตฌํํ์์ผ๋ฉฐ, ๋ค์๊ณผ ๊ฐ์ด ์ ์์ ์ผ๋ก ์ถ๋ ฅ๋๋๊ฑธ ํ์ธํ ์ ์๋ค.
ItemProcessor ์ง์ ๊ตฌํํ๊ธฐ
์ปค์คํ
ํ๋ก์ธ์๋ฅผ ์์ฑํ์ฌ ์ง์ ์ฐํธ๋ฒํธ๋ ํํฐ๋งํ๊ณ ํ์ ์ฐํธ๋ฒํธ๋ง ๋จ๊ฒจ๋๋ ItemProcessor ์์ ๋ฅผ ์ดํด๋ณผ ๊ฒ์ด๋ค.
ItemProcessor๋ **null**์ ๋ฐํํ๋ฉด ํด๋น ์์ดํ
์ ๊ทธ ์ดํ ์ํ๋๋ ItemProcessor๋ ItemWriter๋ก ์ ๋ฌ๋์ง ์๊ณ , ํํฐ๋ง๋๋ค. ์ด๋ ๊ฒ ํํฐ๋ง๋ ๋ ์ฝ๋์ ์๋ฅผ JobRepository์ ๊ด๋ฆฌํ๊ณ ์๋ค.
๋ค์๊ณผ ๊ฐ์ด ItemProcessor๋ฅผ ๊ตฌํํ์ฌ, process ๋ฉ์๋์ ์ํ๋ ๋ก์ง์ ์์ฑํด์ฃผ๋ฉด ๋๋ค.
BATCH_STEP_EXECUTION ๋ด์ ์ฑ๊ณตํ ์, ํํฐ๋ง ๊ฑธ๋ฆฐ ์์ดํ
์๋ฅผ ํ์ธํ ์ ์๋ค.

์ฐธ๊ณ
Last updated
Was this helpful?