A Php Client for Apache RocketMQ.
- install rocketmq-client-cpp; (dist/Dockerfile is a example to build a librocketmq.so file)
- do shell:
cp bin/librocketmq.so /usr/lib/;
mkdir /usr/include/rocketmq/ ;
cp include/* /usr/include/rocketmq/
- install php-cpp
- download rocketmq-client-php
git clone https://github.com/lpflpf/rocketmq-client-php;
cd rocketmq-client-php;
make && make install
- update php.ini file, add line
extension=rocketmq.so
; - try to run example in example directory.
to see autocompelete file.
namespace RocketMQ;
$instanceName = "MessageQueue";
$producer = new Producer($instanceName);
$producer->setInstanceName($instanceName);
$producer->setNamesrvAddr("127.0.0.1:9876");
$producer->start();
for ($i = 0; $i < 10000; $i ++){
$message = new Message("TopicTest", "*", "hello world $i");
$sendResult = $producer->send($message);
echo $sendResult->getSendStatus() . "\n";
}
It is a good idea to save offset in local.
namespace RocketMQ;
$consumer = new PullConsumer("pullTestGroup");
$consumer->setInstanceName("testGroup");
$consumer->setTopic("TopicTest");
$consumer->setNamesrvAddr("127.0.0.1:9876");
$consumer->start();
$queues = $consumer->getQueues();
foreach($queues as $queue){
$newMsg = true;
$offset = 0;
while($newMsg){
$pullResult = $consumer->pull($queue, "*", $offset, 8);
switch ($pullResult->getPullStatus()){
case PullStatus::FOUND:
foreach($pullResult as $key => $val){
echo $val->getMessage()->getBody() . "\n";
}
$offset += count($pullResult);
break;
default:
$newMsg = false;
break;
}
}
}
namespace RocketMQ;
$consumer = new PushConsumer("testGroup");
$consumer->setInstanceName("testGroup");
$consumer->setNamesrvAddr("127.0.0.1:9876");
$consumer->setThreadCount(10);
$consumer->setListenerType(MessageListenerType::LISTENER_ORDERLY);
$count = 0;
$consumer->setCallback(function ($msg) use (&$count){
echo $msg->getMessage()->getBody() . "\n";
$count ++;
});
$consumer->subscribe("TopicTest", "*");
$consumer->start();
$consumer->shutdown();
- Build rocketmq-client-cpp Container.
cd dist
docker build -t rocketmq-client-cpp:1.2.2 ./dist
- Build rocketmq-client-php Container
docker build -t rocketmq-client-php:1.0.0 .
- Manual commit an offset.
- Log handle. ( specify log file is no support by rocketmq-client-cpp. )
- Doc.