Home  >  Article  >  Backend Development  >  How to use ActiveMQ instance sharing with PHP

How to use ActiveMQ instance sharing with PHP

小云云
小云云Original
2018-02-05 16:34:552498browse

Features of the point-to-point model: Only one consumer can receive the message and cannot consume it repeatedly. This article mainly shares with you how to use ActiveMQ in PHP. I hope it can help you.

Producer producer.php code:

username = 'test';        
$obj->password = '123456';        
$queneName     = "/queue/userReg";        
// 4.发送一个注册消息到队列       
 $stomp->send($queneName, json_encode($obj));   
 }} catch (StompException $e) 
{    die('Connection failed: ' 
. $e->getMessage());
}

Consumer 1consumer1.php code:

subscribe('/queue/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:

subscribe('/queue/userReg');while (true) 
{    //判断是否有读取的信息    if ($stomp->hasFrame())
{        $frame = $stomp->readFrame();       
$data  = json_decode($frame->body, true);      
  var_dump($data);        $stomp->ack($frame);  
  }}

Execution result The picture is as follows:

How to use ActiveMQ instance sharing with PHP

Use the Publish/Subscribe (Publish Subscribe) model

Publish/Subscribe model features:

Multiple Consumers can all receive messages

Can be consumed repeatedly

Producer producer.php code:

{    // 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:

subscribe('/topic/userReg');
while (true) {    //判断是否有读取的信息    if ($stomp->hasFrame())
 {        $frame = $stomp->readFrame();       
 $data  = json_decode($frame->body, true);    
    var_dump($data);        $stomp->ack($frame);  
  }}

Consumer2consumer2.php code:

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:

How to use ActiveMQ instance sharing with PHP

##Related recommendations:

Related understanding of Session settings in ActiveMQ

php-activemq ajax call configuration issue

The above is the detailed content of How to use ActiveMQ instance sharing with PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn