diff --git a/src/main/java/com/wb/domainserver/acikey/AciKeyTokenJobConfig.java b/src/main/java/com/wb/domainserver/acikey/AciKeyTokenJobConfig.java new file mode 100644 index 0000000..e7e2da3 --- /dev/null +++ b/src/main/java/com/wb/domainserver/acikey/AciKeyTokenJobConfig.java @@ -0,0 +1,35 @@ +package com.wb.domainserver.acikey; + +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.job.builder.JobBuilder; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.step.builder.StepBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.PlatformTransactionManager; +import lombok.RequiredArgsConstructor; + +// ACIKey 토큰 발급 테스트 배치 - Job/Step 설정 +@Configuration +@RequiredArgsConstructor +public class AciKeyTokenJobConfig { + + private final JobRepository jobRepository; + private final PlatformTransactionManager transactionManager; + private final AciKeyTokenTasklet aciKeyTokenTasklet; + + @Bean + public Job aciKeyTokenJob() { + return new JobBuilder("aciKeyTokenJob", jobRepository) + .start(aciKeyTokenStep()) + .build(); + } + + @Bean + public Step aciKeyTokenStep() { + return new StepBuilder("aciKeyTokenStep", jobRepository) + .tasklet(aciKeyTokenTasklet, transactionManager) + .build(); + } +} diff --git a/src/main/java/com/wb/domainserver/acikey/AciKeyTokenTasklet.java b/src/main/java/com/wb/domainserver/acikey/AciKeyTokenTasklet.java new file mode 100644 index 0000000..7a773ed --- /dev/null +++ b/src/main/java/com/wb/domainserver/acikey/AciKeyTokenTasklet.java @@ -0,0 +1,79 @@ +package com.wb.domainserver.acikey; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.repeat.RepeatStatus; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.*; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +import java.util.Map; + +// ACIKey 토큰 발급 테스트 배치 - API 호출 및 토큰 확인 로직 +@Slf4j +@Component +public class AciKeyTokenTasklet implements Tasklet { + + @Value("${acikey.url}") + private String aciKeyUrl; + + @Value("${acikey.userid}") + private String userId; + + @Value("${acikey.apikey}") + private String apiKey; + + @Override + public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) { + + log.info("[AciKeyBatch] 배치 시작"); + log.info("[AciKeyBatch] API URL: {}", aciKeyUrl); + + try { + // Header 설정 + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.set("USERID", userId); + headers.set("APIKEY", apiKey); + + HttpEntity requestEntity = new HttpEntity<>("{}", headers); + + // API 호출 + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity response = restTemplate.exchange( + aciKeyUrl, + HttpMethod.POST, + requestEntity, + Map.class + ); + + Map body = response.getBody(); + log.info("[AciKeyBatch] 응답 전체: {}", body); + + String status = (String) body.get("Status"); + String aciKey = (String) body.get("ACIKey"); + String errMsg = (String) body.get("ErrMsg"); + + if ("A10".equals(status)) { + log.info("[AciKeyBatch] ✅ 토큰 발급 성공!"); + log.info("[AciKeyBatch] ACIKey: {}", aciKey); + contribution.incrementWriteCount(1); + } else { + log.error("[AciKeyBatch] ❌ 토큰 발급 실패. Status: {}, ErrMsg: {}", status, errMsg); + throw new RuntimeException("ACIKey 토큰 발급 실패. Status: " + status + ", ErrMsg: " + errMsg); + } + + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + log.error("[AciKeyBatch] ❌ API 호출 중 오류 발생: {}", e.getMessage(), e); + throw new RuntimeException("AciKey API 호출 오류", e); + } + + log.info("[AciKeyBatch] 배치 종료"); + return RepeatStatus.FINISHED; + } +} diff --git a/src/main/resources/application-dev.yaml b/src/main/resources/application-dev.yaml index 74f42b5..8b5558b 100644 --- a/src/main/resources/application-dev.yaml +++ b/src/main/resources/application-dev.yaml @@ -30,3 +30,8 @@ secret: mail: host: 10.2.1.76 port: 25 + +acikey: + url: https://acieshop.com/api/kr/token/ + userid: UNIHAN + apikey: DC94F29AFD5399F54AAE7FAA46045B65