|
| 1 | +-- Copyright (c) 2021 Oracle and/or its affiliates. |
| 2 | +-- Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ |
| 3 | + |
| 4 | +CREATE OR REPLACE PROCEDURE inventory_plsql |
| 5 | +IS |
| 6 | + dequeue_options dbms_aq.dequeue_options_t; |
| 7 | + enqueue_options dbms_aq.enqueue_options_t; |
| 8 | + message_properties dbms_aq.message_properties_t; |
| 9 | + message_handle RAW(16); |
| 10 | + message SYS.AQ$_JMS_TEXT_MESSAGE; |
| 11 | + |
| 12 | + order_inv_id VARCHAR2(23767); |
| 13 | + order_inv_loc VARCHAR2(23767); |
| 14 | + order_json JSON_OBJECT_T; |
| 15 | + inventory_json JSON_OBJECT_T; |
| 16 | +BEGIN |
| 17 | + LOOP |
| 18 | + -- Wait for and dequeue the next order message |
| 19 | + dequeue_options.wait := dbms_aq.FOREVER; |
| 20 | + DBMS_AQ.DEQUEUE( |
| 21 | + queue_name => 'ORDERQUEUE', |
| 22 | + dequeue_options => dequeue_options, |
| 23 | + message_properties => message_properties, |
| 24 | + payload => message, |
| 25 | + msgid => message_handle); |
| 26 | + |
| 27 | + -- Parse the order message |
| 28 | + order_json := JSON_OBJECT_T.parse(message.text_vc); |
| 29 | + order_inv_id := order_json.get_string('itemid'); |
| 30 | + |
| 31 | + -- Check the inventory |
| 32 | + update INVENTORYUSER.INVENTORY set inventorycount = inventorycount - 1 |
| 33 | + where inventoryid = order_inv_id and inventorycount > 0 returning inventorylocation into order_inv_loc; |
| 34 | + if sql%rowcount = 0 then |
| 35 | + order_inv_loc := 'inventorydoesnotexist'; |
| 36 | + end if; |
| 37 | + |
| 38 | + -- Construct the inventory message |
| 39 | + inventory_json := new JSON_OBJECT_T; |
| 40 | + inventory_json.put('orderid', order_json.get_string('orderid')); |
| 41 | + inventory_json.put('itemid', order_inv_id); |
| 42 | + inventory_json.put('inventorylocation', order_inv_loc); |
| 43 | + inventory_json.put('suggestiveSale', 'beer'); |
| 44 | + |
| 45 | + -- Send the inventory message |
| 46 | + message := SYS.AQ$_JMS_TEXT_MESSAGE.construct; |
| 47 | + message.set_text(inventory_json.to_string()); |
| 48 | + DBMS_AQ.ENQUEUE(queue_name => 'INVENTORYQUEUE', |
| 49 | + enqueue_options => enqueue_options, |
| 50 | + message_properties => message_properties, |
| 51 | + payload => message, |
| 52 | + msgid => message_handle); |
| 53 | + |
| 54 | + -- commit |
| 55 | + commit; |
| 56 | + END LOOP; |
| 57 | +END; |
| 58 | +/ |
0 commit comments