1. 安装 ActiveMQ
1.1 下载 ActiveMQ
访问 ActiveMQ 官方下载页面,根据你的操作系统选择合适的版本进行下载。这里以 Linux 系统,Java环境1.8版本
为例,下载 apache-activemq-5.16.7-bin.tar.gz
。
1.2 解压文件
将下载的压缩包解压到指定目录,例如 /opt
:
tar -zxvf apache-activemq-5.16.7-bin.tar.gz -C /opt
1.3 启动 ActiveMQ
进入解压后的目录,启动 ActiveMQ:
cd /opt/apache-activemq-5.16.7
./bin/activemq start
1.4 验证 ActiveMQ 是否启动成功
打开浏览器,访问 http://localhost:8161
,使用默认用户名 admin
和密码 admin
登录 ActiveMQ 的管理控制台。如果能成功登录,说明 ActiveMQ 已经启动成功。
1.4 进入ActiveMQ 管理员控制台
ActiveMQ 启动成功后,单击 Manage ActiveMQ broker 超链接进入管理员控制台。
2. 创建 Spring Boot 项目并整合 JMS - ActiveMQ
2.1 添加依赖
在 pom.xml
中添加 Spring Boot 集成 ActiveMQ 的依赖:
<dependencies><!-- Spring Boot Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring Boot JMS --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-activemq</artifactId></dependency>
</dependencies>
2.2 配置 ActiveMQ
在 application.properties
中配置 ActiveMQ 的连接信息:
server.port=8080
# ActiveMQ 服务器地址,默认端口 61616
spring.activemq.broker-url=tcp://localhost:61616
# 配置信任所有的包,这个配置是为了支持发送对象消息
spring.activemq.packages.trust-all=true
# ActiveMQ 用户名
spring.activemq.user=admin
# ActiveMQ 密码
spring.activemq.password=admin
2.3 创建消息生产者
创建一个消息生产者类,用于发送消息到 ActiveMQ 的队列:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;import javax.jms.Queue;@Service
public class JmsProducer {@Autowiredprivate JmsTemplate jmsTemplate;@Autowiredprivate Queue queue;public void sendMessage(String message) {jmsTemplate.convertAndSend(queue, message);System.out.println("Sent message: " + message);}
}
2.4 创建消息消费者
创建一个消息消费者类,用于接收 ActiveMQ 队列中的消息:
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;@Component
public class JmsConsumer {@JmsListener(destination = "test-queue")public void receiveMessage(String message) {System.out.println("Received message: " + message);}
}
2.5 配置 Queue Bean
在 ActiveMqConfig.java 中定义 队列:
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.jms.Queue;@Configuration
public class ActiveMqConfig {@Beanpublic Queue queue() {return new ActiveMQQueue("test-queue");}
}
2.6 创建 API 测试发送消息
import com.weigang.producer.JmsProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/message")
public class MessageController {@Autowiredprivate JmsProducer producer;@GetMapping("/send")public String sendMessage(@RequestParam String msg) {producer.sendMessage(msg);return "Message sent: " + msg;}
}
然后访问:http://localhost:8080/message/send?msg=HelloActiveMQ
控制台应输出:
Sent message: HelloActiveMQ
Received message: HelloActiveMQ
3. 使用 ActiveMQ Web 界面查看消息
访问 http://localhost:8161/admin/queues.jsp
,可以看到 test-queue 队列以及发送的消息。
4. 发送对象消息
在 JmsProducer 发送 对象:
import com.weigang.model.CustomMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;import javax.jms.Queue;@Service
public class JmsProducer {@Autowiredprivate JmsTemplate jmsTemplate;// 仍然使用 test-queue@Autowiredprivate Queue queue;public void sendMessage(CustomMessage customMessage) {jmsTemplate.convertAndSend(queue, customMessage);System.out.println("Sent message----> id:" + customMessage.getId() + ",content:" + customMessage.getContent());}
}
创建消息对象:
import java.io.Serializable;public class CustomMessage implements Serializable {private static final long serialVersionUID = 1L; // 推荐添加,避免序列化问题private String content;private int id;// 必须有默认构造方法(JMS 反序列化需要)public CustomMessage() {}// 构造方法public CustomMessage(String content, int id) {this.content = content;this.id = id;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public int getId() {return id;}public void setId(int id) {this.id = id;}@Overridepublic String toString() {return "CustomMessage{" +"content='" + content + '\'' +", id=" + id +'}';}}
消费者处理对象消息:
import com.weigang.model.CustomMessage;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;@Component
public class JmsConsumer {@JmsListener(destination = "test-queue")public void receiveMessage(String message) {System.out.println("Received message: " + message);}@JmsListener(destination = "test-queue")public void receiveMessage(CustomMessage message) {System.out.println("Received object message: " + message.toString());}
}
通过 API 发送对象:
import com.weigang.model.CustomMessage;
import com.weigang.producer.JmsProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/message")
public class MessageController {@Autowiredprivate JmsProducer producer;@GetMapping("/send")public String sendMessage(@RequestParam String msg) {producer.sendMessage(msg);return "Message sent: " + msg;}@GetMapping("/sendObj")public String sendMessage(@RequestParam Integer id,@RequestParam String content) {CustomMessage customMessage = new CustomMessage(content, id);producer.sendMessage(customMessage);return "Message sent: " + customMessage;}
}
然后访问:http://localhost:8080/message/sendObj?id=1&content=HelloActiveMQ
控制台应输出:
Sent message----> id:1,content:HelloActiveMQ
Received object message: CustomMessage{content='HelloActiveMQ', id=1}
注意事项
- 确保 ActiveMQ 服务器正常运行,并且
application.properties
中的连接信息正确。 - 如果需要使用主题(Topic)进行消息传递,可以在配置中设置
spring.jms.pub-sub-domain=true
,并相应地修改消息生产者和消费者的代码。