forked from CoderKungfu/php-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsAzureServiceBus.php
More file actions
155 lines (136 loc) · 4.56 KB
/
Copy pathWindowsAzureServiceBus.php
File metadata and controls
155 lines (136 loc) · 4.56 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace PHPQueue\Backend;
use PHPQueue\Exception\BackendException;
use PHPQueue\Exception\JobNotFoundException;
use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Common\ServiceException;
use WindowsAzure\ServiceBus\Models\QueueInfo;
use WindowsAzure\ServiceBus\Models\BrokeredMessage;
use WindowsAzure\ServiceBus\Models\ReceiveMessageOptions;
class WindowsAzureServiceBus extends Base
{
public $connection_string = '';
public $queue_name = '';
public function __construct($options=array())
{
parent::__construct();
if (!empty($options['queue'])) {
$this->queue_name = $options['queue'];
}
if (!empty($options['connection_string'])) {
$this->connection_string = $options['connection_string'];
}
}
public function connect()
{
if (empty($this->connection_string)) {
throw new BackendException("Connection string not specified.");
}
$this->connection = ServicesBuilder::getInstance()->createServiceBusService($this->connection_string);
}
/**
* @return \WindowsAzure\ServiceBus\ServiceBusRestProxy
*/
public function getConnection()
{
return parent::getConnection();
}
/**
* @param array $data
* @throws \PHPQueue\Exception\BackendException
* @return boolean Status of saving
*/
public function add($data=array())
{
$this->beforeAdd();
$this->checkQueue();
try {
$message = new BrokeredMessage();
$message->setBody(json_encode($data));
$this->getConnection()->sendQueueMessage($this->queue_name, $message);
} catch (ServiceException $ex) {
throw new BackendException($ex->getMessage(), $ex->getCode());
}
return true;
}
/**
* @throws \PHPQueue\Exception\JobNotFoundException
* @return array
*/
public function get()
{
$this->beforeGet();
$this->checkQueue();
try {
$options = new ReceiveMessageOptions();
$options->setPeekLock();
$response = $this->getConnection()->receiveQueueMessage($this->queue_name, $options);
if (empty($response)) {
throw new JobNotFoundException('No message found.', 404);
}
$this->last_job = $response;
$this->last_job_id = $response->getMessageId();
$this->afterGet();
return json_decode($response->getBody(), TRUE);
} catch (ServiceException $ex) {
throw new JobNotFoundException($ex->getMessage(), $ex->getCode());
}
}
/**
* @param string $jobId
* @throws \PHPQueue\Exception\BackendException
* @return boolean
*/
public function clear($jobId=null)
{
$this->beforeClear($jobId);
$this->isJobOpen($jobId);
try {
$this->getConnection()->deleteMessage($this->open_items[$jobId]);
$this->last_job_id = $jobId;
$this->afterClearRelease();
return true;
} catch (ServiceException $ex) {
throw new BackendException($ex->getMessage(), $ex->getCode());
}
}
/**
* @param string $jobId
* @throws \PHPQueue\Exception\BackendException
* @return boolean
*/
public function release($jobId=null)
{
$this->beforeRelease($jobId);
$this->isJobOpen($jobId);
try {
$this->getConnection()->unlockMessage($this->open_items[$jobId]);
$this->last_job_id = $jobId;
$this->afterClearRelease();
return true;
} catch (ServiceException $ex) {
throw new BackendException($ex->getMessage(), $ex->getCode());
}
}
public function createQueue($queue_name)
{
try {
$queueInfo = new QueueInfo($queue_name);
$this->getConnection()->createQueue($queueInfo);
return true;
} catch (ServiceException $ex) {
throw new BackendException($ex->getMessage(), $ex->getCode());
}
}
private function checkQueue()
{
if (empty($this->queue_name)) {
throw new BackendException("Queue name not specified.");
}
$queue_info = $this->getConnection()->getQueue($this->queue_name);
if ($this->queue_name != $queue_info->getTitle()) {
return $this->createQueue($this->queue_name);
}
return true;
}
}