轉載於:http://blog.csdn.net/yao970953039/article/details/41821387php
1.系統centos 我是直接使用yum install beanstalk安裝的git
2.下載beanstalk的php擴展包 放在extensionsgithub
- Yii::import('application.extensions.SendSmsBeanstalk');
-
- class AController extends CController{
-
- public function actionIndex(){
- for($i=0;$i<1000;$i++){
- SendSmsBeanstalk::sendSms('sendsms_'.$i);
- }
- }
-
- public function actionA(){
- SendSmsBeanstalk::handleMessage();
- }
- }
- </pre><pre code_snippet_id="546591" snippet_file_name="blog_20141209_4_7698245" name="code" class="php">
- Yii::import('application.extensions.beanstalk.*');
- class SendSmsBeanstalk extends CComponent{
-
-
- public static function sendSms($body){
-
- $beanstalk = new Socket_Beanstalk();
- if (!$beanstalk->connect()) {
- exit(current($beanstalk->errors()));
- }
-
- $beanstalk->useTube('hube_send_smss');
-
-
-
- $put = $beanstalk->put(
- 23,
- 0,
- 60,
- $body
- );
- if (!$put) {
- return false;
- }
- $beanstalk->disconnect();
- }
-
-
-
- public static function handleMessage(){
-
- $beanstalk = new Socket_Beanstalk();
-
- if (!$beanstalk->connect()) {
- exit(current($beanstalk->errors()));
- }
-
- $beanstalk->useTube('hube_send_smss');
-
- $beanstalk->watch('hube_send_smss');
-
- $beanstalk->ignore('default');
-
-
-
- while($job = $beanstalk->reserve(2)){
-
- $result = $job['body'];
- echo $job['id'];echo '<br>';
- if ($result) {<span style="font-family: Arial, Helvetica, sans-serif;">
-
- $beanstalk->delete($job['id']);
- } else {
-
- $beanstalk->bury($job['id'],'');
- }
-
- }
- $beanstalk->disconnect();
- }
下方提供我找到的隊列PHP版本core,不能上傳附件。代碼在下面centos
-
- class Socket_Beanstalk {
-
-
- public $connected = false;
-
-
- protected $_config = array();
-
-
- protected $_connection;
-
-
- protected $_errors = array();
-
-
- public function __construct(array $config = array()) {
- $defaults = array(
- 'persistent' => true,
- 'host' => '127.0.0.1',
- 'port' => 11300,
- 'timeout' => 1
- );
- $this->_config = $config + $defaults;
- }
-
-
- public function __destruct() {
- $this->disconnect();
- }
-
-
- public function connect() {
- if (isset($this->_connection)) {
- $this->disconnect();
- }
-
- $function = $this->_config['persistent'] ? 'pfsockopen' : 'fsockopen';
- $params = array($this->_config['host'], $this->_config['port'], &$errNum, &$errStr);
-
- if ($this->_config['timeout']) {
- $params[] = $this->_config['timeout'];
- }
- $this->_connection = @call_user_func_array($function, $params);
-
- if (!empty($errNum) || !empty($errStr)) {
- $this->_error("{$errNum}: {$errStr}");
- }
-
- $this->connected = is_resource($this->_connection);
-
- if ($this->connected) {
- stream_set_timeout($this->_connection, -1);
- }
- return $this->connected;
- }
-
-
- public function disconnect() {
- if (!is_resource($this->_connection)) {
- $this->connected = false;
- } else {
- $this->connected = !fclose($this->_connection);
-
- if (!$this->connected) {
- $this->_connection = null;
- }
- }
- return !$this->connected;
- }
-
-
- public function errors() {
- return $this->_errors;
- }
-
-
- protected function _error($message) {
- if (count($this->_errors) >= 200) {
- array_shift($this->_errors);
- }
- array_push($this->_errors, $message);
- }
-
-
- protected function _write($data) {
- if (!$this->connected && !$this->connect()) {
- return false;
- }
-
- $data .= "\r\n";
- return fwrite($this->_connection, $data, strlen($data));
- }
-
-
- protected function _read($length = null) {
- if (!$this->connected && !$this->connect()) {
- return false;
- }
- if ($length) {
- if (feof($this->_connection)) {
- return false;
- }
- $data = fread($this->_connection, $length + 2);
- $meta = stream_get_meta_data($this->_connection);
-
- if ($meta['timed_out']) {
- $this->_error('Connection timed out.');
- return false;
- }
- $packet = rtrim($data, "\r\n");
- } else {
- $packet = stream_get_line($this->_connection, 16384, "\r\n");
- }
- return $packet;
- }
-
-
-
-
- public function put($pri, $delay, $ttr, $data) {
- $this->_write(sprintf('put %d %d %d %d', $pri, $delay, $ttr, strlen($data)));
- $this->_write($data);
- $status = strtok($this->_read(), ' ');
-
- switch ($status) {
- case 'INSERTED':
- case 'BURIED':
- return (integer)strtok(' ');
- case 'EXPECTED_CRLF':
- case 'JOB_TOO_BIG':
- default:
- $this->_error($status);
- return false;
- }
- }
-
-
- public function choose($tube) {
- $this->_write(sprintf('use %s', $tube));
- $status = strtok($this->_read(), ' ');
-
- switch ($status) {
- case 'USING':
- return strtok(' ');
- default:
- $this->_error($status);
- return false;
- }
- }
-
-
- public function useTube($tube) {
- return $this->choose($tube);
- }
-
-
-
-
- public function reserve($timeout = null) {
- if (isset($timeout)) {
- $this->_write(sprintf('reserve-with-timeout %d', $timeout));
- } else {
- $this->_write('reserve');
- }
- $status = strtok($this->_read(), ' ');
-
- switch ($status) {
- case 'RESERVED':
- return array(
- 'id' => (integer)strtok(' '),
- 'body' => $this->_read((integer)strtok(' '))
- );
- case 'DEADLINE_SOON':
- case 'TIMED_OUT':
- default:
- $this->_error($status);
- return false;
- }
- }
-
-
- public function delete($id) {
- $this->_write(sprintf('delete %d', $id));
- $status = $this->_read();
-
- switch ($status) {
- case 'DELETED':
- return true;
- case 'NOT_FOUND':
- default:
- $this->_error($status);
- return false;
- }
- }
-
-
- public function release($id, $pri, $delay) {
- $this->_write(sprintf('release %d %d %d', $id, $pri, $delay));
- $status = $this->_read();
-
- switch ($status) {
- case 'RELEASED':
- case 'BURIED':
- return true;
- case 'NOT_FOUND':
- default:
- $this->_error($status);
- return false;
- }
- }
-
-
- public function bury($id, $pri) {
- $this->_write(sprintf('bury %d %d', $id, $pri));
- $status = $this->_read();
-
- switch ($status) {
- case 'BURIED':
- return true;
- case 'NOT_FOUND':
- default:
- $this->_error($status);
- return false;
- }
- }
-
-
- public function touch($id) {
- $this->_write(sprintf('touch %d', $id));
- $status = $this->_read();
-
- switch ($status) {
- case 'TOUCHED':
- return true;
- case 'NOT_TOUCHED':
- default:
- $this->_error($status);
- return false;
- }
- }
-
-
- public function watch($tube) {
- $this->_write(sprintf('watch %s', $tube));
- $status = strtok($this->_read(), ' ');
-
- switch ($status) {
- case 'WATCHING':
- return (integer)strtok(' ');
- default:
- $this->_error($status);
- return false;
- }
- }
-
-
- public function ignore($tube) {
- $this->_write(sprintf('ignore %s', $tube));
- $status = strtok($this->_read(), ' ');
-
- switch ($status) {
- case 'WATCHING':
- return (integer)strtok(' ');
- case 'NOT_IGNORED':
- default:
- $this->_error($status);
- return false;
- }
- }
-
-
-
-
- public function peek($id) {
- $this->_write(sprintf('peek %d', $id));
- return $this->_peekRead();
- }
-
-
- public function peekReady() {
- $this->_write('peek-ready');
- return $this->_peekRead();
- }
-
-
- public function peekDelayed() {
- $this->_write('peek-delayed');
- return $this->_peekRead();
- }
-
-
- public function peekBuried() {
- $this->_write('peek-buried');
- return $this->_peekRead();
- }
-
-
- protected function _peekRead() {
- $status = strtok($this->_read(), ' ');
-
- switch ($status) {
- case 'FOUND':
- return array(
- 'id' => (integer)strtok(' '),
- 'body' => $this->_read((integer)strtok(' '))
- );
- case 'NOT_FOUND':
- default:
- $this->_error($status);
- return false;
- }
- }
-
-
- public function kick($bound) {
- $this->_write(sprintf('kick %d', $bound));
- $status = strtok($this->_read(), ' ');
-
- switch ($status) {
- case 'KICKED':
- return (integer)strtok(' ');
- default:
- $this->_error($status);
- return false;
- }
- }
-
-
-
-
- public function statsJob($id) {
- $this->_write(sprintf('stats-job %d', $id));
- return $this->_statsRead();
- }
-
-
- public function statsTube($tube) {
- $this->_write(sprintf('stats-tube %s', $tube));
- return $this->_statsRead();
- }
-
-
- public function stats() {
- $this->_write('stats');
- return $this->_statsRead();
- }
-
-
- public function listTubes() {
- $this->_write('list-tubes');
- return $this->_statsRead();
- }
-
-
- public function listTubeUsed() {
- $this->_write('list-tube-used');
- $status = strtok($this->_read(), ' ');
-
- switch ($status) {
- case 'USING':
- return strtok(' ');
- default:
- $this->_error($status);
- return false;
- }
- }
-
-
- public function listTubeChosen() {
- return $this->listTubeUsed();
- }
-
-
- public function listTubesWatched() {
- $this->_write('list-tubes-watched');
- return $this->_statsRead();
- }
-
-
- protected function _statsRead($decode = true) {
- $status = strtok($this->_read(), ' ');
-
- switch ($status) {
- case 'OK':
- $data = $this->_read((integer)strtok(' '));
- return $decode ? $this->_decode($data) : $data;
- default:
- $this->_error($status);
- return false;
- }
- }
-
-
- protected function _decode($data) {
- $data = array_slice(explode("\n", $data), 1);
- $result = array();
-
- foreach ($data as $key => $value) {
- if ($value[0] === '-') {
- $value = ltrim($value, '- ');
- } elseif (strpos($value, ':') !== false) {
- list($key, $value) = explode(':', $value);
- $value = ltrim($value, ' ');
- }
- if (is_numeric($value)) {
- $value = (integer) $value == $value ? (integer) $value : (float) $value;
- }
- $result[$key] = $value;
- }
- return $result;
- }
- }
安裝成功是上面這樣子app