PHP Message Queuing encapsulation based on system V message queue

Source: Internet
Author: User
Tags getmessage

Original articles, reproduced please specify the source: http://www.huyanping.cn/?p=235
Jenner

The system V Message queue is a process communication (IPC) approach that facilitates the implementation of the producer-consumer model, where single or multiple producers write messages to the queue, and multiple producers then get messages from the queue for processing.

Project Address: Https://github.com/huyanping/Zebra-PHP-Framework

wrapper support:

    1. process communication
    2. set maximum queue capacity (bytes units)
    3. Gets the current number of queues
    4. Modify the queue section properties

Note: If you want to modify the maximum queue size, make sure your script is running under root

<?php/** * Created by Phpstorm. * user:huyanping * date:14-8-9 * Time: Morning 3:44 * * System V Message Queue IPC Communication Message Queue encapsulation * If you want to modify the maximum number of bytes a queue can store, make sure your script has a roo    T-permissions */class Systemvmessagequeue implements imessagequeue{//message grouping type for grouping information in a message queue private $msg _type;    Queue flag Private $queue;    Whether to serialize private $serialize _needed;    Whether to block private $block _send when unable to write to queue;    Sets the bit msg_ipc_nowait, if unable to get to a message, does not wait, if bit null is set, it waits for the message to arrive private $option _receive;    The maximum message size that you want to receive private $maxsize;    IPC Communication key Private $key _t; /** * @param $IPC _filename IPC Communication flag file for obtaining a unique IPC KEY * @param $msg _type Message type * @param bool $serialize _needed Order Column * @param bool $block _send cannot write to the queue, whether to block * @param int $option _receive Set bit msg_ipc_nowait, if you cannot get to a message, do not wait; set bit nul  L, you wait for the message to arrive * @param int $maxsize The maximum message you want to receive */Public function __construct ($msg _type, $ipc _filename = __file__, $serialize _needed = True, $block _send = False, $option _receive = msg_ipc_nowait, $maxsize =100000) {$this->msg_type = $msg _type;        $this->serialize_needed = $serialize _needed;        $this->block_send = $block _send;        $this->option_receive = $option _receive;        $this->maxsize = $maxsize;    $this->init_queue ($ipc _filename, $msg _type);  }/** * Initializes a queue * @param $ipc _filename * @param $msg _type * @throws Exception */Public function        Init_queue ($IPC _filename, $msg _type) {$this->key_t = $this->get_ipc_key ($ipc _filename, $msg _type);        $this->queue = msg_get_queue ($this->key_t);    if (! $this->queue) throw new Exception (' Msg_get_queue failed '); }/** * @param $ipc _filename * @param $msg _type * @return int * @throws Exception */public func        tion Get_ipc_key ($ipc _filename, $msg _type) {$key _t = Ftok ($ipc _filename, $msg _type);        if ($key _t = = 0) throw new Exception (' Ftok error ');    return $key _t; }/** * Get a * from the queue *@return BOOL * @throws Exception */Public function get () {$queue _status = $this->status (); if ($queue _status[' Msg_qnum ' > 0) {if (Msg_receive ($this->queue, $this->msg_type, $msgtype _erhalt En, $this->maxsize, $data, $this->serialize_needed, $this->option_receive, $err) = = = True) {Retur            n $data;            } else {throw new Exception ($ERR);        }} else {return false;        }}/** * Write queue * @param $message * @throws Exception * * Public function put ($message) { if (!msg_send ($this->queue, $this->msg_type, $message, $this->serialize_needed, $this->block_send, $err)        = = = True) {throw new Exception ($ERR);    } return true; The index of the return value array is as follows: * Msg_perm.uid the UID of the "the" queue. User ID * Msg_perm.gid The GID of the "the" of the queue. User Group ID * msg_perm.mode the file access mOde of the queue. Access Mode * Msg_stime The last message is sent to the queue.  Last Queue write times * Msg_rtime The time that is received from the queue. Last queue receive times * Msg_ctime the time that the queue was changed. Last Modified time * Msg_qnum the number of messages waiting to is read from the queue.  Number of queues currently waiting to be read * Msg_qbytes the maximum numbers of bytes allowed in one message queue.     The maximum message size allowed to be received in a message queue * on Linux, the This value could be read and modified VIA/PROC/SYS/KERNEL/MSGMNB. * Msg_lspid The PID of the process that sent the last message to the queue. The process ID of the last message sent * Msg_lrpid The PID of the process that received the end message from the queue. The process ID of the last message received * * @return array */Public function status () {$queue _status = Msg_stat_queue ($this-        >queue);    return $queue _status; /** * Gets the current stacking status of the queue * @return Mixed */Public function size () {$status = $this->status ();        return $status [' Msg_qnum ']; }/** * Allows the values of the Msg_perm.uid, * msg_perm.gid, Msg_perm.mode and Msg_qbytes field s of the underlying message queue data structure * can be used to modify the maximum read data received by the queue to run * * @param $key status subscript * @param $val UE Status Value * @return BOOL */Public Function set_status ($key, $value) {$this->check_set_privilege ($ke        y);        if ($key = = ' Msg_qbytes ') return $this->set_max_queue_size ($value);        $queue _status[$key] = $value;    Return Msg_set_queue ($this->queue, $queue _status); }/** * Delete a queue * @return BOOL */Public Function Queue_remove () {return Msg_remove_queue ($thi    S->queue); }//Modify the maximum number of bytes the queue can hold, requires root privileges/** * @param $size * @return BOOL * @throws Exception */Public Functi        On Set_max_queue_size ($size) {$user = Get_current_user (); if ($user!== ' root ') throw new Exception (' changing mSg_qbytes needs root privileges ');    return $this->set_status (' msg_qbytes ', $size); }/** * Determine if a queue exists * @param $key * @return BOOL */Public Function queue_exists ($key) {RE    Turn msg_queue_exists ($key);    /** * Check permissions to modify queue status * @param $key * @throws Exception * */Private Function Check_set_privilege ($key)        {$privilege _field = array (' Msg_perm.uid ', ' msg_perm.gid ', ' msg_perm.mode '); if (!in_array ($key, $privilege _field)) {throw new Exception (' You can have change Msg_perm.uid, Msg_perm.gid, M Sg_perm.mode and Msg_qbytes.        and msg_qbytes needs root privileges ');  }    }}

The method is called as follows:

Write queue:

<?php/** * Created by Phpstorm. * user:huyanping * date:14-8-22 * Time: PM 12:13 */define (' Test_root ', DirName (__file__)); Require test_root. '/.. /core/imessagequeue.interface.php '; Require test_root. '/.. /core/systemvmessagequeue.class.php '; try{    $messageQueue = new Systemvmessagequeue (1, dirname (__file__));    while (true) {        var_dump ($messageQueue->put (Mt_rand (0, +)));        echo $messageQueue->size (). Php_eol;        Sleep (1);}    } catch (Exception $e) {    echo $e->getmessage ();}

Get queue:
<?php/** * Created by Phpstorm. * user:huyanping * date:14-8-22 * Time: PM 12:13 */define (' Test_root ', DirName (__file__)); Require test_root. '/.. /core/imessagequeue.interface.php '; Require test_root. '/.. /core/systemvmessagequeue.class.php '; try{    $messageQueue = new Systemvmessagequeue (1, dirname (__file__));    while (true) {        var_dump ($messageQueue->get ());        Sleep (1);}    } catch (Exception $e) {    echo $e->getmessage ();}
To delete a queue:
<?php/** * Created by Phpstorm. * User:administrator * date:14-8-22 * Time: PM 2:35 */define (' Test_root ', DirName (__file__)); Require test_root. '/.. /core/imessagequeue.interface.php '; Require test_root. '/.. /core/systemvmessagequeue.class.php '; try{    $messageQueue = new Systemvmessagequeue (1, dirname (__file__));    Var_dump ($messageQueue->queue_remove ());} catch (Exception $e) {    echo $e->getmessage ();}



PHP Message Queuing encapsulation based on system V message queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.