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

[Feat] SQS 모듈 생성 후, FCM 모듈에서 변경 #56

Merged
merged 7 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ jobs:
echo "${{ secrets.MONGO }}" | base64 --decode > ./mongo.yml
working-directory: ./dmforu-infrastructure/storage/mongo/src/main/resources

- name: Create SQS resources directory
run: |
mkdir -p ./dmforu-infrastructure/sqs/src/main/resources

- name: make sqs.yml
run: |
touch ./sqs.yml
echo "${{ secrets.SQS }}" | base64 --decode > ./sqs.yml
working-directory: ./dmforu-infrastructure/sqs/src/main/resources

- name: Create Firebase key resources directory
run: |
mkdir -p ./dmforu-infrastructure/fcm/src/main/resources/key
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ out/
### Configuration Settings ###
**/mongo.yml
**/mysql.yml
**/sqs.yml
**/fire-base-key.json
2 changes: 1 addition & 1 deletion dmforu-admin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies {

implementation("org.springframework.boot:spring-boot-starter-web")

runtimeOnly(project(":dmforu-infrastructure:fcm"))
runtimeOnly(project(":dmforu-infrastructure:sqs"))
runtimeOnly(project(":dmforu-infrastructure:storage:mysql"))
runtimeOnly(project(":dmforu-infrastructure:storage:mongo"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import java.util.*
@SpringBootApplication(
scanBasePackages = [
"com.dmforu.admin",
"com.dmforu.fcm",
"com.dmforu.sqs",
"com.dmforu.storage.db.mongo",
"com.dmforu.storage.db.mysql"
]
Expand Down
1 change: 1 addition & 0 deletions dmforu-admin/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ spring:
- mysql.yml
- mongo.yml
- monitoring.yml
- sqs.yml
web.resources.add-mappings: false

spring.lifecycle.timeout-per-shutdown-phase: 30s
Expand Down
9 changes: 9 additions & 0 deletions dmforu-infrastructure/sqs/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dependencies {
compileOnly(project(":dmforu-domain"))

implementation(enforcedPlatform("software.amazon.awssdk:bom:2.21.20"))
implementation("software.amazon.awssdk:sqs")

testImplementation(project(":dmforu-domain"))
testImplementation("org.mockito.kotlin:mockito-kotlin:4.1.0")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.dmforu.sqs

import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider
import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.sqs.SqsAsyncClient

@Configuration
class AWSConfig(
@Value("\${cloud.aws.credentials.access-key}")
val accessKey: String,

@Value("\${cloud.aws.credentials.secret-key}")
val secretKey: String,

@Value("\${cloud.aws.sqs.queue.url}")
val queueUrl: String,

@Value("\${cloud.aws.sqs.queue.message-delay-seconds}")
val messageDelaySecs: Int,
) {

@Bean
fun sqsAsyncClient(): SqsAsyncClient {
return SqsAsyncClient.builder()
.region(Region.AP_NORTHEAST_2)
.credentialsProvider(createAwsCredentialsProvider())
.build()
}

private fun createAwsCredentialsProvider(): StaticCredentialsProvider {
val basicAwsCredentials = AwsBasicCredentials.create(accessKey, secretKey)

return StaticCredentialsProvider.create(basicAwsCredentials)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.dmforu.sqs

import software.amazon.awssdk.services.sqs.model.MessageAttributeValue
import software.amazon.awssdk.services.sqs.model.SendMessageRequest

object SqsMessageMapper {

fun createRequest(content: String, attributes: Map<String, MessageAttributeValue>, queueUrl: String, delaySeconds: Int): SendMessageRequest {
return SendMessageRequest.builder()
.queueUrl(queueUrl)
.delaySeconds(delaySeconds)
.messageAttributes(attributes)
.messageBody(content)
.build()
}

fun createAttributes(
tokens: List<String>,
title: String,
type: String,
url: String,
): Map<String, MessageAttributeValue> {
val attributes = mutableMapOf<String, MessageAttributeValue>()

attributes["tokens"] = MessageAttributeValue.builder().stringValue(tokens.toString()).dataType("String").build()
attributes["title"] = MessageAttributeValue.builder().stringValue(title).dataType("String").build()
attributes["type"] = MessageAttributeValue.builder().stringValue(type).dataType("String").build()
attributes["url"] = MessageAttributeValue.builder().stringValue(url).dataType("String").build()

return attributes
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.dmforu.sqs

class SqsMessageSendException (message: String, cause: Throwable? = null) : RuntimeException(message, cause)
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.dmforu.sqs

import com.dmforu.domain.message.MessageSender
import com.dmforu.domain.message.NoticeMessage
import com.dmforu.sqs.SqsMessageMapper.createAttributes
import com.dmforu.sqs.SqsMessageMapper.createRequest
import org.springframework.stereotype.Component
import software.amazon.awssdk.services.sqs.SqsAsyncClient
import software.amazon.awssdk.services.sqs.model.SendMessageResponse
import software.amazon.awssdk.services.sqs.model.SqsException

@Component
class SqsMessageSender(
private val sqsAsyncClient: SqsAsyncClient,
private val awsConfig: AWSConfig,
) : MessageSender {

override fun sendNoticeMessage(message: NoticeMessage, tokens: List<String>) {
val attributes = createAttributes(
tokens = tokens,
title = message.title,
type = message.type,
url = message.url
)

val request = createRequest(
content = message.body,
attributes = attributes,
queueUrl = awsConfig.queueUrl,
delaySeconds = awsConfig.messageDelaySecs
)

try {
val future = sqsAsyncClient.sendMessage(request)

future.whenComplete { sendMessageResponse: SendMessageResponse, throwable: Throwable? ->
if (throwable != null) {
throw SqsMessageSendException("[SQS Async Client] 메세지 전송에 실패하였습니다.", throwable)
}
}
} catch (sqsException: SqsException) {
throw SqsMessageSendException("[SQS] 메세지 전송에 실패하였습니다.", sqsException)
}
}


}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ include(
"dmforu-domain",
"dmforu-crawling",

"dmforu-infrastructure:sqs",
"dmforu-infrastructure:fcm",
"dmforu-infrastructure:storage:mysql",
"dmforu-infrastructure:storage:mongo",
Expand Down
Loading