PHP uses ActiveMQ to send and process messages

Time:2021-6-9

In this paper, an example of PHP using ActiveMQ to send and process messages is described. The details are as follows:

Let’s take a simple user registration as an example. When the user clicks the registration button, we send a message, which is received by PHP in the background and then processed.

1. PHP code is as follows:

<?php
$stomp = new Stomp('tcp://192.168.1.222:61613');

$obj = new Stdclass();
//The following data is actually sent by the user through the front-end page post. Here is just a demonstration
$obj->username = 'test';
$obj->password = '123456';
//Send a registration message to the queue. Here we simulate user registration
$stomp->send('/queue/userReg', json_encode($obj));

2. PHP code is as follows:

<?php
$stomp = new Stomp('tcp://192.168.1.222:61613');
//The subscription is only valid for one script. If multiple scripts are started, only one will receive a message
$stomp->subscribe('/queue/userReg');

while(true) {
 //Judge whether there is information read
 if($stomp->hasFrame()) {
  $frame = $stomp->readFrame();

  $data = json_decode($frame->body, true);
  var_dump($data);

  //We get data from
  //Process the corresponding logic, such as storing in the database, sending verification code and a series of operations.
  //$db->query("insert into user values('{$username}','{$password}')");
  //sendVerify();

  //Indicates that the message has been processed. The ack() function is very important
  $stomp->ack($frame);
 }
 sleep(1);
}

Run the above two script files respectively


> /data/php56/bin/php 1.php
> /data/php56/bin/php 2.php

We can also divide the above 2. PHP code into multiple steps.

2. PHP code is as follows:

<?php
$stomp = new Stomp('tcp://192.168.1.222:61613');
$stomp->subscribe('/queue/userReg');

while(true) {
 //Judge whether there is information read
 if($stomp->hasFrame()) {
  $frame = $stomp->readFrame();

  $data = json_decode($frame->body, true);

  //Registration information storage
  //$ret = db->query("insert into user values('{$data['username']}', '{$data['password']}')");
  //The demo here is set to true
  $ret = true;
  if($ret) {
   Echo $data ['user name '],'store successfully', PHP_ EOL;
   //If the storage is successful, the data will be sent to another message queue for further processing
   $stomp->send('/queue/sendVerify', $frame->body);

   $stomp->ack($frame);
  }
 }
 sleep(1);
}

3. PHP code is as follows:

<?php
$stomp = new Stomp('tcp://192.168.1.222:61613');
$stomp->subscribe('/queue/sendVerify');

while(true) {
 //Judge whether there is information read
 if($stomp->hasFrame()) {
  $frame = $stomp->readFrame();

  $data = json_decode($frame->body, true);

  //$RET = sendverify() sends the verification code. In fact, it should request an interface
  $ret = true;
  if($ret) {
   Echo $data ['username '],'Send verification code successfully', PHP_ EOL;

   $stomp->ack($frame);
  }
 }
 sleep(1);
}

Run the above three scripts again


> /data/php56/bin/php 1.php
> /data/php56/bin/php 2.php
> /data/php56/bin/php 3.php

For more information about PHP, readers interested in it can see the following topics: PHP extension development tutorial, PHP network programming skills summary, PHP curl usage summary, PHP array operation skills encyclopedia, PHP data structure and algorithm tutorial, PHP programming algorithm summary and PHP string Usage Summary

I hope this article is helpful for PHP programming.