Home Backend Development PHP Tutorial How to use ActiveMQ instance sharing with PHP

How to use ActiveMQ instance sharing with PHP

Feb 05, 2018 pm 04:34 PM
activemq php share

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:

<?
phptry {  
  // 1.建立连接    $stomp = new Stomp(&#39;tcp://47.52.119.21:61613&#39;);    
// 2.实例化类    $obj = new Stdclass();    
// 3.获取数据    for($i=0; $i<3; $i++)
{        $obj->username = &#39;test&#39;;        
$obj->password = &#39;123456&#39;;        
$queneName     = "/queue/userReg";        
// 4.发送一个注册消息到队列       
 $stomp->send($queneName, json_encode($obj));   
 }} catch (StompException $e) 
{    die(&#39;Connection failed: &#39; 
. $e->getMessage());
}
Copy after login

Consumer 1consumer1.php code:

<?
php$stomp = new Stomp(&#39;tcp://localhost:61613&#39;);
$stomp->subscribe(&#39;/queue/userReg&#39;);while (true) 
{    //判断是否有读取的信息    if ($stomp->hasFrame()) 
{        $frame = $stomp->readFrame();        
$data  = json_decode($frame->body, true);       
var_dump($data);       
 $stomp->ack($frame);   
 }}
Copy after login

Consumer 2consumer2.php code:

<?php
$stomp = new Stomp(&#39;tcp://localhost:61613&#39;);$stomp->subscribe(&#39;/queue/userReg&#39;);while (true) 
{    //判断是否有读取的信息    if ($stomp->hasFrame())
{        $frame = $stomp->readFrame();       
$data  = json_decode($frame->body, true);      
  var_dump($data);        $stomp->ack($frame);  
  }}
Copy after login

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(&#39;tcp://47.52.119.21:61613&#39;);  
 // 2.实例化类    $obj = new Stdclass();   
 // 3.获取数据    for($i = 0; $i < 3; $i++)
{        $obj->username = &#39;test&#39;;      
  $obj->password = &#39;123456&#39;;        
$queneName     = "/topic/userReg";      
  // 4.发送一个注册消息到队列     
   $stomp->send($queneName, json_encode($obj));   
 }} 
catch (StompException $e)
 {    die(&#39;Connection failed: &#39; . $e->getMessage());
}
Copy after login

Consumer 1consumer1. PHP code:

<?php$stomp = new Stomp(&#39;tcp://localhost:61613&#39;);$stomp->subscribe(&#39;/topic/userReg&#39;);
while (true) {    //判断是否有读取的信息    if ($stomp->hasFrame())
 {        $frame = $stomp->readFrame();       
 $data  = json_decode($frame->body, true);    
    var_dump($data);        $stomp->ack($frame);  
  }}
Copy after login

Consumer2consumer2.php code:

<?php$stomp = new Stomp(&#39;tcp://localhost:61613&#39;);$stomp->subscribe(&#39;/topic/userReg&#39;);
while (true) {    //判断是否有读取的信息    if ($stomp->hasFrame()) 
{        $frame = $stomp->readFrame();    
    $data  = json_decode($frame->body, true);       
 var_dump($data);      
  $stomp->ack($frame);   
 }}
Copy after login

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 of this Website
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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles