This is an old revision of the document!
centos: yum install activemq OSX: brew install apache-activemq
foreground run: activemq console background run: activemq start | stop
web访问:http://localhost:8161/admin config:activemq myConfig.xml
Install the [PHP Stomp client](http://www.php.net/manual/en/book.stomp.php) library.
pecl install stomp
maybe you should add “extension=stomp.so” to php.ini
$user = "guoyi"; $password = "guoyi"; $host = "123.56.201.72"; $port = 61613; $destination = '/queue/qhfm'; $param['phone'] = '13422288866'; $param['code'] = '3761'; $msg['id'] = '123'; $msg['type'] = 'sms'; $msg['time'] = strval(time()); $msg['param'] = $param; $body = json_encode($msg); echo 'send: '.$body.PHP_EOL ; try { $url = 'tcp://'.$host.":".$port; $stomp = new Stomp($url, $user, $password); $stomp->send($destination, $body); } catch(StompException $e) { echo $e->getMessage(); }
class QhMsgRoutine { private $stomp; private $count = 0; function __construct($timeout = 60) { $user = "guoyi"; $password = "guoyi"; $host = "123.56.201.72"; $port = 61613; $destination = '/queue/qhfm'; try { $url = 'tcp://'.$host.":".$port; $stomp = new Stomp($url, $user, $password); $stomp->subscribe($destination); $stomp->setReadTimeout($timeout); // var_dump($stomp->getReadTimeout()); } catch(StompException $e) { echo $e->getMessage(); } } function getMsg() { try { echo "Waiting for messages...\n"; while(true) { $frame = $stomp->readFrame(); if( $frame ) { $stomp->ack($frame); if( $frame->command == "MESSAGE" ) { echo "Receive message: ".$frame->body.PHP_EOL; $msg = json_decode($frame->body); var_dump($msg); return $msg; } else { echo "Unexpected frame.\n"; var_dump($frame); } }else{ echo 'waiting for msg...'; } } } catch(StompException $e) { echo $e->getMessage(); } return null; } function processMsg($msg) { switch ($msg['type']) { case 'sms': # code... break; case 'im': # code... break; default: # code... break; } } } $processer = new QhMsgRoutine(60); while(true){ $processer->processMsg($processer->getMsg()); }