本文实例讲述了php使用activemq实现消息队列的方法。分享给大家供大家参考,具体如下:

前面我们已经学了如何部署activemq,
我们知道通过activemq的一个管理后台可以查看任务队列。

今天

用php来操作activemq,我们可以借助一个第三方扩展。
下载:

composer require fusesource/stomp-php:2.0.*

然后新建test.php:

<?php

require __dir__.'/vendor/autoload.php'; //引入自动加载的文件

$connect = new \fusesource\stomp\stomp('tcp://10.211.55.13/:61613');
$connect->connect();

$userid = 1001;
$result = $connect->send('email',$userid); //比如发邮件
var_dump($result);

发送消息成功,打印bool(true)

我们在activemq自带的管理后台查看,确实有一个名为”email”的队列。

上面我们发送的一个id,我们还可以发送json数据。

$data = array('id'=>1001,'email'=>'110@qq.com','content'=>'test');
$result = $connect->send('email',json_encode($data)); 

我们在mq后台可以查看消息详细

上面的代码到这里,还不够完美。如果我们服务器重启了activemq,没有处理的消息会丢失。
这个时候我们需要用到send()方法的第三个参数。

//消息持久化 persistent为true,字符串的'true'
$result = $connect->send('email',json_encode($data),array('persistent'=>'true'));

前面我们完成了『发送』

给mq服务器发送消息(email消息)。
那么在mq的队列中的任务,又是怎么处理的呢?

<?php

require __dir__.'/vendor/autoload.php'; //引入自动加载的文件

$connect = new \fusesource\stomp\stomp('tcp://10.211.55.13/:61613');
$connect->connect();

//订阅队列消息
$connect->subscribe('email');

if ($connect->hasframetoread()){
  $frame = $connect->readframe();
  print_r($frame);
}

在mq服务端,订阅(监听)队列消息。
在服务端是命令行下执行:php mqserver.php
如果有没有处理的消息,可以读取出来,打印结果如下:

fusesource\stomp\frame object
(
  [command] => message
  [headers] => array
    (
      [expires] => 0
      [destination] => /queue/email
      [priority] => 4
      [message-id] => id:localhost.localdomain-38488-1488196907415-3:2:-1:1:1
      [timestamp] => 1489477647931
    )

  [body] => {"id":1001,"email":"110@qq.com","content":"test"}
)

body就把我们发送的内容读取出来了。

我们循环读取(死循环)一直等待新消息:

do{
  if ($connect->hasframetoread()){
    $frame = $connect->readframe();
    print_r($frame->body);
  }
} while (true);

处理消息之后(在发送邮件等业务完成之后),要通知mq我处理了该条消息了

  if ($connect->hasframetoread()){
    $frame = $connect->readframe();
    //print_r($frame->body);

    //做业务逻辑
    //具体发送邮件的业务
    //send email

    //最后通知mq,我们已经处理了该条消息
    $connect->ack($frame);
  }

我们还可以在优化一下代码,解决死循环,控制循环(这里是一种方案演示)

do{
  //会等待,直到有可用消息,才执行后面代码
  if ($connect->hasframetoread()){
    $frame = $connect->readframe();
    //print_r($frame->body);

    //做业务逻辑
    //具体发送邮件的业务
    //send email
    sleep(2); //模拟延时

    //最后通知mq,我们已经处理了该条消息
    $connect->ack($frame);
  }

  //控制循环
  $next = true;
  if (file_exists(__dir__.'/stop')){
    //如果有名为stop的一个文件
    //就不循环了
    $next = false;
  }
} while ($next);

更多关于php相关内容感兴趣的读者可查看本站专题:《php数据结构与算法教程》、《php程序设计算法总结》、《php字符串(string)用法总结》、《php数组(array)操作技巧大全》、《php常用遍历算法与技巧总结》及《php数学运算技巧总结》

希望本文所述对大家php程序设计有所帮助。