How to enqueue a JMS message into Oracle AQ using Java

Viewed 17196

I have an Oracle AQ with the queue type of SYS.AQ$_JMS_TEXT_MESSAGE. What I'm trying to do is to insert a text into the mentioned queue from a java application.

The equivalent SQL query is

declare
 r_enqueue_options    DBMS_AQ.ENQUEUE_OPTIONS_T;
 r_message_properties DBMS_AQ.MESSAGE_PROPERTIES_T;
 v_message_handle     RAW(16);
 o_payload            SYS.AQ$_JMS_TEXT_MESSAGE;
begin
 o_payload := sys.aq$_jms_text_message.construct;
 o_payload.set_text(xmltype('<user>text</user>').getClobVal());
 sys.dbms_aq.enqueue (
   queue_name         => 'QUEUE_NAME',
   enqueue_options    => r_enqueue_options,
   message_properties => r_message_properties,
   payload            => o_payload,
   msgid              => v_message_handle
 );
 commit;
end;
/

I got most of it right using this guide, but I'm stuck at

 o_payload := sys.aq$_jms_text_message.construct;
 o_payload.set_text(xmltype('<user>text</user>').getClobVal());

The guide shows how to enqueue a RAW message, but I need it to be JMS, otherwise the data type doesn't match the queue type.

Any help would be appreciated, because even with the almighty google I am not able to find a solution to this problem. Is there a way to do it using the oracle.jdbc.aq classes, or do I just have to suck it up and use the SQL query?

2 Answers

I will add some tidbits to the answer of @Chathura Kulasinghe.

First, in the consumeMessage method, using the

Session.CLIENT_ACKNOWLEDGE

parameter for creating the session object will have the effect of leaving the message you consume in the queue. If you run this program many times, you will see the number of message going up in the database table of the queue. To remove a message, you need to « acknowledge » it by calling this method on the message object:

msg.acknowledge();

Second, if you want the session do this for you, simply change the client acknowledge mode to :

Session.AUTO_ACKNOWLEDGE

With this parameter, everytime your consumer.receive() is call, it's auto acknowledge and so, removed from the queue.

Related