使用Java for Oracle 11g队列出列队列 [英] Dequeue using Java for Oracle 11g queue

查看:224
本文介绍了使用Java for Oracle 11g队列出列队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用standlone java使用Oracle 11g队列出列队列。下面是代码:

I'm trying to dequeue using Oracle 11g queue using standlone java. Here is the code:

public class testq {
public static void main(String[] args) throws Exception {   
    testq q = new testq();      

    AQSession aq_sess = createSession();
    q.runTest(aq_sess);
}

 public static AQSession createSession() {
      Connection db_conn;
      AQSession  aq_sess = null;

      try {               
         DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

         /* Load the Oracle8i AQ driver: */
         Class.forName("oracle.AQ.AQOracleDriver");

         db_conn = DriverManager.getConnection("jdbc:oracle:thin:@10.10.10.10:1521:demo", "demo_app", "demo");

         System.out.println("JDBC Connection opened "); 
         db_conn.setAutoCommit(false);


         /* Creating an AQ Session: */
         aq_sess = AQDriverManager.createAQSession(db_conn);
         System.out.println("Successfully created AQSession ");  
      }
      catch (Exception ex) {
         System.out.println("Exception: " + ex); 
         ex.printStackTrace();      
      }  
      return aq_sess;
   }

  public void runTest(AQSession aq_sess)  {
       //AQQueueTable             q_table;
       AQQueue                  queue;
       AQMessage                message;
       AQRawPayload             raw_payload;
       AQDequeueOption          deq_option;
       byte[]                   b_array;
       Connection               db_conn;

     try {
       db_conn = ((AQOracleSession)aq_sess).getDBConnection();

   /* Get a handle to a queue - aq_queue4 in aquser schema: */
       queue = aq_sess.getQueue ("myadmin", "STREAM_QUEUE_DEMO");
       System.out.println("Successful getQueue");  

   /* Creating a AQDequeueOption object with default options: */
       deq_option = new AQDequeueOption();

       deq_option.setDequeueMode(AQDequeueOption.DEQUEUE_REMOVE);

       /* Set wait time to 10 seconds: */
       deq_option.setWaitTime(10);

   /* Dequeue a message: */
       message = queue.dequeue(deq_option);
       System.out.println("Successful dequeue"); 

   /* Retrieve raw data from the message: */
       raw_payload = message.getRawPayload();
       b_array = raw_payload.getBytes();
       db_conn.commit();

       String value = new String(b_array);
       System.out.println("queue="+value);  
     } catch(Exception e) {
         e.printStackTrace();
     }
  } 

但我在下面的行收到错误:

But I'm getting error at below line:

    message = queue.dequeue(deq_option);

错误说明

oracle.AQ.AQException: JMS-174: Class must be specified for queues with object payloads
Use dequeue(deq_option, payload_fact) or dequeue(deq_option, sql_data_cl)

任何人都可以帮我修复此错误吗?我需要立即批量出列消息。

Can any one help me out to fix this error? I need to bulk dequeue the message at once.

谢谢!

推荐答案

<你到底在想什么?即你的有效载荷是多少?

创建Oracle Streams AQ时,必须指定将在Q中排队的有效负载类型。

对于对象有效负载类型,您必须添加在排队之前的AQSession中的类信息。例如我们将Oracles本机XMLType对象出列,因此我们必须在创建会话后立即添加以下代码。

What exactly are you en-queuing ? i.e. what is your payload ?
When you create a Oracle Streams AQ, you have to specify a payload type that will be enqueued in the Q.
And for object payload types, you have to add the class information in the AQSession before de-queuing. e.g. we dequeue Oracles native XMLType object, so we have to add the following piece of code right after the session is created.

Map map = session.getTypeMap();
map.put("SYS.XMLTYPE", Class.forName("oracle.xdb.XMLTypeFactory"));

您必须根据有效负载类型执行类似操作。

You'll have to do something similar, based on your payload type.

这篇关于使用Java for Oracle 11g队列出列队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆