Blogger Information
Blog 142
fans 5
comment 0
visits 130504
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP使用ActiveMQ实例
php开发大牛
Original
1560 people have browsed it

使用点对点(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);
 }
}


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post