์ง๋ ์๊ฐ์๋ JobLauncher์ ๋ํด ์ดํด๋ณด์์ต๋๋ค.
JobLauncher๋ ์ค์ Spring Batch Job์ ์คํํ๋ ์ญํ ์ ํ๋ฉฐ, Job & JobParameters๋ฅผ ์ธ์๋ก ๋ฐ์ ๋ฐฐ์น ์์ ์ ์ํํ ํ JobExecution์ ๋ฐํํฉ๋๋ค.
Spring Batch์์๋ BatchAutoConfiguration ํด๋์ค ๋ด์ JobLauncherApplicationRunner ํด๋์ค๊ฐ Job์ ์คํํ๊ฒ ๋ฉ๋๋ค.
๐ JobParametersValidator
๊ธฐ๋ณธ ๊ฐ๋
- Job ์คํ ์ ํ์์ ์ธ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ฒ์ฆํ๋ ์ญํ ์ ๋๋ค.
- ๊ธฐ๋ณธ์ ์ผ๋ก DefaultJobParametersValidator ๊ตฌํ์ฒด๋ฅผ ์ง์ํ๋ฉฐ, JobParametersValidator ์ธํฐํ์ด์ค๋ฅผ ์ง์ ๊ตฌํํ์ฌ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ฒ์ฆํ ์๋ ์์ต๋๋ค.
๐ ์์ ์ฝ๋ 1. JobParametersValidator ์ธํฐํ์ด์ค ์ง์ ๊ตฌํ
์ฒซ ๋ฒ์งธ๋ก JobParametersValidator ์ธํฐํ์ด์ค๋ฅผ ์ง์ ๊ตฌํํ์ฌ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์ดํด๋ณด๊ฒ ์ต๋๋ค.
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.JobParametersValidator;
public class CustomJobParametersValidator implements JobParametersValidator {
@Override
public void validate(JobParameters jobParameters) throws JobParametersInvalidException {
if (jobParameters.getString("name") == null) {
throw new JobParametersInvalidException("name parameter is not found.");
}
}
}
์ ์์ ๋ JobParameters์ key๊ฐ name์ธ ํ๋ผ๋ฏธํฐ๊ฐ ์กด์ฌํ์ง ์์ผ๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋ ์ฝ๋์ ๋๋ค.
์ค์ ์ ์ฉ์ ์๋์ ๊ฐ์ด Job์์ validator() ๋ฉ์๋๋ฅผ ํตํด ์์์ ์์ฑํ CustomJobParametersValidator ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ฉด ๋ฉ๋๋ค.
@RequiredArgsConstructor
@Configuration
public class ValidatorConfiguration {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
public Job parametersValidatorJob() {
return this.jobBuilderFactory.get("parametersValidatorJob")
.validator(new CustomJobParametersValidator()) // Validator ์ค์
.start(parametersValidatorStep1())
.next(parametersValidatorStep2())
.next(parametersValidatorStep3())
.build();
}
...
}
Spring Batch์์ JobParameters์ ๋ํ ๊ฒ์ฆ์ ์ด 2๋ฒ์ด ์ด๋ฃจ์ด์ง๋๋ค.
- JobLauncher ์ธํฐํ์ด์ค์ ๊ตฌํ์ฒด์ธ SimpleJobLauncher์์ JobParameters ๊ฒ์ฆ
- Job ์ธํฐํ์ด์ค์ ๊ตฌํ์ฒด์ธ AbstractJob ์ถ์ ํด๋์ค์์ JobParameters ๊ฒ์ฆ
์ปค์คํ Validator๋ฅผ ์ค์ ํ๊ณ ์คํํด๋ณด๋ฉด ์์ ๊ฐ์ด AbstractJob ์ถ์ ํด๋์ค์์ setter๋ฅผ ํตํด validator๋ฅผ ์ค์ ํฉ๋๋ค.
๊ทธ ํ ์ฒซ ๋ฒ์งธ ๊ฒ์ฆ์ธ SimpleJobLauncher ํด๋์ค์์ validate() ๋ฉ์๋๋ฅผ ํตํด ๊ฒ์ฆํฉ๋๋ค.
๋ค์์ผ๋ก AbstractJob ์ถ์ ํด๋์ค์ validate() ๋ฉ์๋์์๋ ๊ฒ์ฆ์ด ๋์ด์ผํ๋, ์ ์์๋ ์์ธ๋ฅผ ๋ฐ์ํ์ฌ ์ดํ ๋ก์ง์ ์งํ์ด ๋์ง ์์ต๋๋ค.
์์ ์ฝ๋์์๋ ํ๋ผ๋ฏธํฐ์ requestDate๋ผ๋ key๋ฅผ ์ค์ ํ๊ธฐ ๋๋ฌธ์ name์ ํด๋นํ๋ key๋ ์กด์ฌํ์ง ์์ null๋ก ๋์ค๊ณ , ์ปค์คํ Validator ํด๋์ค์์ ์ค์ ํ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ฉฐ ์์ธ๊ฐ ๋ฐ์ํฉ๋๋ค.
๋ง์ฝ ์์ฒ๋ผ JobParameters์ key๋ฅผ name์ผ๋ก ์ค์ ํ์ฌ ๋ค์ ์คํํด๋ณด๋ฉด ๊ฒ์ฆ์ ํต๊ณผํ์ฌ ์ ์์ ์ผ๋ก ์คํ์ด ๋ฉ๋๋ค.
๋ ๋ฒ์งธ ๊ฒ์ฆ์ธ AbstractJob ์ถ์ ํด๋์ค์์๋ ์ ์์ ์ผ๋ก validate() ๋ฉ์๋๋ฅผ ํต๊ณผํ์ฌ Batch Job์ด ์คํ๋ฉ๋๋ค.
๐ ์์ ์ฝ๋ 2. DefaultJobParametersValidator ๊ตฌํ์ฒด ์ฌ์ฉ
๋ค์์ผ๋ก Spring Batch์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํ๋ JobParametersValidator ์ธํฐํ์ด์ค์ ๊ธฐ๋ณธ ๊ตฌํ์ฒด์ธ DefaultJobParametersValidator ํด๋์ค์ ๋ํด ์ดํด๋ณด๊ฒ ์ต๋๋ค.
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | /* * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.batch.core.job; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.Set; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersInvalidException; import org.springframework.batch.core.JobParametersValidator; import org.springframework.beans.factory.InitializingBean; import org.springframework.lang.Nullable; import org.springframework.util.Assert; /** * Default implementation of {@link JobParametersValidator}. * * @author Dave Syer * @author Mahmoud Ben Hassine * */ public class DefaultJobParametersValidator implements JobParametersValidator, InitializingBean { private Collection<String> requiredKeys; private Collection<String> optionalKeys; /** * Convenient default constructor for unconstrained validation. */ public DefaultJobParametersValidator() { this(new String[0], new String[0]); } /** * Create a new validator with the required and optional job parameter keys * provided. * * @see DefaultJobParametersValidator#setOptionalKeys(String[]) * @see DefaultJobParametersValidator#setRequiredKeys(String[]) * * @param requiredKeys the required keys * @param optionalKeys the optional keys */ public DefaultJobParametersValidator(String[] requiredKeys, String[] optionalKeys) { super(); setRequiredKeys(requiredKeys); setOptionalKeys(optionalKeys); } /** * Check that there are no overlaps between required and optional keys. * @throws IllegalStateException if there is an overlap */ @Override public void afterPropertiesSet() throws IllegalStateException { for (String key : requiredKeys) { Assert.state(!optionalKeys.contains(key), "Optional keys cannot be required: " + key); } } /** * Check the parameters meet the specification provided. If optional keys * are explicitly specified then all keys must be in that list, or in the * required list. Otherwise all keys that are specified as required must be * present. * * @see JobParametersValidator#validate(JobParameters) * * @throws JobParametersInvalidException if the parameters are not valid */ @Override public void validate(@Nullable JobParameters parameters) throws JobParametersInvalidException { if (parameters == null) { throw new JobParametersInvalidException("The JobParameters can not be null"); } Set<String> keys = parameters.getParameters().keySet(); // If there are explicit optional keys then all keys must be in that // group, or in the required group. if (!optionalKeys.isEmpty()) { Collection<String> missingKeys = new HashSet<>(); for (String key : keys) { if (!optionalKeys.contains(key) && !requiredKeys.contains(key)) { missingKeys.add(key); } } if (!missingKeys.isEmpty()) { throw new JobParametersInvalidException( "The JobParameters contains keys that are not explicitly optional or required: " + missingKeys); } } Collection<String> missingKeys = new HashSet<>(); for (String key : requiredKeys) { if (!keys.contains(key)) { missingKeys.add(key); } } if (!missingKeys.isEmpty()) { throw new JobParametersInvalidException("The JobParameters do not contain required keys: " + missingKeys); } } /** * The keys that are required in the parameters. The default is empty, * meaning that all parameters are optional, unless optional keys are * explicitly specified. * * @param requiredKeys the required key values * * @see #setOptionalKeys(String[]) */ public final void setRequiredKeys(String[] requiredKeys) { this.requiredKeys = new HashSet<>(Arrays.asList(requiredKeys)); } /** * The keys that are optional in the parameters. If any keys are explicitly * optional, then to be valid all other keys must be explicitly required. * The default is empty, meaning that all parameters that are not required * are optional. * * @param optionalKeys the optional key values * * @see #setRequiredKeys(String[]) */ public final void setOptionalKeys(String[] optionalKeys) { this.optionalKeys = new HashSet<>(Arrays.asList(optionalKeys)); } } | cs |
DefaultJobParametersValidator ํด๋์ค์๋ requiredKeys, optionalKeys ๋ ๊ฐ์ ์ปฌ๋ ์ ํ๋๊ฐ ์กด์ฌํฉ๋๋ค.
๋ณ์๋ช ์์ ์ ์ ์๋ฏ์ด ๊ฐ๊ฐ ํ์ key, ์ต์ ๋ key๋ฅผ ์๋ฏธํ๋๋ฐ์ requiredKeys๋ ๋จ์ด ๊ทธ๋๋ก ํ๋ผ๋ฏธํฐ์ ๋ฐ๋์ ์กด์ฌํด์ผ ํ๋ key๋ฅผ ์๋ฏธํ๋ฉฐ optionalKeys๋ ํ๋ผ๋ฏธํฐ์ ์กด์ฌํ์ง ์์๋ ์๊ด์ด ์์ต๋๋ค.
@RequiredArgsConstructor
@Configuration
public class ValidatorConfiguration {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
public Job parametersValidatorJob() {
String[] requiredKeys = {"name", "date"};
String[] optionalKeys = {"year"};
return this.jobBuilderFactory.get("parametersValidatorJob")
.validator(new DefaultJobParametersValidator(requiredKeys, optionalKeys))
.start(parametersValidatorStep1())
.next(parametersValidatorStep2())
.next(parametersValidatorStep3())
.build();
}
์ ์ฝ๋์์ ํ์ ํค๋ name, date ์ด๋ฏ๋ก ๋ key๊ฐ JobParameters์ ๋ฐ๋์ ํฌํจ์ด ๋์ด์ผ ํฉ๋๋ค.
- name, date = ํ์
- year = ์ ํ
๋ง์ฝ ์์ฒ๋ผ ํ๋ผ๋ฏธํฐ์ ํ์ ํค์ธ date๊ฐ ์กด์ฌํ์ง ์๋๋ค๋ฉด, Job์ด ์ ์์ ์ผ๋ก ์ํ๋์ง ์๊ณ ์คํจํ ๊ฒ์ด๋ผ๊ณ ์์ํ ์ ์์ต๋๋ค.
DefaultJobParametersValidator ํด๋์ค์ validate() ๋ฉ์๋์์ ์ฒซ for๋ฌธ ๋ด์์์ ๊ฒ์ฆ์ ์ ๋ ฅ๋ฐ์ JobParameters์ ๋ํด ๊ฒ์ฆํ์ฌ optionalKeys, requiredKeys ๋ ์ปฌ๋ ์ ์ ๋ชจ๋ ์กด์ฌํ์ง ์๋๋ค๋ฉด ์์ธ๊ฐ ๋ฐ์ํฉ๋๋ค.
๊ทธ ํ ๋ ๋ฒ์งธ for๋ฌธ์์ requiredKeys์ ํด๋นํ๋ date ํค๊ฐ ํ๋ผ๋ฏธํฐ์ ์กด์ฌํ์ง ์๊ธฐ ๋๋ฌธ์ misskingKeys์ ์กด์ฌํ์ง ์๋ key๋ฅผ ์ถ๊ฐํ๊ณ , ์ดํ if๋ฌธ์์ ์์ธ๊ฐ ๋ฐ์ํฉ๋๋ค.
required Keys์์ date ํค๊ฐ ์กด์ฌํ์ง ์๊ธฐ ๋๋ฌธ์ ์์ธ๊ฐ ๋ฐ์ํฉ๋๋ค.
๋ง์ฝ requiredKeys์ ํด๋นํ๋ name, date๋ฅผ ๋ชจ๋ ํ๋ผ๋ฏธํฐ๋ก ์ค์ ํ๋ค๋ฉด ์ ์์ ์ผ๋ก ๊ฒ์ฆ์ ์ฑ๊ณตํ์ฌ Job์ด ์คํ์ด ๋ฉ๋๋ค.
์ฐธ๊ณ ๋ฌธ์
'Spring > Spring Batch' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring Batch (11) Repeat (0) | 2023.01.15 |
---|---|
Spring Batch (10) @JobScope, @StepScope (0) | 2022.12.08 |
Spring Batch (8) JobLauncher (0) | 2022.11.11 |
Spring Batch (7) JobRepository (0) | 2022.11.08 |
Spring Batch (6) ExecutionContext (0) | 2022.11.05 |
๋๊ธ