-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from SKY-HORSE-MAN-POWER/develop
[DEPLOYMENT] 경매 낙찰 시, `auction-close` 토픽으로 수정
- Loading branch information
Showing
21 changed files
with
671 additions
and
67 deletions.
There are no files selected for viewing
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
26 changes: 26 additions & 0 deletions
26
src/main/java/com/skyhorsemanpower/auction/common/DateTimeConverter.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,26 @@ | ||
package com.skyhorsemanpower.auction.common; | ||
|
||
import com.skyhorsemanpower.auction.status.TimeZoneChangeEnum; | ||
|
||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.ZoneOffset; | ||
|
||
public class DateTimeConverter { | ||
|
||
public static LocalDateTime instantToLocalDateTime(long longOfInstant) { | ||
return LocalDateTime.ofInstant(Instant.ofEpochMilli(longOfInstant), ZoneId.systemDefault()); | ||
} | ||
|
||
public static long localDateTimeToInstant(LocalDateTime localDateTime) { | ||
return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); | ||
} | ||
|
||
public static long kstLocalDateTimeToInstant(LocalDateTime kstLocalDateTime) { | ||
return kstLocalDateTime.minusHours(TimeZoneChangeEnum.KOREA.getTimeDiff()) | ||
.toInstant(ZoneOffset.UTC) | ||
.toEpochMilli(); | ||
} | ||
|
||
} |
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
51 changes: 19 additions & 32 deletions
51
src/main/java/com/skyhorsemanpower/auction/config/QuartzConfig.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 |
---|---|---|
@@ -1,64 +1,51 @@ | ||
package com.skyhorsemanpower.auction.config; | ||
|
||
import com.skyhorsemanpower.auction.quartz.EndAuction; | ||
import com.skyhorsemanpower.auction.kafka.dto.InitialAuctionDto; | ||
import com.skyhorsemanpower.auction.quartz.AuctionClose; | ||
import lombok.RequiredArgsConstructor; | ||
import org.quartz.*; | ||
import org.quartz.impl.StdSchedulerFactory; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.Calendar; | ||
import java.time.Instant; | ||
import java.util.Date; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class QuartzConfig { | ||
private final Scheduler scheduler; | ||
|
||
@Bean | ||
public Scheduler scheduler() throws SchedulerException { | ||
Scheduler scheduler = new StdSchedulerFactory().getScheduler(); | ||
scheduler.start(); | ||
return scheduler; | ||
} | ||
|
||
// SimpleScheduler 메서드 | ||
public void schedulerEndAuctionJob(String auctionUuid) throws SchedulerException { | ||
// 경매를 만드는 시간에서 하루를 더한 endedAt | ||
Calendar calendar = Calendar.getInstance(); | ||
calendar.add(Calendar.DAY_OF_MONTH, 1); | ||
Date endedAt = calendar.getTime(); | ||
|
||
// 경매 시작과 경매 마감의 상태 변경 스케줄링 | ||
public void schedulerUpdateAuctionStateJob(InitialAuctionDto initialAuctionDto) throws SchedulerException { | ||
// JobDataMap 생성 및 auctionUuid 설정 | ||
JobDataMap jobDataMap = new JobDataMap(); | ||
jobDataMap.put("auctionUuid", auctionUuid); | ||
|
||
// 오류로 실행되지 않았을 때를 위한 retryCount key 값 | ||
jobDataMap.put("retryCount", 0); | ||
jobDataMap.put("auctionUuid", initialAuctionDto.getAuctionUuid()); | ||
|
||
// Job 생성 | ||
JobDetail job = JobBuilder | ||
.newJob(EndAuction.class) | ||
.withIdentity("EndAuctionJob_" + auctionUuid, "EndAuctionGroup") | ||
JobDetail auctionCloseJob = JobBuilder | ||
.newJob(AuctionClose.class) | ||
.withIdentity("AuctionCloseJob_" + initialAuctionDto.getAuctionUuid(), | ||
"AuctionCloseGroup") | ||
.usingJobData(jobDataMap) | ||
.withDescription("경매 마감 Job") | ||
.build(); | ||
|
||
Date auctionEndDate = Date.from(Instant.ofEpochMilli(initialAuctionDto.getAuctionStartTime())); | ||
|
||
// Trigger 생성 | ||
Trigger trigger = TriggerBuilder | ||
Trigger auctionCloseTrigger = TriggerBuilder | ||
.newTrigger() | ||
.withIdentity("EndAuctionTrigger_" + auctionUuid, "EndAuctionGroup") | ||
.withIdentity("AuctionCloseTrigger_" + initialAuctionDto.getAuctionUuid(), | ||
"AuctionCloseGroup") | ||
.withDescription("경매 마감 Trigger") | ||
|
||
// test용 30초 후 시작하는 스케줄러 | ||
// test용 60초 후 시작하는 스케줄러 | ||
.startAt(DateBuilder.futureDate(60, DateBuilder.IntervalUnit.SECOND)) | ||
|
||
//Todo 실제 배포에서는 endedAt을 사용해야 한다. | ||
// .startAt(endedAt) | ||
//Todo 실제 배포에서는 auctionEndDate을 사용해야 한다. | ||
// .startAt(auctionEndDate) | ||
.build(); | ||
|
||
// 스케줄러 생성 및 Job, Trigger 등록 | ||
scheduler.scheduleJob(job, trigger); | ||
|
||
scheduler.scheduleJob(auctionCloseJob, auctionCloseTrigger); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/skyhorsemanpower/auction/domain/AuctionCloseState.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,26 @@ | ||
package com.skyhorsemanpower.auction.domain; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@ToString | ||
@Document(collection = "auction_close_state") | ||
public class AuctionCloseState { | ||
@Id | ||
private String auctionCloseStateId; | ||
|
||
private String auctionUuid; | ||
private boolean auctionCloseState; | ||
|
||
@Builder | ||
public AuctionCloseState(String auctionUuid, boolean auctionCloseState) { | ||
this.auctionUuid = auctionUuid; | ||
this.auctionCloseState = auctionCloseState; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/com/skyhorsemanpower/auction/kafka/KafkaConsumerCluster.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,76 @@ | ||
package com.skyhorsemanpower.auction.kafka; | ||
|
||
import com.skyhorsemanpower.auction.common.DateTimeConverter; | ||
import com.skyhorsemanpower.auction.config.QuartzConfig; | ||
import com.skyhorsemanpower.auction.domain.RoundInfo; | ||
import com.skyhorsemanpower.auction.kafka.dto.InitialAuctionDto; | ||
import com.skyhorsemanpower.auction.repository.RoundInfoRepository; | ||
import com.skyhorsemanpower.auction.status.RoundTimeEnum; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.kafka.annotation.KafkaListener; | ||
import org.springframework.messaging.MessageHeaders; | ||
import org.springframework.messaging.handler.annotation.Headers; | ||
import org.springframework.messaging.handler.annotation.Payload; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.LocalDateTime; | ||
import java.util.LinkedHashMap; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Component | ||
public class KafkaConsumerCluster { | ||
private final RoundInfoRepository roundInfoRepository; | ||
private final QuartzConfig quartzConfig; | ||
|
||
@KafkaListener(topics = Topics.Constant.INITIAL_AUCTION, groupId = "${spring.kafka.consumer.group-id}") | ||
public void initialAuction(@Payload LinkedHashMap<String, Object> message, | ||
@Headers MessageHeaders messageHeaders) { | ||
log.info("consumer: success >>> message: {}, headers: {}", message.toString(), | ||
messageHeaders); | ||
|
||
// round_info 초기 데이터 저장 | ||
InitialAuctionDto initialAuctionDto = InitialAuctionDto.builder() | ||
.auctionUuid(message.get("auctionUuid").toString()) | ||
.startPrice(new BigDecimal(message.get("startPrice").toString())) | ||
.numberOfEventParticipants((Integer) message.get("numberOfEventParticipants")) | ||
.auctionStartTime(((Long) message.get("auctionStartTime"))) | ||
.auctionEndTime(((Long) message.get("auctionEndTime"))) | ||
.incrementUnit(new BigDecimal(message.get("incrementUnit").toString())) | ||
.build(); | ||
log.info("InitialAuctionDto >>> {}", initialAuctionDto.toString()); | ||
|
||
initialRoundInfo(initialAuctionDto); | ||
|
||
// 경매 마감 스케줄러 등록 | ||
try { | ||
quartzConfig.schedulerUpdateAuctionStateJob(initialAuctionDto); | ||
} catch (Exception e1) { | ||
log.warn(e1.getMessage()); | ||
} | ||
} | ||
|
||
private void initialRoundInfo(InitialAuctionDto initialAuctionDto) { | ||
// Instant 타입을 LocalDateTime 변환 | ||
LocalDateTime roundStartTime = DateTimeConverter. | ||
instantToLocalDateTime(initialAuctionDto.getAuctionStartTime()); | ||
|
||
RoundInfo roundinfo = RoundInfo.builder() | ||
.auctionUuid(initialAuctionDto.getAuctionUuid()) | ||
.round(1) | ||
.roundStartTime(roundStartTime) | ||
.roundEndTime(roundStartTime.plusSeconds(RoundTimeEnum.SECONDS_60.getSecond())) | ||
.incrementUnit(initialAuctionDto.getIncrementUnit()) | ||
.price(initialAuctionDto.getStartPrice()) | ||
.isActive(true) | ||
.numberOfParticipants((long) initialAuctionDto.getNumberOfEventParticipants()) | ||
.leftNumberOfParticipants((long) initialAuctionDto.getNumberOfEventParticipants()) | ||
.createdAt(LocalDateTime.now()) | ||
.build(); | ||
|
||
log.info("Initial round_info >>> {}", roundinfo); | ||
roundInfoRepository.save(roundinfo); | ||
} | ||
} |
Oops, something went wrong.