1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AppserverIo\Messaging\AbstractMessage |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/appserver-io/messaging |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Messaging; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Psr\Pms\QueueInterface; |
24
|
|
|
use AppserverIo\Psr\Pms\MessageInterface; |
25
|
|
|
use AppserverIo\Psr\Pms\MonitorInterface; |
26
|
|
|
use AppserverIo\Psr\Pms\StateKeyInterface; |
27
|
|
|
use AppserverIo\Psr\Pms\PriorityKeyInterface; |
28
|
|
|
use AppserverIo\Messaging\Utils\PriorityKeys; |
29
|
|
|
use AppserverIo\Messaging\Utils\PriorityLow; |
30
|
|
|
use AppserverIo\Messaging\Utils\StateKeys; |
31
|
|
|
use AppserverIo\Messaging\Utils\StateActive; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The abstract superclass for all messages. |
35
|
|
|
* |
36
|
|
|
* @author Tim Wagner <[email protected]> |
37
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
38
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
39
|
|
|
* @link https://github.com/appserver-io/messaging |
40
|
|
|
* @link http://www.appserver.io |
41
|
|
|
*/ |
42
|
|
|
abstract class AbstractMessage implements MessageInterface, \Serializable |
43
|
|
|
{ |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The unique session-ID. |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $sessionId = null; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The destination queue to send the message to. |
54
|
|
|
* |
55
|
|
|
* @var \AppserverIo\Psr\Pms\QueueInterface |
56
|
|
|
*/ |
57
|
|
|
protected $destination = null; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The parent message. |
61
|
|
|
* |
62
|
|
|
* @var \AppserverIo\Psr\Pms\MessageInterface |
63
|
|
|
*/ |
64
|
|
|
protected $parentMessage = null; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The monitor for monitoring the message. |
68
|
|
|
* |
69
|
|
|
* @var \AppserverIo\Psr\Pms\MonitorInterface |
70
|
|
|
*/ |
71
|
|
|
protected $messageMonitor = null; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The priority of the message, defaults to 'low'. |
75
|
|
|
* |
76
|
|
|
* @var integer |
77
|
|
|
*/ |
78
|
|
|
protected $priority = null; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The state of the message, defaults to 'active'. |
82
|
|
|
* |
83
|
|
|
* @var integer |
84
|
|
|
*/ |
85
|
|
|
protected $state = null; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* The flag if the message should be deleted when finished or not. |
89
|
|
|
* |
90
|
|
|
* @var boolean |
91
|
|
|
*/ |
92
|
|
|
protected $locked = null; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Initializes the message with the array |
96
|
|
|
* to send to the queue. |
97
|
|
|
*/ |
98
|
1 |
|
public function __construct() |
99
|
|
|
{ |
100
|
|
|
// initialize the default values |
101
|
1 |
|
$this->sessionId = ""; |
102
|
1 |
|
$this->priority = PriorityLow::KEY; |
103
|
1 |
|
$this->state = StateActive::KEY; |
104
|
1 |
|
$this->locked = false; |
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Sets the unique session id. |
109
|
|
|
* |
110
|
|
|
* @param string $sessionId The uniquid id |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function setSessionId($sessionId) |
115
|
|
|
{ |
116
|
|
|
$this->sessionId = $sessionId; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns the unique session id. |
121
|
|
|
* |
122
|
|
|
* @return string The uniquid id |
123
|
|
|
*/ |
124
|
|
|
public function getSessionId() |
125
|
|
|
{ |
126
|
|
|
return $this->sessionId; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Sets the destination queue. |
131
|
|
|
* |
132
|
|
|
* @param \AppserverIo\Psr\Pms\QueueInterface $destination The destination queue |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
public function setDestination(QueueInterface $destination) |
137
|
|
|
{ |
138
|
|
|
$this->destination = $destination; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns the destination queue. |
143
|
|
|
* |
144
|
|
|
* @return \AppserverIo\Psr\Pms\QueueInterface The destination queue |
145
|
|
|
*/ |
146
|
|
|
public function getDestination() |
147
|
|
|
{ |
148
|
|
|
return $this->destination; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Sets the priority of the message. |
153
|
|
|
* |
154
|
|
|
* @param \AppserverIo\Psr\Pms\PriorityKeyInterface $priority The priority to set the message to |
155
|
|
|
* |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
|
|
public function setPriority(PriorityKeyInterface $priority) |
159
|
|
|
{ |
160
|
|
|
$this->priority = $priority->getPriority(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Returns the priority of the message. |
165
|
|
|
* |
166
|
|
|
* @return \AppserverIo\Psr\Pms\PriorityKeyInterface The priority of the message |
167
|
|
|
*/ |
168
|
|
|
public function getPriority() |
169
|
|
|
{ |
170
|
|
|
return PriorityKeys::get($this->priority); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Sets the state of the message. |
175
|
|
|
* |
176
|
|
|
* @param \AppserverIo\Psr\Pms\StateKeyInterface $state The new state |
177
|
|
|
* |
178
|
|
|
* @return void |
179
|
|
|
*/ |
180
|
|
|
public function setState(StateKeyInterface $state) |
181
|
|
|
{ |
182
|
|
|
$this->state = $state->getState(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Returns the state of the message. |
187
|
|
|
* |
188
|
|
|
* @return \AppserverIo\Psr\Pms\StateKeyInterface The message state |
189
|
|
|
*/ |
190
|
|
|
public function getState() |
191
|
|
|
{ |
192
|
|
|
return StateKeys::get($this->state); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Sets the parent message. |
197
|
|
|
* |
198
|
|
|
* @param \AppserverIo\Psr\Pms\MessageInterface $parentMessage The parent message |
199
|
|
|
* |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
|
|
public function setParentMessage(MessageInterface $parentMessage) |
203
|
|
|
{ |
204
|
|
|
$this->parentMessage = $parentMessage; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Returns the parent message. |
209
|
|
|
* |
210
|
|
|
* @return \AppserverIo\Psr\Pms\MessageInterface The parent message |
211
|
|
|
* |
212
|
|
|
* @see \AppserverIo\Psr\Pms\MessageInterface::getParentMessage() |
213
|
|
|
*/ |
214
|
|
|
public function getParentMessage() |
215
|
|
|
{ |
216
|
|
|
return $this->parentMessage; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Sets the monitor for monitoring the message itself. |
221
|
|
|
* |
222
|
|
|
* @param \AppserverIo\Psr\Pms\MonitorInterface $messageMonitor The monitor |
223
|
|
|
* |
224
|
|
|
* @return void |
225
|
|
|
*/ |
226
|
|
|
public function setMessageMonitor(MonitorInterface $messageMonitor) |
227
|
|
|
{ |
228
|
|
|
$this->messageMonitor = $messageMonitor; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Returns the message monitor. |
233
|
|
|
* |
234
|
|
|
* @return \AppserverIo\Psr\Pms\MonitorInterface The monitor |
235
|
|
|
*/ |
236
|
|
|
public function getMessageMonitor() |
237
|
|
|
{ |
238
|
|
|
return $this->messageMonitor; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Locks the message. |
243
|
|
|
* |
244
|
|
|
* @return void |
245
|
|
|
*/ |
246
|
|
|
public function lock() |
247
|
|
|
{ |
248
|
|
|
$this->locked = true; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Unlocks the message. |
253
|
|
|
* |
254
|
|
|
* @return void |
255
|
|
|
*/ |
256
|
|
|
public function unlock() |
257
|
|
|
{ |
258
|
|
|
$this->locked = false; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Returns the message lock flag. |
263
|
|
|
* |
264
|
|
|
* @return boolean TRUE if the message is locked, else FALSE |
265
|
|
|
*/ |
266
|
|
|
public function isLocked() |
267
|
|
|
{ |
268
|
|
|
return $this->locked; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Serializes the message and returns the serialized representation. |
273
|
|
|
* |
274
|
|
|
* @return string the string representation of the object or null |
275
|
|
|
* @link http://php.net/manual/en/serializable.serialize.php |
276
|
|
|
*/ |
277
|
1 |
|
public function serialize() |
278
|
|
|
{ |
279
|
1 |
|
return serialize(get_object_vars($this)); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* The serialized representation of the message. |
284
|
|
|
* |
285
|
|
|
* @param string $data The string representation of the object |
286
|
|
|
* |
287
|
|
|
* @return void |
288
|
|
|
* @link http://php.net/manual/en/serializable.unserialize.php |
289
|
|
|
*/ |
290
|
1 |
|
public function unserialize($data) |
291
|
|
|
{ |
292
|
1 |
|
foreach (unserialize($data) as $propertyName => $propertyValue) { |
293
|
1 |
|
$this->$propertyName = $propertyValue; |
294
|
1 |
|
} |
295
|
1 |
|
} |
296
|
|
|
} |
297
|
|
|
|