Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#33] 경매 활성화 및 종료 상태 변경 코드 추가 #34

Merged
merged 8 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions src/main/java/com/example/readyauction/batch/BatchConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
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.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import com.example.readyauction.batch.job.RedisLikeReader;
import com.example.readyauction.batch.job.RedisLikeWriter;

@Configuration
@EnableBatchProcessing
public class BatchConfig {
Expand All @@ -27,24 +25,49 @@ public class BatchConfig {
private final ItemReader redisLikeReader;
private final ItemWriter redisLikeWriter;

private final ItemProcessor productConditionUpdate;
private final ItemReader productReader;
private final ItemWriter productConditionWriter;

public BatchConfig(JobRepository jobRepository, PlatformTransactionManager platformTransactionManager,
RedisLikeReader redisLikeReader, RedisLikeWriter redisLikeWriter) {
ItemReader redisLikeReader, ItemWriter redisLikeWriter, ItemProcessor productConditionUpdate,
ItemReader productReader, ItemWriter productConditionWriter) {
this.jobRepository = jobRepository;
this.platformTransactionManager = platformTransactionManager;
this.redisLikeReader = redisLikeReader;
this.redisLikeWriter = redisLikeWriter;
this.productConditionUpdate = productConditionUpdate;
this.productReader = productReader;
this.productConditionWriter = productConditionWriter;
}

@Bean
public Job updateProductConditionJob() {
return new JobBuilder("updateProductCondition", jobRepository)
.start(updateProductConditionStep())
.build();
}

@Bean
public Step updateProductConditionStep() {
return new StepBuilder("updateProductConditionStep", jobRepository)
.chunk(CHUNK_SIZE, platformTransactionManager)
.reader(productReader)
.processor(productConditionUpdate)
.writer(productConditionWriter)
.build();
}

@Bean
public Job readLikeInRedis() {
return new JobBuilder("readLikeInRedis", jobRepository)
.start(updateStep())
public Job readLikeInRedisJob() {
return new JobBuilder("readLikeInRedisJob", jobRepository)
.start(updateProductLikeStep())
.build();
}

@Bean
public Step updateStep() {
return new StepBuilder("updateProductLikeDB", jobRepository)
public Step updateProductLikeStep() {
return new StepBuilder("updateProductLikeStep", jobRepository)
.chunk(CHUNK_SIZE, platformTransactionManager)
.reader(redisLikeReader)
.writer(redisLikeWriter)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.readyauction.batch.job;

import java.time.LocalDateTime;

import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Component;

import com.example.readyauction.domain.product.Product;
import com.example.readyauction.domain.product.ProductCondition;

@Component
public class ProductConditionUpdate implements ItemProcessor<Product, Product> {
// 상품의 상태를 갱신하는 로직
@Override
public Product process(Product product) throws Exception {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요거 그냥 Writer에서 하고 Processor 빼도 될거같아요 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 수정하도록 하겠습니다!

LocalDateTime now = LocalDateTime.now();
// 경매 상태 업데이트
if (now.isBefore(product.getStartDate())) {
product.updateProductCondition(ProductCondition.READY); // 경매 대기중
} else if (now.isAfter(product.getCloseDate())) {
product.updateProductCondition(ProductCondition.DONE); // 경매 종료됨
} else {
product.updateProductCondition(ProductCondition.ACTIVE); // 경매 진행중
}
return product;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.readyauction.batch.job;

import java.util.List;

import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Component;

import com.example.readyauction.domain.product.Product;
import com.example.readyauction.repository.product.ProductRepository;

@Component
public class ProductConditionWriter implements ItemWriter<Product> {

private final ProductRepository productRepository;

public ProductConditionWriter(ProductRepository productRepository) {
this.productRepository = productRepository;
}

@Override
public void write(Chunk<? extends Product> chunk) throws Exception {
List<? extends Product> products = chunk.getItems();
productRepository.saveAll(products);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.readyauction.batch.job;

import java.util.List;

import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import com.example.readyauction.domain.product.Product;
import com.example.readyauction.repository.product.ProductRepository;

@Component
public class ProductReader implements ItemReader<List<Product>> {
private static final int PAGE_SIZE = 500; // 한번에 읽을 데이터 개수 (청크 사이즈)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요거 CHUNK_SIZE를 같이 사용하는게 어떨까요 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하 넵!!

private final ProductRepository productRepository;
private int currentPage = 0;

public ProductReader(ProductRepository productRepository) {
this.productRepository = productRepository;
}

@Override
public List<Product> read() throws
Exception,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

예외 목록이 약간 읽기 어렵게 줄바꿈이 된 것 같아요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요게 저두 불편한데,, 줄바꿈을 제가 바꿔두 save 하면 저런식으로 줄바꿈이 되더라구용,,,🥲

UnexpectedInputException,
ParseException,
NonTransientResourceException {
Pageable pageable = PageRequest.of(currentPage, PAGE_SIZE);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전체를 다 읽는거라 데이터가 많아지면 점점 느려질 것 같아요, more 방식으로 읽도록 변경하는게 어떨까요 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 그렇네요!! more 방식으로 수정하도록 하겠습니다!

List<Product> products = productRepository.findAll(pageable).getContent();

if (products.isEmpty()) {
currentPage = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요거는 혹시 무슨 의도인가요 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엇 이게 DB에서 더이상 읽어올게없으면 다시 처음부터 읽을 수 있도록 pageNo를 0으로 하는 코드였습니다!

return null;
}
currentPage++;
return products;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ public ProductResponse enroll(

@GetMapping("/{id}")
public ProductFindResponse findById(@PathVariable Long id) {
LocalDateTime request = LocalDateTime.now();
ProductFindResponse productFindResponse = productFacade.findById(id, request);
return productFindResponse;
LocalDateTime requestTime = LocalDateTime.now();
return productFacade.findById(id, requestTime);
}

@GetMapping
Expand Down
Loading