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 com.fasterxml.jackson.databind.ObjectMapper; 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, String.class ); ObjectMapper objectMapper = new ObjectMapper(); Map body = objectMapper.readValue(response.getBody(), Map.class); log.info("[AciKeyBatch] 응답 전체: {}", body); String status = (String) body.get("Status"); String aciKey = (String) body.get("ACIKey"); String errMsg = (String) body.get("ErrMsg"); if ("success".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; } }