【发布时间】:2014-09-24 23:54:20
【问题描述】:
环境/背景:
使用 PHP Stomp 库从 ActiveMQ (v5.4.3) 发送和接收消息。
步骤:
- 客户端将带有回复和相关 ID 标头的消息发送到请求队列(例如 /queue/request)
- 订阅响应队列(例如 /queue/response)
- 阅读框架
- 确认
- 退订
当没有待处理消息或待处理消息 200 时,消息不会被传递。该过程一直等到超时,最后超时而没有响应。超时后我可以看到消息(使用管理 UI)。这是我用于此案例的代码:
<?php
// make a connection
$con = new Stomp("tcp://localhost:61616");
// Set read timeout.
$con->setReadTimeout(10);
// Prepare request variables.
$correlation_id = rand();
$request_queue = '/queue/com.domain.service.request';
$response_queue = '/queue/com.domain.service.response';
$selector = "JMSCorrelationID='$correlation_id'";
$headers = array('correlation-id' => $correlation_id, 'reply-to' => $response_queue);
$message = '<RequestBody></RequestBody>';
// send a message to the queue.
$con->send($request_queue, $message, $headers);
// subscribe to the queue
$con->subscribe($response_queue, array('selector' => $selector, 'ack' => 'auto'));
// receive a message from the queue
$msg = $con->readFrame();
// do what you want with the message
if ( $msg != null) {
echo "Received message with body\n";
var_dump($msg);
// mark the message as received in the queue
$con->ack($msg);
} else {
echo "Failed to receive a message\n";
}
unset($con);
其他发现:
从一个文件(比如从 sender.php)发送消息并使用另一个脚本(比如 receiver.php)接收工作正常。
允许在同一个请求队列中发送超过 1000 条消息(并最终处理并放入响应队列中)。所以看起来不像是内存问题。
很有趣,在等待超时时,如果我在管理 UI 上浏览队列,我会得到响应。
默认情况下,我使用的 stomp 代理将预取大小设置为 1。
【问题讨论】: