以 ActiveMQ 为中间件做提醒

客服系统有个需求,就是右下角需要实时出现客服创建的提醒,比如几点几点给某某客户回拨电话。看了下之前系统的实现,客户端每分钟以 stomp 读取 ActiveMQ 中属于登录客服的信息。下面就把安装、配置及简单 demo 演示下

ActiveMQ 是 Java 编写的,因此需要安装 Java

yum -y list java*
yum -y install java-1.8.0-openjdk*

安装 ActiveMQ

cd /usr/local
wget https://archive.apache.org/dist/activemq/5.12.1/apache-activemq-5.12.1-bin.tar.gz
tar -xf apache-activemq-5.12.1-bin.tar.gz
chmod -R 755 apache-activemq-5.12.1

# 启动
/usr/local/apache-activemq-5.12.1/bin/activemq start
#启动成功后可以通过 web 方式访问,默认端口为 8161,账号密码为 admin/admin

安装 stomp 扩展

cd /usr/local
wget http://pecl.php.net/get/stomp-1.0.5.tgz
tar zxvf stomp-1.0.5.tgz
cd stomp-1.0.5
phpize
./configure
make
make install
echo "extension = stomp.so" >> /etc/php.d/php-stomp.ini
service php-fpm restart

数据写入 ActiveMQ

<?php

$notice = new stdClass();
$notice->reply_user_id=2;
$notice->content=8901;
$notice->type='remind';
$notice->mark=1;
$notice->sender='';
$notice->deal_url='';
$notice_str = json_encode($notice);

$raw_time = '2018-07-18 17:16:01';
$header = array('show_time'=>strtotime($raw_time),'receiver'=>2,'mark'=>1,'raw_time' => $raw_time);

try
{
    $stomp = new Stomp("tcp://127.0.0.1:61612");
    $stomp->send('/queue/msg', $notice_str, $header);
    unset($stomp);
}
catch(StompException $e)
{
    echo $e->getMessage();
}

实时读取 ActiveMQ

<?php

$stomp = new Stomp('tcp://127.0.0.1:61612');
$stomp->subscribe('/queue/msg');


while(true) {
    $stomp->setReadTimeout(30);
    $now= time()+60;
    $stomp->subscribe('/queue/msg', array('selector'=>"receiver=2 AND show_time<=$now"));
    $notice_amq = $stomp->readFrame();
    $stomp->ack($notice_amq); // 删除此 mq 消息
    var_dump($notice_amq);
    unset($stomp);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值