本文主要和大家分享关于php使用activemq的实例,我们分享了实例代码,并做了相关要点的解释,需要的朋友参考下吧。希望能帮助到大家。
使用点对点(Point To Point)模型
点对点模型特点:
只有一个消费者可以接收到消息
-
不能重复消费
立即学习“PHP免费学习笔记(深入)”;
生产者producer.php代码:
username = 'test';
$obj->password = '123456';
$queneName = "/queue/userReg";
// 4.发送一个注册消息到队列
$stomp->send($queneName, json_encode($obj));
}
} catch (StompException $e) {
die('Connection failed: ' . $e->getMessage());
}消费者1consumer1.php代码:
subscribe('/queue/userReg');
while (true) {
//判断是否有读取的信息
if ($stomp->hasFrame()) {
$frame = $stomp->readFrame();
$data = json_decode($frame->body, true);
var_dump($data);
$stomp->ack($frame);
}
}消费者2consumer2.php代码:
subscribe('/queue/userReg');
while (true) {
//判断是否有读取的信息
if ($stomp->hasFrame()) {
$frame = $stomp->readFrame();
$data = json_decode($frame->body, true);
var_dump($data);
$stomp->ack($frame);
}
}执行结果图如下:

使用发布/订阅(Publish Subscribe)模型
发布/订阅模型特点:
多个消费者都可以收到消息
能重复消费
生产者producer.php代码:
username = 'test';
$obj->password = '123456';
$queneName = "/topic/userReg";
// 4.发送一个注册消息到队列
$stomp->send($queneName, json_encode($obj));
}
} catch (StompException $e) {
die('Connection failed: ' . $e->getMessage());
}消费者1consumer1.php代码:
subscribe('/topic/userReg');
while (true) {
//判断是否有读取的信息
if ($stomp->hasFrame()) {
$frame = $stomp->readFrame();
$data = json_decode($frame->body, true);
var_dump($data);
$stomp->ack($frame);
}
}消费者2consumer2.php代码:
?php
$stomp = new Stomp('tcp://localhost:61613');
$stomp->subscribe('/topic/userReg');
while (true) {
//判断是否有读取的信息
if ($stomp->hasFrame()) {
$frame = $stomp->readFrame();
$data = json_decode($frame->body, true);
var_dump($data);
$stomp->ack($frame);
}
}执行结果图如下:

相关推荐:











