1、先安裝php擴展stomp (針對php7)
#cd /usr/local/src
#wget http://pecl.php.net/get/stomp-2.0.1.tgz
#tar zxvf stomp-2.0.1.tgz
#cd stomp-2.0.1
#/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消息存取。結果如圖