Skip to content

Commit d4498cf

Browse files
Added support for Advanced Queueing RAW queues and bulk enqueue/dequeue.
1 parent 04a7dec commit d4498cf

15 files changed

+967
-93
lines changed

doc/src/aq.rst

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,76 @@
44
Advanced Queuing
55
****************
66

7+
.. note::
8+
9+
All of these objects are extensions to the DB API.
10+
11+
.. _queue:
12+
13+
------
14+
Queues
15+
------
16+
17+
Queues are created using the :meth:`Connection.queue()` method and are used to
18+
enqueue and dequeue messages.
19+
20+
.. attribute:: Queue.connection
21+
22+
This read-only attribute returns a reference to the connection object on
23+
which the queue was created.
24+
25+
26+
.. method:: Queue.deqMany(maxMessages)
27+
28+
Dequeues up to the specified number of messages from the queue and returns
29+
a list of these messages. Each element of the returned list is a
30+
:ref:`message property<msgproperties>` object.
31+
32+
33+
.. method:: Queue.deqOne()
34+
35+
Dequeues at most one message from the queue. If a message is dequeued, it
36+
will be a :ref:`message property<msgproperties>` object; otherwise, it will
37+
be the value None.
38+
39+
.. attribute:: Queue.deqOptions
40+
41+
This read-only attribute returns a reference to the :ref:`options
42+
<deqoptions>` that will be used when dequeuing messages from the queue.
43+
44+
45+
.. method:: Queue.enqOne(message)
46+
47+
Enqueues a single message into the queue. The message must be a
48+
:ref:`message property<msgproperties>` object which has had its payload
49+
attribute set to a value that the queue supports.
50+
51+
52+
.. method:: Queue.enqMany(messages)
53+
54+
Enqueues multiple messages into the queue. The messages parameter must be a
55+
sequence containing :ref:`message property <msgproperties>` objects which
56+
have all had their payload attribute set to a value that the queue
57+
supports.
58+
59+
60+
.. attribute:: Queue.enqOptions
61+
62+
This read-only attribute returns a reference to the :ref:`options
63+
<enqoptions>` that will be used when enqueuing messages into the queue.
64+
65+
66+
.. attribute:: Queue.name
67+
68+
This read-only attribute returns the name of the queue.
69+
70+
71+
.. attribute:: Queue.payloadType
72+
73+
This read-only attribute returns the object type for payloads that can be
74+
enqueued and dequeued. If using a raw queue, this returns the value None.
75+
76+
777
.. _deqoptions:
878

979
---------------
@@ -205,6 +275,14 @@ Message Properties
205275
generated this message.
206276

207277

278+
.. attribute:: MessageProperties.payload
279+
280+
This attribute identifies the payload that will be enqueued or the payload
281+
that was dequeued when using a :ref:`queue <queue>`. When enqueuing, the
282+
value is checked to ensure that it conforms to the type expected by that
283+
queue.
284+
285+
208286
.. attribute:: MessageProperties.priority
209287

210288
This attribute specifies the priority of the message. A smaller number

doc/src/connection.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ Connection Object
193193

194194
.. versionadded:: 5.3
195195

196+
.. deprecated:: 7.2
197+
198+
Use the methods :meth:`Queue.deqOne()` or :meth:`Queue.deqMany()`
199+
instead.
200+
196201
.. note::
197202

198203
This method is an extension to the DB API definition.
@@ -205,6 +210,10 @@ Connection Object
205210

206211
.. versionadded:: 5.3
207212

213+
.. deprecated:: 7.2
214+
215+
Use the attribute :attr:`Queue.deqOptions` instead.
216+
208217
.. note::
209218

210219
This method is an extension to the DB API definition.
@@ -253,6 +262,11 @@ Connection Object
253262

254263
.. versionadded:: 5.3
255264

265+
.. deprecated:: 7.2
266+
267+
Use the methods :meth:`Queue.enqOne()` or :meth:`Queue.enqMany()`
268+
instead.
269+
256270
.. note::
257271

258272
This method is an extension to the DB API definition.
@@ -265,6 +279,10 @@ Connection Object
265279

266280
.. versionadded:: 5.3
267281

282+
.. deprecated:: 7.2
283+
284+
Use the attribute :attr:`Queue.enqOptions` instead.
285+
268286
.. note::
269287

270288
This method is an extension to the DB API definition.
@@ -447,6 +465,25 @@ Connection Object
447465
This method is an extension to the DB API definition.
448466

449467

468+
.. method:: Connection.queue(name, payloadType=None)
469+
470+
Creates a :ref:`queue <queue>` which is used to enqueue and dequeue
471+
messages in Advanced Queueing.
472+
473+
The name parameter is expected to be a string identifying the queue in
474+
which messages are to be enqueued or dequeued.
475+
476+
The payloadType parameter, if specified, is expected to be an
477+
:ref:`object type <objecttype>` that identifies the type of payload the
478+
queue expects. If not specified, RAW data is enqueued and dequeued.
479+
480+
.. versionadded:: 7.2
481+
482+
.. note::
483+
484+
This method is an extension to the DB API definition.
485+
486+
450487
.. method:: Connection.rollback()
451488

452489
Rollback any pending transactions.

samples/AdvancedQueuing.py

Lines changed: 0 additions & 64 deletions
This file was deleted.

samples/BulkAQ.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#------------------------------------------------------------------------------
2+
# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
#
4+
# Portions Copyright 2007-2015, Anthony Tuininga. All rights reserved.
5+
#
6+
# Portions Copyright 2001-2007, Computronix (Canada) Ltd., Edmonton, Alberta,
7+
# Canada. All rights reserved.
8+
#------------------------------------------------------------------------------
9+
10+
#------------------------------------------------------------------------------
11+
# BulkAQ.py
12+
# This script demonstrates how to use bulk enqueuing and dequeuing of
13+
# messages with advanced queuing using cx_Oracle. It makes use of a RAW queue
14+
# created in the sample setup.
15+
#
16+
# This script requires cx_Oracle 7.2 and higher.
17+
#------------------------------------------------------------------------------
18+
19+
from __future__ import print_function
20+
21+
import cx_Oracle
22+
import SampleEnv
23+
24+
QUEUE_NAME = "DEMORAW"
25+
PAYLOAD_DATA = [
26+
"The first message",
27+
"The second message",
28+
"The third message",
29+
"The fourth message",
30+
"The fifth message",
31+
"The sixth message",
32+
"The seventh message",
33+
"The eighth message",
34+
"The ninth message",
35+
"The tenth message",
36+
"The eleventh message",
37+
"The twelfth and final message"
38+
]
39+
40+
# connect to database
41+
connection = cx_Oracle.connect(SampleEnv.GetMainConnectString())
42+
cursor = connection.cursor()
43+
44+
# create queue
45+
queue = connection.queue(QUEUE_NAME)
46+
queue.deqOptions.wait = cx_Oracle.DEQ_NO_WAIT
47+
queue.deqOptions.navigation = cx_Oracle.DEQ_FIRST_MSG
48+
49+
# dequeue all existing messages to ensure the queue is empty, just so that
50+
# the results are consistent
51+
while queue.deqOne():
52+
pass
53+
54+
# enqueue a few messages
55+
print("Enqueuing messages...")
56+
batchSize = 6
57+
dataToEnq = PAYLOAD_DATA
58+
while dataToEnq:
59+
batchData = dataToEnq[:batchSize]
60+
dataToEnq = dataToEnq[batchSize:]
61+
messages = [connection.msgproperties(payload=d) for d in batchData]
62+
for data in batchData:
63+
print(data)
64+
queue.enqMany(messages)
65+
connection.commit()
66+
67+
# dequeue the messages
68+
print("\nDequeuing messages...")
69+
batchSize = 8
70+
while True:
71+
messages = queue.deqMany(batchSize)
72+
if not messages:
73+
break
74+
for props in messages:
75+
print(props.payload.decode())
76+
connection.commit()
77+
print("\nDone.")

samples/ObjectAQ.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#------------------------------------------------------------------------------
2+
# Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
3+
#
4+
# Portions Copyright 2007-2015, Anthony Tuininga. All rights reserved.
5+
#
6+
# Portions Copyright 2001-2007, Computronix (Canada) Ltd., Edmonton, Alberta,
7+
# Canada. All rights reserved.
8+
#------------------------------------------------------------------------------
9+
10+
#------------------------------------------------------------------------------
11+
# ObjectAQ.py
12+
# This script demonstrates how to use advanced queuing with objects using
13+
# cx_Oracle. It makes use of a simple type and queue created in the sample
14+
# setup.
15+
#
16+
# This script requires cx_Oracle 7.2 and higher.
17+
#------------------------------------------------------------------------------
18+
19+
from __future__ import print_function
20+
21+
import cx_Oracle
22+
import SampleEnv
23+
import decimal
24+
25+
BOOK_TYPE_NAME = "UDT_BOOK"
26+
QUEUE_NAME = "BOOKS"
27+
BOOK_DATA = [
28+
("The Fellowship of the Ring", "Tolkien, J.R.R.",
29+
decimal.Decimal("10.99")),
30+
("Harry Potter and the Philosopher's Stone", "Rowling, J.K.",
31+
decimal.Decimal("7.99"))
32+
]
33+
34+
# connect to database
35+
connection = cx_Oracle.connect(SampleEnv.GetMainConnectString())
36+
cursor = connection.cursor()
37+
38+
# create queue
39+
booksType = connection.gettype(BOOK_TYPE_NAME)
40+
queue = connection.queue(QUEUE_NAME, booksType)
41+
queue.deqOptions.wait = cx_Oracle.DEQ_NO_WAIT
42+
queue.deqOptions.navigation = cx_Oracle.DEQ_FIRST_MSG
43+
44+
# dequeue all existing messages to ensure the queue is empty, just so that
45+
# the results are consistent
46+
while queue.deqOne():
47+
pass
48+
49+
# enqueue a few messages
50+
print("Enqueuing messages...")
51+
for title, authors, price in BOOK_DATA:
52+
book = booksType.newobject()
53+
book.TITLE = title
54+
book.AUTHORS = authors
55+
book.PRICE = price
56+
print(title)
57+
queue.enqOne(connection.msgproperties(payload=book))
58+
connection.commit()
59+
60+
# dequeue the messages
61+
print("\nDequeuing messages...")
62+
while True:
63+
props = queue.deqOne()
64+
if not props:
65+
break
66+
print(props.payload.TITLE)
67+
connection.commit()
68+
print("\nDone.")

0 commit comments

Comments
 (0)