@@ -18,14 +18,22 @@ architecture.
1818Python-oracledb uses the updated interface for Oracle Advanced Queuing that
1919was first introduced in cx_Oracle 7.2.
2020
21+ Starting from Oracle Database 21c, Advanced Queuing also supports the JSON
22+ payload type. To use the JSON payload type, the Oracle Client libraries must
23+ be version 21 or later.
24+
2125There are Advanced Queuing examples in the `GitHub examples
2226<https://github.com/oracle/python-oracledb/tree/main/samples> `__ directory.
2327
2428
2529Creating a Queue
2630================
2731
28- Before being used, queues need to be created in the database, for example in
32+ Before being used, queues need to be created in the database.
33+
34+ **Using RAW Payloads **
35+
36+ Queues can be created using the RAW payload type, for example in
2937SQL*Plus:
3038
3139.. code-block :: sql
@@ -37,16 +45,37 @@ SQL*Plus:
3745 end;
3846 /
3947
40- This examples creates a RAW queue suitable for sending string or raw bytes
48+ This example creates a RAW queue suitable for sending string or bytes
4149messages.
4250
51+ **Using JSON Payloads **
52+
53+ Also, queues can be created using the JSON payload type. For example,
54+ in SQL*Plus:
55+
56+ .. code-block :: sql
57+
58+ begin
59+ dbms_aqadm.create_queue_table('JSON_QUEUE_TABLE', 'JSON');
60+ dbms_aqadm.create_queue('DEMO_JSON_QUEUE', 'JSON_QUEUE_TABLE');
61+ dbms_aqadm.start_queue('DEMO_JSON_QUEUE');
62+ end;
63+ /
64+
65+ This example creates a JSON queue suitable for sending JSON data
66+ messages.
4367
4468Enqueuing Messages
4569==================
4670
4771To send messages in Python, you connect and get a :ref: `queue <queue >`. The
4872queue can be used for enqueuing, dequeuing, or both as needed.
4973
74+ **Using RAW Payloads **
75+
76+ You can connect to the database and get the queue that was created with RAW
77+ payload type by using:
78+
5079.. code-block :: python
5180
5281 queue = connection.queue(" DEMO_RAW_QUEUE" )
@@ -66,24 +95,63 @@ messages:
6695 connection.commit()
6796
6897 Since the queue sending the messages is a RAW queue, the strings in this
69- example will be internally encoded to bytes using :attr: ` Connection.encoding `
98+ example will be internally encoded to bytes using `` message.encode() ` `
7099before being enqueued.
71100
101+ **Using JSON Payloads **
102+
103+ You can connect to the database and get the queue that was created with JSON
104+ payload type by using:
105+
106+ .. code-block :: python
107+
108+ queue = connection.queue(" DEMO_JSON_QUEUE" , " JSON" )
109+ # The second argument (JSON) indicates that the queue is of JSON payload type.
110+
111+ Now the message can be enqueued using :meth: `~Queue.enqone() `.
112+
113+ .. code-block :: python
114+
115+ json_data = [
116+ [
117+ 2.75 ,
118+ True ,
119+ ' Ocean Beach' ,
120+ b ' Some bytes' ,
121+ {' keyA' : 1.0 , ' KeyB' : ' Melbourne' },
122+ datetime.datetime(2022 , 8 , 1 , 0 , 0 )
123+ ],
124+ dict (name = " John" , age = 30 , city = " New York" )
125+ ]
126+ for data in json_data:
127+ queue.enqone(connection.msgproperties(payload = data))
128+ connection.commit()
72129
73130 Dequeuing Messages
74131==================
75132
76133Dequeuing is performed similarly. To dequeue a message call the method
77- :meth: `~Queue.deqone() ` as shown. Note that if the message is expected to be a
78- string, the bytes must be decoded using :attr: `Connection.encoding `.
134+ :meth: `~Queue.deqone() ` as shown in the examples below.
135+
136+ **Using RAW Payload Type **
79137
80138.. code-block :: python
81139
82140 queue = connection.queue(" DEMO_RAW_QUEUE" )
83- msg = queue.deqOne()
141+ message = queue.deqOne()
84142 connection.commit()
85- print (msg.payload.decode(connection.encoding))
143+ print (message.payload.decode())
144+
145+ Note that if the message is expected to be a string, the bytes must
146+ be decoded using ``message.payload.decode() ``, as shown.
147+
148+ **Using JSON Payload Type **
86149
150+ .. code-block :: python
151+
152+ queue = connection.queue(" DEMO_JSON_QUEUE" , " JSON" )
153+ message = queue.deqOne()
154+ connection.commit()
87155
88156 Using Object Queues
89157===================
@@ -133,9 +201,9 @@ Dequeuing is done like this:
133201 book_type = connection.gettype(" UDT_BOOK" )
134202 queue = connection.queue(" DEMO_BOOK_QUEUE" , book_type)
135203
136- msg = queue.deqone()
204+ message = queue.deqone()
137205 connection.commit()
138- print (msg .payload.TITLE ) # will print Quick Brown Fox
206+ print (message .payload.TITLE ) # will print Quick Brown Fox
139207
140208
141209 Using Recipient Lists
@@ -241,8 +309,8 @@ time:
241309
242310.. code-block :: python
243311
244- for m in queue.deqmany(10 ):
245- print (m .payload.decode(connection.encoding ))
312+ for message in queue.deqmany(10 ):
313+ print (message .payload.decode())
246314
247315 Depending on the queue properties and the number of messages available to
248316dequeue, this code will print out from zero to ten messages.
0 commit comments