Connection.php
1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* Created by PhpStorm.
* User: Ziy
* Date: 14/8/31
* Time: 下午2:17
*/
class Q_Message_Amqp_Connection
{
private $section = 'amqp';
private $nodeName = 'message';
private $credentials = array(
'host' => '127.0.0.1',
'vhost' => '/',
'port' => 5672,
'login' => 'guest',
'password' => 'guest'
);
public function __construct()
{
$server = $this->getServers();
$this->credentials['host'] = $server['host'];
$this->credentials['port'] = $server['port'];
$this->credentials['login'] = $server['login'];
$this->credentials['password'] = $server['password'];
}
public function setVhost($vhost)
{
$this->credentials['vhost'] = $vhost;
return $this;
}
public function setSection($section)
{
$this->section = strtolower($section);
return $this;
}
public function setNodeName($nodeName)
{
$this->nodeName = $nodeName;
return $this;
}
private function getServers()
{
$options = array(
'mode' => Q_Server_Core::SERVER_MODE_PROXY,
'select' => Q_Server_Core::SERVER_SELECT_MODE_RAND
);
return Q_Server::factory('message', $this->section, $this->nodeName, $options);
}
/**
* @return AMQPConnection
*/
public function connect()
{
$connection = new AMQPConnection($this->credentials);
$connection->connect();
return $connection;
}
}