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
,并相應地修改消息生產者和消費者的代碼。