跳至主要内容

🧣 ActiveMQ 通訊協議

ActiveMQ Classic 同時支援多種傳輸協議,讓 Java、Python、瀏覽器與 IoT 裝置都能以各自擅長的方式連接同一個 Broker。選對協議,是整合異質系統的第一步。

協議適用場景對照

協議Port適用場景典型客戶端
OpenWire (TCP)61616Java JMS 應用ActiveMQ Java Client
AMQP5672跨語言企業整合RabbitMQ 相容客戶端
STOMP61613Web 前端、腳本stomp.js、Python stomp
MQTT1883IoT、低頻寬裝置Eclipse Paho
WebSocket61614瀏覽器即時通訊stomp.js over WS

客戶端用法參見 stompMqttClient

官方預設通訊協議

  • 檔案: /conf/activemq.xml
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB-->
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
</transportConnectors>

傳輸協議

TCP

TCP (Transmission Control Protocol)

  • 預設 port 為 61616
  • 在預設的情況下,被命名為 openwire
  • 連線字串的組成為 tcp://ip:port?key=value
  • TCP 優點
    • TCP 協議為保證連線
    • 效率高,socket 本身有 retry 機制
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB-->
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
</transportConnectors>

AMQP

AMQP (Advanced Message Queuing Protocol)

  • 預設 port 為 5672
  • 在預設的情況下,AMQP 連線被命名為 amqp
  • 連線字串的組成為 amqp://ip:port?key=value
  • AMQP 優點
    • 支援多種客戶端和伺服器,使其成為一個極具靈活性的協議
    • 提供高度可靠的訊息交付機制,適合需要確保訊息不會丟失的應用程序
    • 能夠處理大量訊息,使其適用於高流量環境
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB-->
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
</transportConnectors>

STOMP

STOMP (Streaming Text Oriented Messaging Protocol)

  • 預設 port 為 61613
  • 在預設的情況下,STOMP 連線被命名為 stomp
  • 連線字串的組成為 stomp://ip:port?key=value
  • STOMP 優點
    • 提供輕量級的訊息傳遞協議,適合實時通訊和流數據處理
    • 易於實現並與多種客戶端和伺服器庫兼容
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB-->
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
</transportConnectors>

MQTT

MQTT (Message Queuing Telemetry Transport)

  • 預設 port 為 1883
  • 在預設的情況下,MQTT 連線被命名為 mqtt
  • 連線字串的組成為 mqtt://ip:port?key=value
  • MQTT 優點
    • 非常適合 IOT 設備,因為它是一個輕量級協議,節省網路和資源
    • 提供發布/訂閱和請求/回應訊息傳遞模式
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB-->
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
</transportConnectors>

WebSocket

  • 預設 port 為 61614
  • 在預設的情況下,WebSocket 連線被命名為 ws
  • 連線字串的組成為 ws://ip:port?key=value
  • WebSocket 優點
    • 允許伺服器和客戶端之間的即時雙向通信
    • 適用於互動性應用程序,如即時聊天、在線遊戲等
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB-->
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
</transportConnectors>