LoginSignup
5
5

More than 5 years have passed since last update.

ZendQueueとKestrelでメッセージキューサーバーを体験

Posted at

Kestrel

Scalaで書かれたメッセージキューサーバー。Twitterで使われてるらしいです。

Twitterで使っているScalaで書かれたオープンソースのメッセージキューサーバー、Kestrel :侍ズム#samuraism

インストールと起動

$ curl -O http://robey.github.com/kestrel/download/kestrel-2.4.1.zip
$ unzip kestrel-2.4.1.zip
$ cd kestrel-2.4.1
$ sudo java -jar kestrel_2.9.2-2.4.1.jar

ZendQueue

Zend Frameworkのコンポーネントの1つで、メッセージキューを利用するために使います。  

GitHub
https://github.com/zendframework/ZendQueue

メッセージを格納する方法によって複数のアダプタが用意されています。

Kestrel用のアダプタはありませんが、Kestrelはmemcachedプロトコルをサポートしているので、MemcacheQアダプタを利用します。

Memcache

あらかじめMemcachedライブラリもインストールしておいて下さい。

Macの場合はHomebrewを使うと簡単にインストールできます♪

$ brew install memcached
$ brew install memcache-php

メッセージキューサーバーを体験

2つのスクリプトを用意してください。

・worker.php : ワーカープロセス。キューからメッセージを取得して表示する。

<?php
// worker.php
$options = array(
  'name' => 'ack_queue',
    'driverOptions' => array(
        'host' => 'localhost',
        'port' => '22133',
        ),
    );

$queue = new ZendQueue\Queue('MemcacheQ', $options);

for (;;) {
    $messages = $queue->receive(1);
    foreach ($messages as $mess) if ($mess->body) echo $mess->body . PHP_EOL;
}

・front.php : キューにメッセージを送信する。

<?php
// front.php
$options = array(
  'name' => 'ack_queue',
    'driverOptions' => array(
        'host' => 'localhost',
        'port' => '22133',
        ),
    );

$queue = new ZendQueue\Queue('MemcacheQ', $options);
$queue->send('Hello, World!');

ターミナルを2つたちあげてください。

・ターミナル1でworker.phpを実行

プロンプトが返ってこない → Kestrelのキューを監視してくれています。

・ターミナル2でfront.phpを実行すると…

ターミナル1に「Hello, World!」と表示されます!

簡単ですが以上です。

Hello, Worldが表示された時には感動しますね (・∀・)

worker.phpを実行するターミナルを増やしたりするとなお楽しくなってきます♪

参考

Twitterで使っているScalaで書かれたオープンソースのメッセージキューサーバー、Kestrel :侍ズム#samuraism

PHP+Kestrel+Supervisorでお手軽タスクキューイング :アシアルブログ

PHPアプリでメッセージキューサーバを活用する - Zend_Queue - :CodeZine

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5