-
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 branch 'develop' into fix/async
- Loading branch information
Showing
19 changed files
with
384 additions
and
23 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
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
46 changes: 46 additions & 0 deletions
46
src/main/java/com/dailyon/auctionservice/config/SqsConfig.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,46 @@ | ||
package com.dailyon.auctionservice.config; | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.sqs.AmazonSQSAsync; | ||
import com.amazonaws.services.sqs.AmazonSQSAsyncClientBuilder; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate; | ||
import org.springframework.cloud.context.config.annotation.RefreshScope; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.core.env.Environment; | ||
|
||
@Configuration | ||
public class SqsConfig { | ||
// bus-refresh 적용된 부분 | ||
private final Environment environment; | ||
|
||
@Autowired | ||
public SqsConfig(Environment environment) { | ||
this.environment = environment; | ||
} | ||
|
||
@Bean | ||
@Primary | ||
@RefreshScope | ||
public AmazonSQSAsync amazonSQSAsync() { | ||
|
||
String accessKey = environment.getProperty("cloud.aws.credentials.ACCESS_KEY_ID"); | ||
String secretKey = environment.getProperty("cloud.aws.credentials.SECRET_ACCESS_KEY"); | ||
String sqsRegion = environment.getProperty("cloud.aws.sqs.region"); | ||
|
||
BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey); | ||
return AmazonSQSAsyncClientBuilder.standard() | ||
.withRegion(sqsRegion) | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCreds)) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public QueueMessagingTemplate queueMessagingTemplate(AmazonSQSAsync amazonSQSAsync) { | ||
return new QueueMessagingTemplate(amazonSQSAsync); | ||
} | ||
|
||
} |
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
34 changes: 34 additions & 0 deletions
34
src/main/java/com/dailyon/auctionservice/infra/sqs/AuctionSqsProducer.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,34 @@ | ||
package com.dailyon.auctionservice.infra.sqs; | ||
|
||
|
||
import com.dailyon.auctionservice.infra.sqs.dto.SQSNotificationDto; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.messaging.support.MessageBuilder; | ||
import org.springframework.stereotype.Component; | ||
|
||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class AuctionSqsProducer { | ||
|
||
private final QueueMessagingTemplate sqsTemplate; | ||
private final ObjectMapper objectMapper; | ||
|
||
public static final String AUCTION_END_NOTIFICATION_QUEUE = "auction-end-notification-queue"; | ||
|
||
public void produce(String queueName, SQSNotificationDto sqsNotificationDto) { | ||
// 알림 생성 중 에러 때문에 전체 로직이 취소되는것을 막음. | ||
try { | ||
String jsonMessage = objectMapper.writeValueAsString(sqsNotificationDto); | ||
Message<String> message = MessageBuilder.withPayload(jsonMessage).build(); | ||
sqsTemplate.send(queueName, message); | ||
} catch (Exception e) { | ||
log.error(e.getMessage()); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/dailyon/auctionservice/infra/sqs/dto/RawNotificationData.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,32 @@ | ||
package com.dailyon.auctionservice.infra.sqs.dto; | ||
|
||
import com.dailyon.auctionservice.infra.sqs.dto.enums.NotificationType; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class RawNotificationData { | ||
private String message; | ||
private Map<String, String> parameters; | ||
private NotificationType notificationType; // 알림 유형 | ||
|
||
|
||
public static RawNotificationData forAuctionEnd(String auctionId) { | ||
Map<String, String> parameters = new HashMap<>(); | ||
parameters.put("auctionId", auctionId); | ||
|
||
return new RawNotificationData( | ||
null, | ||
parameters, | ||
NotificationType.AUCTION_END | ||
); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/dailyon/auctionservice/infra/sqs/dto/SQSNotificationDto.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,23 @@ | ||
package com.dailyon.auctionservice.infra.sqs.dto; | ||
|
||
import lombok.*; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
@Getter | ||
@ToString | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class SQSNotificationDto { | ||
List<Long> whoToNotify; // if null, 전체유저에게 발송 | ||
|
||
RawNotificationData rawNotificationData; | ||
|
||
public static SQSNotificationDto create( RawNotificationData rawNotificationData) { | ||
return SQSNotificationDto.builder() | ||
.rawNotificationData(rawNotificationData) | ||
.build(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/dailyon/auctionservice/infra/sqs/dto/enums/NotificationType.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,25 @@ | ||
package com.dailyon.auctionservice.infra.sqs.dto.enums; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum NotificationType { | ||
PRODUCT_RESTOCK("재입고", "상품 재입고 알림."), | ||
ORDER_COMPLETE("주문완료", "주문이 완료되었습니다."), | ||
ORDER_SHIPPED("선적", "주문하신 상품이 출발했습니다."), | ||
ORDER_ARRIVED("배송 도착", "주문하신 상품이 도착했습니다."), | ||
ORDER_CANCELED("주문 취소", "주문이 취소되었습니다."), | ||
AUCTION_END("실시간 경매 종료", "실시간 경매가 종료되었습니다."), | ||
GIFT_RECEIVED("선물", "선물을 받았습니다."), | ||
POINTS_EARNED_SNS("SNS 구매유도 포인트 적립", "SNS를 통해 포인트가 적립되었습니다."), | ||
HEARTBEAT("하트비트", "연결 유지용 주기적 송신."); | ||
// 정의하면서 넣을 예정 | ||
|
||
private final String name; | ||
private final String description; | ||
|
||
NotificationType(String name, String description) { | ||
this.name = name; | ||
this.description = description; | ||
} | ||
} |
Oops, something went wrong.