broker
태그에 useVirtualTopics
속성을 true
로 설정하여 추가한다.
아래와 같이 destinationInterceptors
태그를 추가한다.
<destinationInterceptors>
<virtualDestinationInterceptor>
<virtualDestinations>
<virtualTopic name="VirtualTopic.>" prefix="Consumer.*." selectorAware="false"/>
</virtualDestinations>
</virtualDestinationInterceptor>
</destinationInterceptors>
virtualTopic
태그의name
속성은Publisher
의destination
명칭에 대한 패턴이다.virtualTopic
태그의prefix
속성은Subscriber
의destination
명칭에 대한 패턴이다.
<!-- activemq.xml -->
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}" useVirtualTopics="true">
<destinationInterceptors>
<virtualDestinationInterceptor>
<virtualDestinations>
<virtualTopic name="VirtualTopic.>" prefix="Consumer.*." selectorAware="false"/>
</virtualDestinations>
</virtualDestinationInterceptor>
</destinationInterceptors>
<!-- 이하 생략 -->
</broker>
예를 들어 Pulblisher
의 destination
을 VirtualTopic.TEST
으로 전송하는 경우 일반적인 Pub-Sub 모델로 메시지를 수신하려면 수신자의 destination
을 VirtualTopic.TEST
으로 정하면 된다.
Pulblisher 예시
@Component
class MessageSender(private val jmsTemplate: JmsTemplate) {
private val logger = LoggerFactory.getLogger(this::class.java)
fun sendVirtual(message: SampleData) {
logger.info("Sender Message -> [$message]")
jmsTemplate.convertAndSend("VirtualTopic.TEST", message)
}
}
data class SampleData(
val body: String = ""
) : Serializable
Pulblisher application.yml 예시
spring:
jms:
pub-sub-domain: true
activemq:
broker-url: tcp://localhost:61616
user: admin
password: admin
Subscriber 예시
@Component
class MessageListener {
private val logger = LoggerFactory.getLogger(this::class.java)
@JmsListener(destination = "VirtualTopic.TEST")
fun listen3(message: SampleData) {
logger.info("Server3 Virtual1 - > $message")
}
@JmsListener(destination = "VirtualTopic.TEST")
fun listen4(message: SampleData) {
logger.info("Server3 Virtual2 - > $message")
}
}
data class SampleData(
val body: String = ""
) : Serializable
Subscriber application.yml 예시
spring:
jms:
pub-sub-domain: true
activemq:
broker-url: tcp://localhost:61616
user: admin
password: admin
예를 들어 Pulblisher
의 destination
을 VirtualTopic.TEST
으로 전송하고 Subscriber
는 destination
을 Consumer.{Client-ID}.VirtualTopic.TEST
로 지정하여 사용합니다.
Client-ID
별로 별도 메시지를 수신할 수 있다.
Pulblisher 예시
@Component
class MessageSender(private val jmsTemplate: JmsTemplate) {
private val logger = LoggerFactory.getLogger(this::class.java)
fun sendVirtual(message: SampleData) {
logger.info("Sender Message -> [$message]")
jmsTemplate.convertAndSend("VirtualTopic.TEST", message)
}
}
data class SampleData(
val body: String = ""
) : Serializable
Pulblisher application.yml 예시
spring:
jms:
pub-sub-domain: true
activemq:
broker-url: tcp://localhost:61616
user: admin
password: admin
Subscriber 예시
@Component
class MessageListener {
private val logger = LoggerFactory.getLogger(this::class.java)
@JmsListener(destination = "Consumer.test.VirtualTopic.TEST")
fun listen4(message: SampleData) {
logger.info("Server1 Virtual2 - > $message")
}
}
data class SampleData(
val body: String = ""
) : Serializable
Subscriber application.yml 예시
spring:
jms:
pub-sub-domain: false
activemq:
broker-url: tcp://localhost:61616
user: admin
password: admin
<policyEntry queue=">">
<deadLetterStrategy>
<individualDeadLetterStrategy queuePrefix="DLQ." useQueueForQueueMessages="true"/>
</deadLetterStrategy>
</policyEntry>
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}" useVirtualTopics="true">
<destinationPolicy>
<policyMap>
<policyEntries>
<policyEntry queue=">">
<deadLetterStrategy>
<individualDeadLetterStrategy queuePrefix="DLQ." useQueueForQueueMessages="true"/>
</deadLetterStrategy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
</broker>