Home  >  Article  >  Backend Development  >  How PHP uses ActiveMQ

How PHP uses ActiveMQ

小云云
小云云Original
2018-02-07 09:06:553323browse

This article mainly shares with you examples of using ActiveMQ with PHP. We have shared the example code and explained the relevant points. Friends in need can refer to it. Hope it helps everyone.

Use Point To Point model

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(&#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());
}

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);
  }
}

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);
  }
}

Execution The result diagram is as follows:

Use the publish/subscribe (Publish Subscribe) model

Features of the publish/subscribe model:

Multiple consumers Can receive messages
Can be consumed repeatedly
Producer producer.php code:


<?php
try {
  // 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());
}

Consumer1consumer1.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);
  }
}

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);
  }
}

The execution result diagram is as follows:

Related recommendations:

How to use ActiveMQ in PHP to share examples

Java ActiveMQ code examples to share

Related understanding of Session settings in ActiveMQ

The above is the detailed content of How PHP uses ActiveMQ. 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