Home >Backend Development >PHP Tutorial >PHP uses ActiveMQ case analysis (with code)
This time I will bring you a case analysis of PHP using ActiveMQ (with code). What are the precautions for PHP using ActiveMQ? The following is a practical case, let's take a look.
Use Point To PointModel
Features of point-to-point model:
Only one consumer can receive the message
Cannot be consumed repeatedly
producer producer.php code:
<?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()); }
consumer1consumer1 .php code:
<?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); } }
Consumer2consumer2.php code:
<?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); } }
The execution result is as follows:
##Use publish/subscribe ( Publish Subscribe) modelPublish/subscribe model features:Multiple consumers can receive messagesCan be consumed repeatedly
Producer producer.php code:
<?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()); }Consumer 1consumer1.php code:
<?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); } }Consumer 2consumer2.php code:
?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); } }The execution result is as follows: I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
Detailed explanation of the steps to pass parameters when opening a local exe application with php and js
How to achieve php deletion fixation Folders and files under the path
The above is the detailed content of PHP uses ActiveMQ case analysis (with code). For more information, please follow other related articles on the PHP Chinese website!