|
| 1 | +import os |
| 2 | +import logging |
| 3 | +from os import environ as env |
| 4 | +import cx_Oracle |
| 5 | +import threading |
| 6 | +import time |
| 7 | +import oci |
| 8 | +import base64 |
| 9 | + |
| 10 | +connection = cx_Oracle.connect(dsn=env.get('DB_ALIAS')) |
| 11 | +cursor = connection.cursor() |
| 12 | + |
| 13 | +#ADT payload |
| 14 | +book_type = connection.gettype("ADT_BOOK") |
| 15 | +queue = connection.queue("PYTHON_ADT_Q", book_type) |
| 16 | + |
| 17 | +book = book_type.newobject() |
| 18 | +book.TITLE = "Quick Brown Fox" |
| 19 | +book.AUTHORS = "The Dog" |
| 20 | +book.PRICE = 123 |
| 21 | +props = connection.msgproperties(payload=book, correlation="correlation-py", expiration=30, priority=7) |
| 22 | + |
| 23 | +print("1) Sample for Classic Queue : ADT payload") |
| 24 | +print("Enqueue one message with ADT payload : ",book.TITLE) |
| 25 | +queue.enqOne(props) |
| 26 | +connection.commit() |
| 27 | +print("Enqueue Done!!!") |
| 28 | + |
| 29 | +#deqOptions should have consumername in case of multiconsumer queue |
| 30 | +#queue.deqOptions.consumername = "PYTHON_ADT_SUBSCIBER" |
| 31 | +options = connection.deqoptions() |
| 32 | +options.wait = cx_Oracle.DEQ_NO_WAIT |
| 33 | +msg = queue.deqOne() |
| 34 | +connection.commit() |
| 35 | +print("Dequeued message with ADT payload : ",msg.payload.TITLE) |
| 36 | +print("Dequeue Done!!!") |
| 37 | +print("-----------------------------------------------------------------") |
| 38 | + |
| 39 | +#RAW PAYLOAD |
| 40 | +print("\n2) Sample for Classic queue : RAW payload") |
| 41 | + |
| 42 | +queue = connection.queue("PYTHON_RAW_Q") |
| 43 | +PAYLOAD_DATA = [ |
| 44 | + "The first message" |
| 45 | +] |
| 46 | +for data in PAYLOAD_DATA: |
| 47 | + print("Enqueue message with RAW payload : ",data) |
| 48 | + queue.enqone(connection.msgproperties(payload=data)) |
| 49 | + |
| 50 | +connection.commit() |
| 51 | +print("Enqueue Done!!!") |
| 52 | + |
| 53 | +msg = queue.deqOne() |
| 54 | +connection.commit() |
| 55 | +print(msg.payload.decode(connection.encoding)) |
| 56 | +print("Dequeued message with RAW payload : ",msg.payload) |
| 57 | +print("Dequeue Done!!!") |
| 58 | +print("-----------------------------------------------------------------") |
| 59 | + |
| 60 | +print("\n3) Sample for Classic queue : JMS payload") |
| 61 | +#get the JMS type |
| 62 | +jmsType = connection.gettype("SYS.AQ$_JMS_TEXT_MESSAGE") |
| 63 | +headerType = connection.gettype("SYS.AQ$_JMS_HEADER") |
| 64 | +user_prop_Type = connection.gettype("SYS.AQ$_JMS_USERPROPARRAY") |
| 65 | + |
| 66 | +queue = connection.queue("PYTHON_JMS_Q",jmsType) |
| 67 | +#create python object for JMS type |
| 68 | +text = jmsType.newobject() |
| 69 | +text.HEADER = headerType.newobject() |
| 70 | +text.TEXT_VC = "JMS text message" |
| 71 | +text.TEXT_LEN = 20 |
| 72 | +text.HEADER.APPID = "py-app-1" |
| 73 | +text.HEADER.GROUPID = "py-grp-1" |
| 74 | +text.HEADER.GROUPSEQ = 1 |
| 75 | +text.HEADER.PROPERTIES = user_prop_Type.newobject() |
| 76 | +print("Enqueue one message with JMS payload : ",text.TEXT_VC) |
| 77 | +queue.enqOne(connection.msgproperties(payload=text)) |
| 78 | +connection.commit() |
| 79 | +print("Enqueue Done!!!") |
| 80 | + |
| 81 | +msg = queue.deqOne() |
| 82 | +connection.commit() |
| 83 | +print("Dequeued message with JMS payload :",msg.payload.TEXT_VC) |
| 84 | +print("Dequeue Done!!!") |
| 85 | +print("-----------------------------------------------------------------") |
0 commit comments