-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feature :: 위해우려제품 상세 정보를 초록누리 API 로 불러와서 저장하기
- Loading branch information
Showing
11 changed files
with
351 additions
and
52 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
.../java/com/kernel360/modulebatch/concernedproduct/client/ConcernedProductDetailClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.kernel360.modulebatch.concernedproduct.client; | ||
|
||
import com.kernel360.ecolife.entity.ConcernedProduct; | ||
import jakarta.annotation.PostConstruct; | ||
import java.nio.charset.StandardCharsets; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestClient; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
@Slf4j | ||
@Component | ||
public class ConcernedProductDetailClient { | ||
|
||
@Value("${external.ecolife-api.path}") | ||
private String BASE_PATH; | ||
|
||
@Value("${external.ecolife-api.service-key}") | ||
private String AUTH_KEY; | ||
|
||
private final RestClient restClient; | ||
|
||
public ConcernedProductDetailClient() { | ||
this.restClient = RestClient.builder() | ||
.build(); | ||
log.info("ConcernedProductDetailClient initialized with BASE_PATH: " + BASE_PATH); | ||
} | ||
@PostConstruct | ||
public void postConstruct() { | ||
log.info("ConcernedProductDetailClient postConstruct with BASE_PATH: " + BASE_PATH); | ||
} | ||
|
||
public String getXmlResponse(ConcernedProduct concernedProduct) { | ||
|
||
return restClient.get().uri(buildUri(concernedProduct)) | ||
.accept(MediaType.APPLICATION_XML) | ||
.acceptCharset(StandardCharsets.UTF_8) | ||
.retrieve() | ||
.onStatus(HttpStatusCode::is4xxClientError, | ||
((request, response) -> { | ||
log.error("[ERROR] :: 4XX 에러 발생" | ||
+ response.getStatusText()); | ||
throw new RuntimeException( | ||
response.getStatusCode() | ||
+ response.getHeaders() | ||
.toString()); | ||
})) | ||
.onStatus(HttpStatusCode::is5xxServerError, | ||
(request, response) -> { | ||
log.error("[ERROR] :: 5XX 에러 발생" | ||
+ response.getStatusText()); | ||
throw new RuntimeException( | ||
response.getStatusCode() | ||
+ response.getHeaders() | ||
.toString()); | ||
}) | ||
.onStatus(HttpStatusCode::is2xxSuccessful, | ||
((request, response) -> log.info("[INFO] :: 2XX API 요청 성공" | ||
+ response.getBody()))) | ||
.body(String.class); | ||
} | ||
|
||
public String buildUri(ConcernedProduct concernedProduct) { | ||
|
||
return UriComponentsBuilder.fromHttpUrl(BASE_PATH) | ||
.queryParam("AuthKey", AUTH_KEY) | ||
.queryParam("ServiceName", "slfsfcfst01Detail") | ||
.queryParam("prdtNo", concernedProduct.getProductNo()) | ||
.toUriString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...kernel360/modulebatch/concernedproduct/job/core/FetchConcernedProductDetailJobConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.kernel360.modulebatch.concernedproduct.job.core; | ||
|
||
import com.kernel360.ecolife.entity.ConcernedProduct; | ||
import com.kernel360.modulebatch.concernedproduct.job.infra.ConcernedProductDetailItemProcessor; | ||
import jakarta.persistence.EntityManagerFactory; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.batch.core.ExitStatus; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.JobExecution; | ||
import org.springframework.batch.core.JobExecutionListener; | ||
import org.springframework.batch.core.Step; | ||
import org.springframework.batch.core.StepExecution; | ||
import org.springframework.batch.core.StepExecutionListener; | ||
import org.springframework.batch.core.configuration.annotation.StepScope; | ||
import org.springframework.batch.core.job.builder.JobBuilder; | ||
import org.springframework.batch.core.launch.support.RunIdIncrementer; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.batch.core.step.builder.StepBuilder; | ||
import org.springframework.batch.item.database.JpaItemWriter; | ||
import org.springframework.batch.item.database.JpaPagingItemReader; | ||
import org.springframework.batch.item.database.builder.JpaPagingItemReaderBuilder; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
@Slf4j | ||
@Configuration | ||
@RequiredArgsConstructor | ||
public class FetchConcernedProductDetailJobConfig { | ||
|
||
private final ConcernedProductDetailItemProcessor concernedProductDetailItemProcessor; | ||
|
||
private final EntityManagerFactory emf; | ||
|
||
@Bean | ||
public Job fetchConcernedProductDetailJob(JobRepository jobRepository, | ||
@Qualifier("fetchConcernedProductDetailStep") Step fetchConcernedProductDetailStep) { | ||
log.info("FetchConcernedProductDetailJobConfig initialized"); | ||
|
||
return new JobBuilder("fetchConcernedProductDetailJobConfig", jobRepository) | ||
.start(fetchConcernedProductDetailStep) | ||
.incrementer(new RunIdIncrementer()) | ||
.listener(new FetchConcernedProductDetailJobListener()) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Step fetchConcernedProductDetailStep(JobRepository jobRepository, | ||
PlatformTransactionManager transactionManager) { | ||
|
||
return new StepBuilder("fetchConcernedProductDetailStep", jobRepository) | ||
.<ConcernedProduct, ConcernedProduct>chunk(100, transactionManager) | ||
.listener(new FetchConcernedProductDetailStepListener()) | ||
.reader(concernedProductDetailItemReader()) | ||
.processor(concernedProductDetailItemProcessor) | ||
.writer(concernedProductItemWriter(emf)) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@StepScope | ||
public JpaPagingItemReader<ConcernedProduct> concernedProductDetailItemReader() { | ||
String jpql = "SELECT cp FROM ConcernedProduct cp"; | ||
|
||
return new JpaPagingItemReaderBuilder<ConcernedProduct>() | ||
.queryString(jpql) | ||
.entityManagerFactory(emf) | ||
.name("concernedProductDetailJpaItemReader") | ||
// FIXME :: 트랜잭션 관리 개선, 작업의 병렬성 조정, 사용자 정의 Reader 를 만드는 식으로 페이지 크기에 따른 읽기 문제를 해결해야 함. | ||
.pageSize(1000) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@StepScope | ||
public JpaItemWriter<ConcernedProduct> concernedProductItemWriter(EntityManagerFactory emf) { | ||
JpaItemWriter<ConcernedProduct> jpaItemWriter = new JpaItemWriter<>(); | ||
jpaItemWriter.setEntityManagerFactory(emf); | ||
|
||
return jpaItemWriter; | ||
} | ||
|
||
//-- Execution Listener --// | ||
|
||
public static class FetchConcernedProductDetailJobListener implements JobExecutionListener { | ||
@Override | ||
public void beforeJob(JobExecution jobExecution) { | ||
log.info("{} starts", jobExecution.getJobInstance().getJobName()); | ||
} | ||
|
||
@Override | ||
public void afterJob(JobExecution jobExecution) { | ||
log.info("{} ends", jobExecution.getJobInstance().getJobName()); | ||
} | ||
} | ||
|
||
public static class FetchConcernedProductDetailStepListener implements StepExecutionListener{ | ||
@Override | ||
public void beforeStep(StepExecution stepExecution) { | ||
log.info("{} starts", stepExecution.getStepName()); | ||
} | ||
|
||
@Override | ||
public ExitStatus afterStep(StepExecution stepExecution) { | ||
log.info("StepExecutionListener - afterStep, step name: {}, status: {}", stepExecution.getStepName(), stepExecution.getStatus()); | ||
log.info("StepExecutionListener - ReadCount: {}, WriteCount: {}, FilterCount: {}, ReadSkipCount: {}, ProcessSkipCount: {}, WriteSkipCount: {}", | ||
stepExecution.getReadCount(), stepExecution.getWriteCount(), stepExecution.getFilterCount(), | ||
stepExecution.getReadSkipCount(), stepExecution.getProcessSkipCount(), stepExecution.getWriteSkipCount()); | ||
return StepExecutionListener.super.afterStep(stepExecution); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.