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: rabbitmq 기본설정 추가 #52

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions domain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ dependencies {
runtimeOnly("mysql:mysql-connector-java")
developmentOnly("org.springframework.boot:spring-boot-devtools")
api("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-amqp")
implementation("com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.3")
testRuntimeOnly("com.h2database:h2")
testImplementation("org.springframework.amqp:spring-rabbit-test")
}

bootJar {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package mashup.backend.spring.member.infrastructure.amqp

import org.springframework.amqp.core.Binding
import org.springframework.amqp.core.BindingBuilder
import org.springframework.amqp.core.Queue
import org.springframework.amqp.core.TopicExchange
import org.springframework.amqp.rabbit.connection.ConnectionFactory
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class AmqpConfig {
companion object {
const val topicExchangeName = "test-exchange"
const val queueName = "member"
}

@Bean
fun queue(): Queue {
return Queue(queueName, false)
}

@Bean
fun exchange(): TopicExchange? {
return TopicExchange(topicExchangeName)
}

@Bean
fun binding(queue: Queue?, exchange: TopicExchange?): Binding {
return BindingBuilder.bind(queue).to(exchange).with("member.#")
}

@Bean
fun container(
connectionFactory: ConnectionFactory,
listenerAdapter: MessageListenerAdapter
): SimpleMessageListenerContainer? {
val container = SimpleMessageListenerContainer()
container.connectionFactory = connectionFactory
container.setQueueNames(queueName)
container.setMessageListener(listenerAdapter)
return container
}

@Bean
fun listenerAdapter(receiver: Receiver?): MessageListenerAdapter {
return MessageListenerAdapter(receiver, "receiveMessage")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mashup.backend.spring.member.infrastructure.amqp

import org.springframework.stereotype.Component
import java.util.concurrent.CountDownLatch

@Component
class Receiver {
val latch = CountDownLatch(1)

fun receiveMessage(message: String) {
println("Received <$message>")
latch.countDown()
}
}
12 changes: 11 additions & 1 deletion domain/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,14 @@ spring:
password: ENC(iff8gDZ+DD3jE5Ekm6WHp8fbilspPmYSuXe1jA2rRPRIjpjhlAVbEjsDk+B0Kbwj)
jpa:
hibernate:
ddl-auto: update
ddl-auto: update
rabbitmq:
host: b-708a3dff-a404-4fa8-8285-794c9180fcea.mq.ap-northeast-2.amazonaws.com
port: 5671
username: member
password: ENC(yHVJNvwjSH3pzvNvtf1ZgUrn+EsAcjPJOc7tUMAkPMmiSMgo9PXR4ZapTshSq+0D)
ssl:
enabled: true
listener:
simple:
acknowledge-mode: manual
14 changes: 13 additions & 1 deletion domain/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
jasypt:
encryptor:
password: # inject from environment variable
password: # inject from environment variable

spring:
rabbitmq:
host: b-708a3dff-a404-4fa8-8285-794c9180fcea.mq.ap-northeast-2.amazonaws.com
port: 5671
username: member
password: ENC(yHVJNvwjSH3pzvNvtf1ZgUrn+EsAcjPJOc7tUMAkPMmiSMgo9PXR4ZapTshSq+0D)
ssl:
enabled: true
listener:
simple:
acknowledge-mode: manual
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package mashup.backend.spring.member.infrastructure.amqp

import org.junit.jupiter.api.Test
import org.springframework.amqp.rabbit.core.RabbitTemplate
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import java.util.concurrent.TimeUnit

@SpringBootTest
internal class AmqpConfigTest {
@Autowired
lateinit var receiver: Receiver

@Autowired
lateinit var rabbitTemplate: RabbitTemplate

@Test
fun test() {
println("Sending message...")
rabbitTemplate.convertAndSend(
AmqpConfig.topicExchangeName,
"member.#",
"Hello from RabbitMQ!"
)
receiver.latch.await(10000, TimeUnit.MILLISECONDS)
}
}
15 changes: 15 additions & 0 deletions domain/src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
spring:
rabbitmq:
host: b-708a3dff-a404-4fa8-8285-794c9180fcea.mq.ap-northeast-2.amazonaws.com
port: 5671
username: member
password: ENC(yHVJNvwjSH3pzvNvtf1ZgUrn+EsAcjPJOc7tUMAkPMmiSMgo9PXR4ZapTshSq+0D)
ssl:
enabled: true
listener:
simple:
acknowledge-mode: manual

jasypt:
encryptor:
password: # inject from environment variable