问题描述
总结
我正在尝试为我的消费者设置心跳,以便它可以检测到断开的连接 - 例如,如果代理重新启动,则它可能会重新连接到故障转移。
我使用在本地运行的消费者/调度程序和在 AWS 中运行的队列测试了代码,一切正常。但是,当将代码移动到 AWS 时,消费者会与服务器/代理设置心跳,但服务器/代理从未发送过心跳或客户端/消费者从未收到过心跳。因此,一旦请求的服务器心跳间隔一过,就会抛出 HeartbeatException
。
我的代码基于 stomp-PHP-examples github 中的示例
- 回购:https://github.com/stomp-php/stomp-php-examples
- 心跳示例:https://github.com/stomp-php/stomp-php-examples/blob/support/version-4/src/heartbeats_server.php
我的下一个最佳猜测是为什么这不起作用与队列配置有关,因为我使用的是 AWS 提供的默认配置(我认为)。我已经在谷歌上搜索并搜索了有关心跳的配置设置,但还没有深入,因为这是一个新主题。任何帮助将不胜感激!
设置
- 亚马逊 MQ (ActiveMQ 5.15.14)
- stomp-php 5.0(截至今天的最新版本)
我很乐意提供有关我的设置的更多详细信息。
代码
消费者(精简版)
abstract class AmqConsumerAbstract
{
/** @var StatefulStomp */
protected $consumer;
const HEARTBEAT = 5000;
public function listen(): void
{
$observer = new ServerAliveObserver();
$this->client->getConnection()->getobservers()->addobserver($observer);
$this->client->setHeartbeat(0,self::HEARTBEAT); // heartbeats setup here
// Note: the heartbeat above is longer than the read-timeout below
$this->client->getConnection()->setReadTimeout(2,0);
$this->client->connect();
$this->consumer = new StatefulStomp($this->client);
$this->consumer->subscribe(
$this->getQueueName(),null,'client'
);
if (!$observer->isEnabled()) {
// we never get here so I assume everything is working OK
echo 'The Server is not supporting heartbeats.');
exit(1);
} else {
echo sprintf('The Server should send us signals every %d ms.',$observer->getInterval() * 1000);
}
try {
while (true) {
$frame = $this->consumer->read(); // I assumed this line would read the heartbeat?
// there is then some logic that deals with payload and does
// $this->consumer->begin();
// $this->consumer->ack($frame);
// $this->consumer->commit();
if ($observer->isDelayed()) {
$this->echoLog('ServerAliveObserver: Server has been delayed.');
}
}
} catch (HeartbeatException $e) {
echo 'AMQ (STOMP) Error,the server Failed to send us heartbeats within the defined interval: ' . $e->getMessage()
$this->reconnect();
} catch (ConnectionException $e) {
echo $e->getMessage();
$this->reconnect();
} catch (Exception $e) {
echo 'AMQ (STOMP) Queue error: ' . $e->getMessage();
exit(1);
}
}
}
我已经在版本为 5.15.14 的单实例代理上进行了复制。
我已尝试打开 STOMP debugging for the broker,但似乎不允许 uri
下的 <transportConnector>
属性。配置将保存但会提取 uri
属性。
<transportConnector>
Elements and Their Attributes Permitted in Amazon MQ Configurations
Working with Spring XML configuration files (xsd files)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)