1 | <?php |
||
46 | abstract class AbstractMessage implements MessageInterface, \Serializable |
||
47 | { |
||
48 | |||
49 | /** |
||
50 | * The unique session-ID. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $sessionId = null; |
||
55 | |||
56 | /** |
||
57 | * The destination queue to send the message to. |
||
58 | * |
||
59 | * @var \AppserverIo\Psr\Pms\QueueInterface |
||
60 | */ |
||
61 | protected $destination = null; |
||
62 | |||
63 | /** |
||
64 | * The parent message. |
||
65 | * |
||
66 | * @var \AppserverIo\Psr\Pms\MessageInterface |
||
67 | */ |
||
68 | protected $parentMessage = null; |
||
69 | |||
70 | /** |
||
71 | * The monitor for monitoring the message. |
||
72 | * |
||
73 | * @var \AppserverIo\Psr\Pms\MonitorInterface |
||
74 | */ |
||
75 | protected $messageMonitor = null; |
||
76 | |||
77 | /** |
||
78 | * The priority of the message, defaults to 'low'. |
||
79 | * |
||
80 | * @var integer |
||
81 | */ |
||
82 | protected $priority = null; |
||
83 | |||
84 | /** |
||
85 | * The state of the message, defaults to 'active'. |
||
86 | * |
||
87 | * @var integer |
||
88 | */ |
||
89 | protected $state = null; |
||
90 | |||
91 | /** |
||
92 | * The flag if the message should be deleted when finished or not. |
||
93 | * |
||
94 | * @var boolean |
||
95 | */ |
||
96 | protected $locked = null; |
||
97 | |||
98 | /** |
||
99 | * The array with the timeouts for the retries. |
||
100 | * |
||
101 | * @var array |
||
102 | */ |
||
103 | protected $retryTimeouts = array(); |
||
104 | |||
105 | /** |
||
106 | * The array with the callbacks. |
||
107 | * |
||
108 | * @var array |
||
109 | */ |
||
110 | protected $callbacks = array(); |
||
111 | |||
112 | /** |
||
113 | * Initializes the message with the array |
||
114 | * to send to the queue. |
||
115 | */ |
||
116 | 1 | public function __construct() |
|
124 | |||
125 | /** |
||
126 | * Sets the unique session id. |
||
127 | * |
||
128 | * @param string $sessionId The uniquid id |
||
129 | * |
||
130 | * @return void |
||
131 | */ |
||
132 | public function setSessionId($sessionId) |
||
136 | |||
137 | /** |
||
138 | * Returns the unique session id. |
||
139 | * |
||
140 | * @return string The uniquid id |
||
141 | */ |
||
142 | public function getSessionId() |
||
146 | |||
147 | /** |
||
148 | * Sets the destination queue. |
||
149 | * |
||
150 | * @param \AppserverIo\Psr\Pms\QueueInterface $destination The destination queue |
||
151 | * |
||
152 | * @return void |
||
153 | */ |
||
154 | public function setDestination(QueueInterface $destination) |
||
158 | |||
159 | /** |
||
160 | * Returns the destination queue. |
||
161 | * |
||
162 | * @return \AppserverIo\Psr\Pms\QueueInterface The destination queue |
||
163 | */ |
||
164 | public function getDestination() |
||
168 | |||
169 | /** |
||
170 | * Sets the priority of the message. |
||
171 | * |
||
172 | * @param \AppserverIo\Psr\Pms\PriorityKeyInterface $priority The priority to set the message to |
||
173 | * |
||
174 | * @return void |
||
175 | */ |
||
176 | public function setPriority(PriorityKeyInterface $priority) |
||
180 | |||
181 | /** |
||
182 | * Returns the priority of the message. |
||
183 | * |
||
184 | * @return \AppserverIo\Psr\Pms\PriorityKeyInterface The priority of the message |
||
185 | */ |
||
186 | public function getPriority() |
||
190 | |||
191 | /** |
||
192 | * Sets the state of the message. |
||
193 | * |
||
194 | * @param \AppserverIo\Psr\Pms\StateKeyInterface $state The new state |
||
195 | * |
||
196 | * @return void |
||
197 | */ |
||
198 | public function setState(StateKeyInterface $state) |
||
202 | |||
203 | /** |
||
204 | * Returns the state of the message. |
||
205 | * |
||
206 | * @return \AppserverIo\Psr\Pms\StateKeyInterface The message state |
||
207 | */ |
||
208 | public function getState() |
||
212 | |||
213 | /** |
||
214 | * Sets the parent message. |
||
215 | * |
||
216 | * @param \AppserverIo\Psr\Pms\MessageInterface $parentMessage The parent message |
||
217 | * |
||
218 | * @return void |
||
219 | */ |
||
220 | public function setParentMessage(MessageInterface $parentMessage) |
||
224 | |||
225 | /** |
||
226 | * Returns the parent message. |
||
227 | * |
||
228 | * @return \AppserverIo\Psr\Pms\MessageInterface The parent message |
||
229 | * |
||
230 | * @see \AppserverIo\Psr\Pms\MessageInterface::getParentMessage() |
||
231 | */ |
||
232 | public function getParentMessage() |
||
236 | |||
237 | /** |
||
238 | * Sets the monitor for monitoring the message itself. |
||
239 | * |
||
240 | * @param \AppserverIo\Psr\Pms\MonitorInterface $messageMonitor The monitor |
||
241 | * |
||
242 | * @return void |
||
243 | */ |
||
244 | public function setMessageMonitor(MonitorInterface $messageMonitor) |
||
248 | |||
249 | /** |
||
250 | * Returns the message monitor. |
||
251 | * |
||
252 | * @return \AppserverIo\Psr\Pms\MonitorInterface The monitor |
||
253 | */ |
||
254 | public function getMessageMonitor() |
||
258 | |||
259 | /** |
||
260 | * Locks the message. |
||
261 | * |
||
262 | * @return void |
||
263 | */ |
||
264 | public function lock() |
||
268 | |||
269 | /** |
||
270 | * Unlocks the message. |
||
271 | * |
||
272 | * @return void |
||
273 | */ |
||
274 | public function unlock() |
||
278 | |||
279 | /** |
||
280 | * Returns the message lock flag. |
||
281 | * |
||
282 | * @return boolean TRUE if the message is locked, else FALSE |
||
283 | */ |
||
284 | public function isLocked() |
||
288 | |||
289 | /** |
||
290 | * Serializes the message and returns the serialized representation. |
||
291 | * |
||
292 | * @return string the string representation of the object or null |
||
293 | * @link http://php.net/manual/en/serializable.serialize.php |
||
294 | */ |
||
295 | 1 | public function serialize() |
|
299 | |||
300 | /** |
||
301 | * The serialized representation of the message. |
||
302 | * |
||
303 | * @param string $data The string representation of the object |
||
304 | * |
||
305 | * @return void |
||
306 | * @link http://php.net/manual/en/serializable.unserialize.php |
||
307 | */ |
||
308 | 1 | public function unserialize($data) |
|
309 | { |
||
310 | 1 | foreach (unserialize($data) as $propertyName => $propertyValue) { |
|
311 | 1 | $this->$propertyName = $propertyValue; |
|
312 | 1 | } |
|
313 | 1 | } |
|
314 | |||
315 | /** |
||
316 | * Set's the array with the retry timeouts which is also responsible |
||
317 | * for the the number of retries. |
||
318 | * |
||
319 | * @param array $retryTimeouts The number of retries with their timeouts |
||
320 | * |
||
321 | * @return void |
||
322 | */ |
||
323 | public function setRetryTimeouts(array $retryTimeouts) |
||
327 | |||
328 | /** |
||
329 | * Return's the array with the retry timeouts. |
||
330 | * |
||
331 | * @return array The retry timeouts |
||
332 | */ |
||
333 | public function getRetryTimeouts() |
||
337 | |||
338 | /** |
||
339 | * Return's the timeout for the given retry. |
||
340 | * |
||
341 | * @param integer $retry The retry to return the timeout for |
||
342 | * |
||
343 | * @return integer The timeout in seconds for the passed retry |
||
344 | * @throws \InvalidArgumentException Is thrown if the timeout for the passed retry is NOT available |
||
345 | */ |
||
346 | public function getRetryTimeout($retry) |
||
357 | |||
358 | /** |
||
359 | * Return's the number of retries for this message. |
||
360 | * |
||
361 | * @return integer The number of retries |
||
362 | */ |
||
363 | public function getRetryCounter() |
||
367 | |||
368 | /** |
||
369 | * Add's the callback for the given state. |
||
370 | * |
||
371 | * @param \AppserverIo\Psr\Pms\StateKeyInterface $state The state to register the callback for |
||
372 | * @param array $callback The array with the bean's lookup and method name that has to be invoked |
||
373 | * |
||
374 | * @return void |
||
375 | * @throws \Exception Is thrown if the passed state doesn't support callbacks |
||
376 | */ |
||
377 | public function addCallback(StateKeyInterface $state, array $callback) |
||
378 | { |
||
379 | |||
380 | // query whether or not the state supports callbacks |
||
381 | if (in_array($state->getState(), array(StateFailed::KEY, StateProcessed::KEY, StatePaused::KEY))) { |
||
382 | // initialize the array with the state callbacks, if not already done |
||
383 | if (!isset($this->callbacks[$state->getState()])) { |
||
384 | $this->callbacks[$state->getState()] = array(); |
||
385 | } |
||
386 | |||
387 | // add the callback to the state |
||
388 | array_push($this->callbacks[$state->getState()], $callback); |
||
389 | |||
390 | // return immediately |
||
391 | return; |
||
392 | } |
||
393 | |||
394 | // throw an exception, if the passed state doesn't support callbacks |
||
395 | throw new \Exception( |
||
396 | sprintf( |
||
397 | 'Callbacks for state %s is not supported, state has to be either one of StateFailed, StateProcessed or StatePaused', |
||
398 | (new ReflectionObject($state))->getShortName() |
||
399 | ) |
||
400 | ); |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * Return's the callback information for the given state. |
||
405 | * |
||
406 | * @param \AppserverIo\Psr\Pms\StateKeyInterface $state The state to register the callback for |
||
407 | * |
||
408 | * @return array The array with the callback information |
||
409 | */ |
||
410 | public function getCallbacks(StateKeyInterface $state) |
||
411 | { |
||
412 | |||
413 | // initialize the array with the callbacks |
||
414 | $callbacks = array(); |
||
415 | |||
416 | // try to find the callback for the passed state |
||
417 | if (isset($this->callbacks[$state->getState()])) { |
||
418 | $callbacks = $this->callbacks[$state->getState()]; |
||
419 | } |
||
420 | |||
421 | // return the array with the callbacks |
||
422 | return $callbacks; |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * Return's whether or not callbacks for the passed state has been registered. |
||
427 | * |
||
428 | * @param \AppserverIo\Psr\Pms\StateKeyInterface $state The state to register the callback for |
||
429 | * |
||
430 | * @return boolean TRUE if callbacks has been registered, else FALSE |
||
431 | */ |
||
432 | public function hasCallbacks(StateKeyInterface $state) |
||
436 | } |
||
437 |