繁体   English   中英

使用Java for Oracle 11g队列出队

[英]Dequeue using Java for Oracle 11g queue

我正在尝试使用独立Java使用Oracle 11g队列出队。 这是代码:

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();
     }
  } 

但是我在下面的行出现错误:

    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)

有人可以帮我解决此错误吗? 我需要立即将邮件批量出队。

谢谢!

您到底要排队什么? 即您的有效载荷是多少?
创建Oracle Streams AQ时,必须指定将在Q中排队的有效负载类型。
对于对象有效负载类型,必须在出队之前在AQSession中添加类信息。 例如,我们使Oracle的本机XMLType对象出队,因此我们必须在创建会话后立即添加以下代码。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM