【问题标题】:Dequeue using Java for Oracle 11g queue使用 Java for Oracle 11g 队列出列
【发布时间】:2011-12-23 04:04:58
【问题描述】:

我正在尝试使用 Oracle 11g 队列使用独立 java 进行出列。代码如下:

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)

谁能帮我解决这个错误?我需要立即将消息批量出列。

谢谢!

【问题讨论】:

    标签: java oracle jakarta-ee oracle11g


    【解决方案1】:

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

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

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

    【讨论】:

    • 嗨..你能帮忙吗?有效载荷类型是 sys.aq$_jms_text_message
    • 可以分享一下Q创建脚本和入队代码吗?
    猜你喜欢
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多