副标题[/!--empirenews.page--]
本文实例为大家分享了php beanstalkd消息队列类的具体代码,供大家参考,具体内容如下
use RuntimeException;
/**
- An interface to the beanstalk queue service. Implements the beanstalk
- protocol spec 1.9. Where appropriate the documentation from the protocol
- has been added to the docblocks in this class.
-
- @link https://github.com/kr/beanstalkd/blob/master/doc/protocol.txt
*/
class BeanStalk {
/**
- Minimum priority value which can be assigned to a job. The minimum
- priority value is also the highest priority a job can have.
-
- @var integer
*/
const MIN_PRIORITY = 0;
/**
- Maximum priority value which can be assigned to a job. The maximum
- priority value is also the lowest priority a job can have.
-
- @var integer
*/
const MAX_PRIORITY = 4294967295;
/**
- Holds a boolean indicating whether a connection to the server is
- currently established or not.
-
- @var boolean
*/
public $connected = false;
/**
- Holds configuration values.
-
- @var array
*/
protected $_config = [];
/**
- The current connection resource handle (if any).
-
- @var resource
*/
protected $_connection;
/**
- Constructor.
-
- @param array $config An array of configuration values:
-
'persistent' Whether to make the connection persistent or
- not,defaults to
true as the FAQ recommends
- persistent connections.
-
'host' The beanstalk server hostname or IP address to
- connect to,defaults to
127.0.0.1 .
-
'port' The port of the server to connect to,defaults
- to
11300 .
-
'timeout' Timeout in seconds when establishing the
- connection,defaults to
1 .
-
'logger' An instance of a PSR-3 compatible logger.
-
- @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
- @return void
*/
public function __construct(array $config = []) {
$defaults = [
'persistent' => true,'host' => '127.0.0.1','port' => 11300,'timeout' => 1,'logger' => null
];
$this->_config = $config + $defaults;
}
/**
- Destructor,disconnects from the server.
-
- @return void
*/
public function __destruct() {
$this->disconnect();
}
/**
- Initiates a socket connection to the beanstalk server. The resulting
- stream will not have any timeout set on it. Which means it can wait
- an unlimited amount of time until a packet becomes available. This
- is required for doing blocking reads.
-
- @see BeanstalkClient::$_connection
- @see BeanstalkClient::reserve()
- @return boolean
true if the connection was established,false otherwise.
*/
public function connect() {
if (isset($this->_connection)) {
$this->disconnect();
}
$errNum = '';
$errStr = '';
$function = $this->_config['persistent'] ? 'pfsockopen' : 'fsockopen';
$params = [$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;
}
/**
-
Closes the connection to the beanstalk server by first signaling
-
that we want to quit then actually closing the socket connection.
-
-
@return boolean true if diconnecting was successful.
*/
public function disconnect() {
if (!is_resource($this->_connection)) {
$this->connected = false;
} else {
$this->_write('quit');
$this->connected = !fclose($this->_connection);
if (!$this->connected) {
$this->_connection = null;
}
}
return !$this->connected;
}
(编辑:晋中站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|