注意 : 以下的 code 所 include 的 Stomp.php 均客製過, 故傳送 msg 的部份不是標準的格式
php - producer
require_once '../library/Util/Stomp.php';
require_once '../library/Util/Stomp/Exception.php';
require_once '../library/Util/Stomp/Message/Map.php';
//$url = 'failover://(tcp://localhost:61613,tcp://192.168.1.10:61613)';
$url = 'tcp://localhost:61613';
$dest = '/queue/trully_dest';
$wrapper = array(
'media_id' => '1234',
'title' => 'this is title',
'type' => 'Video'
);
$header = array(
'transformation' => 'jms-map-json'
);
$message = new StompMessageMap($wrapper, $header);
try {
$con = new Stomp($url);
$con->connect();
for ($i=0; $i<2; $i++) {
$con->send($dest, $message, array('persistent'=>'true'), false);
}
$con->disconnect();
} catch (StompException $e) {
error_log('StompException: '.$e->getMessage());
}
echo 'done';
執行完 producer 後, msg table 裡便會有資料
php - consumer
require_once '../library/Util/Stomp.php';
require_once '../library/Util/Stomp/Exception.php';
require_once '../library/Util/Stomp/Message/Map.php';
//$url = 'failover://(tcp://localhost:61613,tcp://192.168.1.10:61613)';
$url = 'tcp://localhost:61613';
$dest = '/queue/trully_dest';
try {
$con = new Stomp($url);
$con->connect();
while(true) {
$con->subscribe($dest);
$msg = $con->readFrame();
if (empty($msg)) {
continue;
}
echo $msg;
echo '
';
print_r($msg->headers);
echo '
';
$msgBody = json_decode($msg->body, true);
print_r($msgBody);
//$con->ack($msg->headers['message-id']);
$con->ack($msg);
}
$con->disconnect();
} catch (StompException $e) {
error_log('StompException: '.$e->getMessage());
}
執行完 consumer 後, 剛才 producer 塞的資料會被清除 (因為事情發出去做了)
consumer 的執行結果
echo $msg
MESSAGE
message-id: ID:vlogweb-sit-56155-1317957279937-5:61:-1:1:1
transformation: jms-map-json
destination: /queue/trully_dest
timestamp: 1317959574227
expires: 0
transformation-error: media_id : media_id
priority: 4
{"media_id":"1234","title":"this is title","type":"Video"}
print_r($msg->headers);
Array
(
[message-id] => ID:vlogweb-sit-56155-1317957279937-5:61:-1:1:1
[transformation] => jms-map-json
[destination] => /queue/trully_dest
[timestamp] => 1317959574227
[expires] => 0
[transformation-error] => media_id : media_id
[priority] => 4
)
print_r($msgBody);
Array
(
[media_id] => 1234
[title] => this is title
[type] => Video
)
cf :
http://www.webdeveloperjuice.com/2010/02.....q-and-configuring-it-for-php-application/
http://blog.xuite.net/misgarlic/weblogic/18382603
http://blog.xuite.net/misgarlic/weblogic/18408119
http://fallenempires.net/activemq_stomp/
http://pookey.co.uk/wordpress/archives/75-stomping-with-php-intergration-with-activemq

沒有留言:
張貼留言