Zend_Queueのカスタマイズ

独自のアダプタ作成

Zend_Queueは、 Zend_Queue_Adapter_AdapterAbstractを実装するどんなアダプタも扱います。 既存のアダプタ、 または抽象クラスZend_Queue_Adapter_AdapterAbstractのうちの1つを拡張することにより、 独自のアダプタを作成できます。 このアダプタとしてZend_Queue_Adapter_Arrayを検討することが、 最も簡単に概念化できると提案します。

class Custom_DbForUpdate extends Zend_Queue_Adapter_Db
{
    /**
     * @see tests/Zend/Queue/Custom/DbForUpdate.php のコード
     *
     * Custom_DbForUpdate はその行を見つけるために、SELECT ... FOR UPDATE を使います。
     * 既存のコードよりも求められる列をもたらす可能性がよりあります。
     *
     * しかしながら、データベース全てに SELECT ... FOR UPDATE フィーチャがあるとは限りません。
     *
     * 注意: これは後でZend_Queue_Adapter_Dbのオプションに変換されました。
     *
     * このコードは良い例をまだ提供します。
     */
}

$options = array(
    'name'          => 'queue1',
    'driverOptions' => array(
        'host'      => '127.0.0.1',
        'port'      => '3306',
        'username'  => 'queue',
        'password'  => 'queue',
        'dbname'    => 'queue',
        'type'      => 'pdo_mysql'
    )
);

$adapter = new Custom_DbForUpdate($options);
$queue = new Zend_Queue($adapter, $options);

同様に即座にアダプタを変えることもできます。

$adapter = new MyCustom_Adapter($options);
$queue   = new Zend_Queue($options);
$queue->setAdapter($adapter);
echo "Adapter: ", get_class($queue->getAdapter()), "\n";

or

$options = array(
    'name'           => 'queue1',
    'namespace'      => 'Custom',
    'driverOptions'  => array(
        'host'       => '127.0.0.1',
        'port'       => '3306',
        'username'   => 'queue',
        'password'   => 'queue',
        'dbname'     => 'queue',
        'type'       => 'pdo_mysql'
    )
);
$queue = new Zend_Queue('DbForUpdate', $config); // Custom_DbForUpdate をロード

独自のメッセージクラスを作成

Zend_Queueは、独自のメッセージクラスも扱います。 変数はアンダーラインで始めます。 例えば:

class Zend_Queue_Message
{
    protected $_data = array();
}

既存のメッセージクラスを拡張できます。 tests/Zend/Queue/Custom/Message.phpでコード例をご覧下さい。

独自のメッセージ・イテレータクラスを作成

Zend_Queueは、独自のメッセージ・イテレータ・クラスも扱います。 メッセージ・イテレータ・クラスは、 Zend_Queue_Adapter_Abstract::recieve()からメッセージを返すために使われます。 たとえメッセージが1つだけだとしても、 Zend_Queue_Abstract::receive()は、 Zend_Queue_Message_Iteratorのようなコンテナ・クラスを常に返さなければなりません。

tests/Zend/Queue/Custom/Messages.phpでファイル名の例をご覧ください。

独自の待ち行列クラスを作成

Zend_Queueは上書きすることも簡単にできます。

tests/Zend/Queue/Custom/Queue.phpでファイル名の例をご覧ください。