-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZyProducer.php
68 lines (59 loc) · 1.75 KB
/
ZyProducer.php
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
<?php
/**
* Created by PhpStorm.
*
* @author Bi Zhiming <evan2884@gmail.com>
* @created 2017/12/15 上午11:30
* @since 1.0
*/
namespace myziyue\amqp;
class ZyProducer extends Base
{
protected static $exchange = null;
protected static $producer = null;
public function __construct()
{
parent::__construct();
}
public static function getInstance()
{
if (static::$producer == null) {
static::$producer = new self();
}
return static::$producer;
}
public function create()
{
try {
$this->getChannel();
static::$exchange = new \AMQPExchange(static::$channel);
static::$exchange->setName($this->exchangeName);
static::$exchange->setType($this->exchangeType);
static::$exchange->setFlags($this->flags);
static::$exchange->declareExchange();
static::$queue = new \AMQPQueue(static::$channel);
static::$queue->setName($this->queueName);
static::$queue->setFlags($this->flags);
static::$queue->declareQueue();
} catch (\AMQPException $ex) {
throw new \Exception($ex->getMessage());
return false;
}
return true;
}
public function push($message, $key)
{
try {
static::$channel->startTransaction();
//echo "exchange status:".$ex->declare();
echo "exchange status:" . static::$exchange->declareExchange();
echo "\n";
static::$exchange->publish($message, $key);
static::$channel->commitTransaction();
} catch (\AMQPException $ex) {
throw new \Exception($ex);
return false;
}
return true;
}
}