原文:http://www.dearda.com/index.php/archives/1370
1、先安装php扩展stomp
#cd /usr/local/src
#wget http://pecl.php.net/get/stomp-1.0.9.tgz
#tar zxvf stomp-1.0.9.tgz
#cd stomp-1.0.9
#/usr/local/php/bin/phpize
#./configure --enable-stomp --with-php-config=/usr/local/php/bin/php-config
#make
#make install
修改php配置文件php.ini添加刚装好的stomp
#vim /usr/local/php/etc/php.ini
添加 extension=stomp.so
重启php
2、生产者publisher.php(每秒发送一次当前时间)
<?php
$queue = '/topic/phptest';
$msg = 'bar';
try {
$stomp = new Stomp('tcp://localhost:61613');
while (true) {
$stomp->send($queue, $msg." ". date("Y-m-d H:i:s"));
sleep(1);
}
} catch(StompException $e) {
die('Connection failed: ' . $e->getMessage());
}
3、消费者consumer.php
<?php
$queue = '/topic/phptest';
try {
$stomp = new Stomp('tcp://localhost:61613');
$stomp->subscribe($queue);
while (true) {
if ($stomp->hasFrame()) {
$frame = $stomp->readFrame();
if ($frame != NULL) {
print "Received: " . $frame->body . " - time now is " . date("Y-m-d H:i:s"). "\n";
$stomp->ack($frame);
}
} else {
print "No frames to read\n";
}
}
} catch(StompException $e) {
die('Connection failed: ' . $e->getMessage());
}
4、通过执行 php producer.php 和 php consumer.php来进行mq消息存取。结果如图