说明
1-在两个不同的应用发送和接收消息
2-消息发送应用是 e-car 项目,接收端是 tcm-chatgpt项目,当然,同一个项目也是可以发送和接收的
e-car项目配置
引入activemq依赖
<!-- 集成 ActiveMQ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
application.yml配置
spring:
# activemq相关配置
activemq:
broker-url: tcp://localhost:61616
user: liping
password: liping
packages:
# 配置信任所有的包,这个配置为了支持发送对象消息(如果传递的是对象则需要设置为true,默认是传字符串)
trust-all: true
# 开启主题策略,默认是关闭 开启主题模式
jms:
pub-sub-domain: true
# 配置activemq队列的名称和主题名称
amq:
qname:
queueName-1:
queueName-1
topicName:
name-1:
topic-prot-1
name-2:
topic-prot-2
MQConfig.java 配置类
/**
* 专门配置mq通道的配置类
*/
@Slf4j
@Configuration
public class MQConfig {
@Value("${amq.topicName.name-1}")
private String tpName;
@Bean(name = "queueName")
Queue queueName() {
return new ActiveMQQueue("test_queue");
}
/**
* 主题(发布\订阅模式)通道
* @author kazaf
* @date 2024/4/24 16:43
*/
@Bean(name = "topic1")
Topic queueFind() {
log.info("${amq.topicName.name-1}=" + tpName);
return new ActiveMQTopic("topic-model");
}
@Bean(name = "topic2")
Topic topic2() {
return new ActiveMQTopic("topic-model2");
}
@Bean(name = "topic3")
Topic topic3() {
return new ActiveMQTopic(tpName);
}
}