本文主要和大家分享关于php使用activemq的实例,我们分享了实例代码,并做了相关要点的解释,需要的朋友参考下吧。希望能帮助到大家。
使用点对点(point to point)模型
点对点模型特点:
只有一个消费者可以接收到消息
不能重复消费
生产者producer.php代码:
<?php
try {
// 1.建立连接
$stomp = new stomp('tcp://47.52.119.21:61613');
// 2.实例化类
$obj = new stdclass();
// 3.获取数据
for($i=0; $i<3; $i++){
$obj->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代码:
<?php
$stomp = new stomp('tcp://localhost:61613');
$stomp->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代码:
<?php
$stomp = new stomp('tcp://localhost:61613');
$stomp->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代码:
<?php
try {
// 1.建立连接
$stomp = new stomp('tcp://47.52.119.21:61613');
// 2.实例化类
$obj = new stdclass();
// 3.获取数据
for($i = 0; $i < 3; $i++){
$obj->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代码:
<?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);
}
}
消费者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);
}
}
执行结果图如下:
相关推荐:
php如何使用activemq实例分享
java activemq的代码实例分享
activemq中session设置的相关理解
以上就是php如何使用activemq的详细内容。