-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathaq.py
625 lines (539 loc) · 21.6 KB
/
aq.py
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# -----------------------------------------------------------------------------
# Copyright (c) 2021, 2025, Oracle and/or its affiliates.
#
# This software is dual-licensed to you under the Universal Permissive License
# (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
# 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
# either license.
#
# If you elect to accept the software under the Apache License, Version 2.0,
# the following applies:
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# aq.py
#
# Contains the classes used for handling Advanced Queuing (AQ): Queue,
# DeqOptions, EnqOptions and MessageProperties.
# -----------------------------------------------------------------------------
import datetime
from . import connection as connection_module
from typing import Any, Union, List
from . import errors
from .dbobject import DbObject, DbObjectType
class BaseQueue:
@classmethod
def _from_impl(cls, connection, impl):
queue = cls.__new__(cls)
queue._connection = connection
queue._deq_options = DeqOptions._from_impl(impl.deq_options_impl)
queue._enq_options = EnqOptions._from_impl(impl.enq_options_impl)
queue._payload_type = None
queue._impl = impl
return queue
def _verify_message(self, message: "MessageProperties") -> None:
"""
Internal method used for verifying a message.
"""
if not isinstance(message, MessageProperties):
raise TypeError("expecting MessageProperties object")
if message.payload is None:
errors._raise_err(errors.ERR_MESSAGE_HAS_NO_PAYLOAD)
if isinstance(self.payload_type, DbObjectType):
if (
not isinstance(message.payload, DbObject)
or message.payload.type != self.payload_type
):
errors._raise_err(errors.ERR_PAYLOAD_CANNOT_BE_ENQUEUED)
elif self.payload_type == "JSON":
if not isinstance(message.payload, (dict, list)):
errors._raise_err(errors.ERR_PAYLOAD_CANNOT_BE_ENQUEUED)
else:
if not isinstance(message.payload, (str, bytes)):
errors._raise_err(errors.ERR_PAYLOAD_CANNOT_BE_ENQUEUED)
if self.connection.thin:
if message.recipients:
errors._raise_not_supported("specifying AQ message recipients")
@property
def connection(self) -> "connection_module.Connection":
"""
Returns the connection on which the queue was created.
"""
return self._connection
@property
def deqoptions(self) -> "DeqOptions":
"""
Returns the options that will be used when dequeuing messages from the
queue.
"""
return self._deq_options
@property
def deqOptions(self) -> "DeqOptions":
"""
Deprecated: use deqoptions instead.
"""
return self.deqoptions
@property
def enqoptions(self) -> "EnqOptions":
"""
Returns the options that will be used when enqueuing messages into the
queue.
"""
return self._enq_options
@property
def enqOptions(self) -> "EnqOptions":
"""
Deprecated: use enqoptions() instead.
"""
return self.enqoptions
@property
def name(self) -> str:
"""
Returns the name of the queue.
"""
return self._impl.name
@property
def payload_type(self) -> Union[DbObjectType, None]:
"""
Returns the object type for payloads that can be enqueued and dequeued.
If using a raw queue, this returns the value None.
"""
if self._payload_type is None:
if self._impl.is_json:
self._payload_type = "JSON"
elif self._impl.payload_type is not None:
self._payload_type = DbObjectType._from_impl(
self._impl.payload_type
)
return self._payload_type
@property
def payloadType(self) -> Union[DbObjectType, None]:
"""
Deprecated: use payload_type instead.
"""
return self.payload_type
class Queue(BaseQueue):
def deqmany(self, max_num_messages: int) -> list:
"""
Dequeues up to the specified number of messages from the queue and
returns a list of these messages.
"""
if self._impl._supports_deq_many(self._connection._impl):
message_impls = self._impl.deq_many(max_num_messages)
else:
message_impls = []
while len(message_impls) < max_num_messages:
message_impl = self._impl.deq_one()
if message_impl is None:
break
message_impls.append(message_impl)
return [MessageProperties._from_impl(impl) for impl in message_impls]
def deqMany(self, max_num_messages: int) -> List["MessageProperties"]:
"""
Deprecated: use deqmany() instead.
"""
return self.deqmany(max_num_messages)
def deqone(self) -> Union["MessageProperties", None]:
"""
Dequeues at most one message from the queue and returns it. If no
message is dequeued, None is returned.
"""
message_impl = self._impl.deq_one()
if message_impl is not None:
return MessageProperties._from_impl(message_impl)
def deqOne(self) -> Union["MessageProperties", None]:
"""
Deprecated: use deqone() instead.
"""
return self.deqone()
def enqmany(self, messages: list) -> None:
"""
Enqueues multiple messages into the queue. The messages parameter must
be a sequence containing message property objects which have all had
their payload attribute set to a value that the queue supports.
Warning: calling this function in parallel on different connections
acquired from the same pool may fail due to Oracle bug 29928074. Ensure
that this function is not run in parallel, use standalone connections
or connections from different pools, or make multiple calls to
enqone() instead. The function Queue.deqmany() call is not affected.
"""
for message in messages:
self._verify_message(message)
message_impls = [m._impl for m in messages]
self._impl.enq_many(message_impls)
def enqMany(self, messages: list) -> None:
"""
Deprecated: use enqmany() instead.
"""
return self.enqmany(messages)
def enqone(self, message: "MessageProperties") -> None:
"""
Enqueues a single message into the queue. The message must be a message
property object which has had its payload attribute set to a value that
the queue supports.
"""
self._verify_message(message)
self._impl.enq_one(message._impl)
def enqOne(self, message: "MessageProperties") -> None:
"""
Deprecated: use enqone() instead.
"""
return self.enqone(message)
class AsyncQueue(BaseQueue):
async def deqmany(self, max_num_messages: int) -> list:
"""
Dequeues up to the specified number of messages from the queue and
returns a list of these messages.
"""
message_impls = await self._impl.deq_many(max_num_messages)
return [MessageProperties._from_impl(impl) for impl in message_impls]
async def deqone(self) -> Union["MessageProperties", None]:
"""
Dequeues at most one message from the queue and returns it. If no
message is dequeued, None is returned.
"""
message_impl = await self._impl.deq_one()
if message_impl is not None:
return MessageProperties._from_impl(message_impl)
async def enqmany(self, messages: list) -> None:
"""
Enqueues multiple messages into the queue. The messages parameter must
be a sequence containing message property objects which have all had
their payload attribute set to a value that the queue supports.
Warning: calling this function in parallel on different connections
acquired from the same pool may fail due to Oracle bug 29928074. Ensure
that this function is not run in parallel, use standalone connections
or connections from different pools, or make multiple calls to
enqone() instead. The function Queue.deqmany() call is not affected.
"""
for message in messages:
self._verify_message(message)
message_impls = [m._impl for m in messages]
await self._impl.enq_many(message_impls)
async def enqone(self, message: "MessageProperties") -> None:
"""
Enqueues a single message into the queue. The message must be a message
property object which has had its payload attribute set to a value that
the queue supports.
"""
self._verify_message(message)
await self._impl.enq_one(message._impl)
class DeqOptions:
@classmethod
def _from_impl(cls, impl):
options = cls.__new__(cls)
options._impl = impl
return options
@property
def condition(self) -> str:
"""
Specifies a boolean expression similar to the where clause of a SQL
query. The boolean expression can include conditions on message
properties, user data properties and PL/SQL or SQL functions. The
default is to have no condition specified.
"""
return self._impl.get_condition()
@condition.setter
def condition(self, value: str) -> None:
self._impl.set_condition(value)
@property
def consumername(self) -> str:
"""
Specifies the name of the consumer. Only messages matching the consumer
name will be accessed. If the queue is not set up for multiple
consumers this attribute should not be set. The default is to have no
consumer name specified.
"""
return self._impl.get_consumer_name()
@consumername.setter
def consumername(self, value: str) -> None:
self._impl.set_consumer_name(value)
@property
def correlation(self) -> str:
"""
Specifies the correlation identifier of the message to be dequeued.
Special pattern-matching characters, such as the percent sign (%) and
the underscore (_), can be used. If multiple messages satisfy the
pattern, the order of dequeuing is indeterminate. The default is to
have no correlation specified.
"""
return self._impl.get_correlation()
@correlation.setter
def correlation(self, value: str) -> None:
self._impl.set_correlation(value)
@property
def deliverymode(self) -> None:
"""
Specifies what types of messages should be dequeued. It should be one
of the values MSG_PERSISTENT (default), MSG_BUFFERED or
MSG_PERSISTENT_OR_BUFFERED.
"""
raise AttributeError("deliverymode can only be written")
@deliverymode.setter
def deliverymode(self, value: int) -> None:
self._impl.set_delivery_mode(value)
@property
def mode(self) -> int:
"""
Specifies the locking behaviour associated with the dequeue operation.
It should be one of the values DEQ_BROWSE, DEQ_LOCKED, DEQ_REMOVE
(default), or DEQ_REMOVE_NODATA.
"""
return self._impl.get_mode()
@mode.setter
def mode(self, value: int) -> None:
self._impl.set_mode(value)
@property
def msgid(self) -> bytes:
"""
Specifies the identifier of the message to be dequeued. The default is
to have no message identifier specified.
"""
return self._impl.get_message_id()
@msgid.setter
def msgid(self, value: bytes) -> None:
self._impl.set_message_id(value)
@property
def navigation(self) -> int:
"""
Specifies the position of the message that is retrieved. It should be
one of the values DEQ_FIRST_MSG, DEQ_NEXT_MSG (default), or
DEQ_NEXT_TRANSACTION.
"""
return self._impl.get_navigation()
@navigation.setter
def navigation(self, value: int) -> None:
self._impl.set_navigation(value)
@property
def transformation(self) -> str:
"""
Specifies the name of the transformation that must be applied after the
message is dequeued from the database but before it is returned to the
calling application. The transformation must be created using
dbms_transform. The default is to have no transformation specified.
"""
return self._impl.get_transformation()
@transformation.setter
def transformation(self, value: str) -> None:
self._impl.set_transformation(value)
@property
def visibility(self) -> int:
"""
Specifies the transactional behavior of the dequeue request. It should
be one of the values DEQ_ON_COMMIT (default) or DEQ_IMMEDIATE. This
attribute is ignored when using the DEQ_BROWSE mode. Note the value of
autocommit is always ignored.
"""
return self._impl.get_visibility()
@visibility.setter
def visibility(self, value: int) -> None:
self._impl.set_visibility(value)
@property
def wait(self) -> int:
"""
Specifies the time to wait, in seconds, for a message matching the
search criteria to become available for dequeuing. One of the values
DEQ_NO_WAIT or DEQ_WAIT_FOREVER can also be used. The default is
DEQ_WAIT_FOREVER.
"""
return self._impl.get_wait()
@wait.setter
def wait(self, value: int) -> None:
self._impl.set_wait(value)
class EnqOptions:
@classmethod
def _from_impl(cls, impl):
options = cls.__new__(cls)
options._impl = impl
return options
@property
def deliverymode(self) -> int:
"""
Specifies what type of messages should be enqueued. It should be one of
the values MSG_PERSISTENT (default) or MSG_BUFFERED.
"""
raise AttributeError("deliverymode can only be written")
@deliverymode.setter
def deliverymode(self, value: int) -> None:
self._impl.set_delivery_mode(value)
@property
def transformation(self) -> str:
"""
Specifies the name of the transformation that must be applied before
the message is enqueued into the database. The transformation must be
created using dbms_transform. The default is to have no transformation
specified.
"""
return self._impl.get_transformation()
@transformation.setter
def transformation(self, value: str) -> None:
self._impl.set_transformation(value)
@property
def visibility(self) -> int:
"""
Specifies the transactional behavior of the enqueue request. It should
be one of the values ENQ_ON_COMMIT (default) or ENQ_IMMEDIATE. Note the
value of autocommit is ignored.
"""
return self._impl.get_visibility()
@visibility.setter
def visibility(self, value: int) -> None:
self._impl.set_visibility(value)
class MessageProperties:
_recipients = []
@classmethod
def _from_impl(cls, impl):
props = cls.__new__(cls)
props._impl = impl
return props
@property
def attempts(self) -> int:
"""
Specifies the number of attempts that have been made to dequeue the
message.
"""
return self._impl.get_num_attempts()
@property
def correlation(self) -> str:
"""
Specifies the correlation used when the message was enqueued.
"""
return self._impl.get_correlation()
@correlation.setter
def correlation(self, value: str) -> None:
self._impl.set_correlation(value)
@property
def delay(self) -> int:
"""
Specifies the number of seconds to delay an enqueued message. Any
integer is acceptable but the constant MSG_NO_DELAY can also be used
indicating that the message is available for immediate dequeuing.
"""
return self._impl.get_delay()
@delay.setter
def delay(self, value: int) -> None:
self._impl.set_delay(value)
@property
def deliverymode(self) -> int:
"""
Specifies the type of message that was dequeued. It will be one of the
values MSG_PERSISTENT or MSG_BUFFERED.
"""
return self._impl.get_delivery_mode()
@property
def enqtime(self) -> datetime.datetime:
"""
Specifies the time that the message was enqueued.
"""
return self._impl.get_enq_time()
@property
def exceptionq(self) -> str:
"""
Specifies the name of the queue to which the message is moved if it
cannot be processed successfully. Messages are moved if the number of
unsuccessful dequeue attempts has exceeded the maximum number of
retries or if the message has expired. All messages in the exception
queue are in the MSG_EXPIRED state. The default value is the name of
the exception queue associated with the queue table.
"""
return self._impl.get_exception_queue()
@exceptionq.setter
def exceptionq(self, value: str) -> None:
self._impl.set_exception_queue(value)
@property
def expiration(self) -> int:
"""
Specifies, in seconds, how long the message is available for dequeuing.
This attribute is an offset from the delay attribute. Expiration
processing requires the queue monitor to be running. Any integer is
accepted but the constant MSG_NO_EXPIRATION can also be used indicating
that the message never expires.
"""
return self._impl.get_expiration()
@expiration.setter
def expiration(self, value: int) -> None:
self._impl.set_expiration(value)
@property
def msgid(self) -> bytes:
"""
Specifies the id of the message in the last queue that enqueued or
dequeued this message. If the message has never been dequeued or
enqueued, the value will be `None`.
"""
return self._impl.get_message_id()
@property
def payload(self) -> Union[bytes, DbObject]:
"""
Specifies the payload that will be enqueued or the payload that was
dequeued when using a queue. When enqueuing, the value is checked to
ensure that it conforms to the type expected by that queue. For RAW
queues, the value can be a bytes object or a string. If the value is a
string it will be converted to bytes in the encoding UTF-8.
"""
return self._impl.payload
@payload.setter
def payload(self, value: Any) -> None:
if isinstance(value, DbObject):
self._impl.set_payload_object(value._impl)
elif not isinstance(value, (str, bytes)):
self._impl.set_payload_json(value)
else:
if isinstance(value, str):
value_bytes = value.encode()
elif isinstance(value, bytes):
value_bytes = value
self._impl.set_payload_bytes(value_bytes)
self._impl.payload = value
@property
def priority(self) -> int:
"""
Specifies the priority of the message. A smaller number indicates a
higher priority. The priority can be any integer, including negative
numbers. The default value is zero.
"""
return self._impl.get_priority()
@priority.setter
def priority(self, value: int) -> None:
self._impl.set_priority(value)
@property
def recipients(self) -> list:
"""
A list of recipient names can be associated with a message at the time
a message is enqueued. This allows a limited set of recipients to
dequeue each message. The recipient list associated with the message
overrides the queue subscriber list, if there is one. The recipient
names need not be in the subscriber list but can be, if desired.
To dequeue a message, the consumername attribute can be set to one of
the recipient names. The original message recipient list is not
available on dequeued messages. All recipients have to dequeue a
message before it gets removed from the queue.
Subscribing to a queue is like subscribing to a magazine: each
subscriber can dequeue all the messages placed into a specific queue,
just as each magazine subscriber has access to all its articles. Being
a recipient, however, is like getting a letter: each recipient is a
designated target of a particular message.
"""
return self._recipients
@recipients.setter
def recipients(self, value: list) -> None:
self._impl.set_recipients(value)
self._recipients = value
@property
def state(self) -> int:
"""
Specifies the state of the message at the time of the dequeue. It will
be one of the values MSG_WAITING, MSG_READY, MSG_PROCESSED or
MSG_EXPIRED.
"""
return self._impl.get_state()