PHP 是用 Stomp 協定,所以要先把Stomp 抓回來。
# wget http://stomp.fusesource.org/release/php/1.0/stomp-php-1.0.0.tar.gz
# tar -zxvf stomp-php-1.0.0.tar.gz
解壓完就有 Stomp 的 library啦。# tar -zxvf stomp-php-1.0.0.tar.gz
接著程式部份,只要 include STOMP 就好了。
下面是 Producer & Consumer 的程式碼。
Producer.php
<?php // include a library require_once("Stomp.php"); // make a connection $con = new Stomp("tcp://localhost:61613"); // connect $con->connect(); // send a message to the queue for ($i=0; $i<10; $i++) { $msg = "test" . $i; $con->send("/queue/test", $msg, array('persistent' => 'true')); echo "Sent message with body $msg \n"; } // disconnect $con->disconnect(); ?>Consumer.php
<?php // include a library require_once("Stomp.php"); // make a connection $con = new Stomp("tcp://localhost:61613"); // connect $con->connect(); // subscribe to the queue $con->subscribe("/queue/test"); while (1) { // receive a message from the queue $msg = $con->readFrame(); // do what you want with the message if ( $msg != null) { echo "Received message with body '$msg->body'\n"; // mark the message as received in the queue $con->ack($msg); } else { echo "Failed to receive a message\n"; } } // disconnect $con->disconnect(); ?>PS.設定 array('persistent' => 'true') ,這樣子才會存在資料庫中。
搞定。
0 comments:
張貼留言