xp-forge/stomp

STOMP 协议实现

v12.1.0 2024-03-24 13:28 UTC

README

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.0+ Supports PHP 8.0+ Latest Stable Version

关于

STOMP 是一种网络协议,用于与消息代理(如 Apache ActiveMQRabbitMQ)通信。

STOMP 规范可以在 http://stomp.github.io/ 找到。

示例

生产者

消息生产者

use peer\stomp\{Connection, SendableMessage};

$conn= new Connection('stomp://localhost:61613/');
$conn->connect();

$conn->getDestination('/queue/producer')->send(
  new SendableMessage('Message contents', 'text/plain')
);

消费者

简单的消息消费者(订阅者)

use peer\stomp\{Connection, Subscription};

$conn= new Connection('stomp://localhost:61613/');
$conn->connect();

$conn->subscribeTo(new Subscription('/queue/producer', function($message) {
  Console::writeLine('---> Received message: ', $message);
  $message->ack();
}));

$conn->consume();

多端点故障转移

消费者可以通过代理网络连接到任何可用的主机

use peer\stomp\{Connection, Subscription, Failover};

$nodes= ['stomp://one.example.com:61613/', 'stomp://two.example.com:61613/'];

// Connect randomly to one or the other
$conn= new Connection(Failover::using($nodes)->byRandom());
$conn->connect();

$conn->subscribeTo(new Subscription('/queue/producer', function($message) {
  Console::writeLine('---> Received message: ', $message);
  $message->ack();
}));

$conn->consume();

更多示例,请参阅 examples/ 目录。

连接 URL

URL 指定连接的选项和位置

  • protocol 应为 stompstomp+ssl
  • host 是要连接的主机名
  • port 是连接的端口(默认:61613)
  • userpass 可以在 URL 中提供,并用于身份验证
  • 支持参数
    • vhost - 虚拟主机名,自 STOMP 1.1 起支持(例如 ?vhost=example.com
    • versions - 指定支持的版本列表(例如 ?versions=1.0,1.1);默认支持 1.0 和 1.1