/** * Add a message to PMS server * * @param string $msg Json encode string (must contain 'type' and 'data' fields) * @return void */ public function add($msg) { if (!Pms_Util::is_json($msg)) { throw new Pms_Exception('Please add a json format message'); } // send directly if (!$this->xa_start) { $this->xa_client->sendMsg($msg); // in transaction } else { $this->xa_client->send($msg); } }
/** * Construct */ public function __construct($host = '', $ports = array()) { parent::__construct(); // init shared space // init host address $this->host = $host ? (string) $host : PMS_SERVER_HOST; // init shared ports array $this->ports = $ports ? (array) $ports : Pms_Util::getServerPorts(PMS_SERVER_PORT); // init max process for server $this->setMaxProcess(count($ports)); // clear pid file first // do only once !!! $this->__pid(false); // store parent process pid // do only once !!! $this->__pid(true); }
/** * PMS Framework * * @ignore * @category Pms_Message * @package Pms_Message * @author James.Huang <*****@*****.**> * @version $Id$ */ // could be call by other scripts ; avoid path change require_once realpath(dirname(__FILE__) . '/../etc') . '/config.inc'; require_once 'Pms.php'; $action = $argv[1]; // passed parameter $host = PMS_SERVER_HOST; $ports = Pms_Util::getServerPorts(PMS_SERVER_PORT); $xa = new Pms($host, $ports); $xa->beginTransaction(); // start transaction try { if ($action == 'get') { echo "Deal with the messages : \n"; // deal with messages for ($i = 0; $i < 10; $i++) { var_dump($xa->get()); } } if ($action == 'getback') { echo "Deal with the messages : \n"; // deal with messages for ($i = 0; $i < 10; $i++) {
/** * Get message object * * @return Object */ public function getMsg() { // all queues are empty if (!sizeof($this->xports)) { return false; } // get random client $client = new Pms_Client($this->host, $this->xports); // if mq is empty if (!$client->getSize()) { $ports = Pms_Util::array_remove($this->xports, $client->getPort()); return $client->getMsg(); } // store current client $this->client = $client; // call mq server to recv msg return $client->recvMsg(); }