Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 12.0.0 (中文) » QAnywhere » 编写 QAnywhere 客户端应用程序 » 发送 QAnywhere 消息 » 实现事务性消息传递

 

为 Java 客户端实现事务性消息传递

 ♦  创建事务管理器
  1. 初始化 QAnywhere。

    这一步骤与在非事务性消息传递中的步骤相同。

    import ianywhere.qanywhere.client;
    QAManagerFactory factory = new QAManagerFactory();

    请参见QAManagerFactory 类

  2. 创建 QATransactionalManager 对象。

    例如,要创建缺省的 QATransactionalManager 对象,可调用 createQATransactionalManager(以 null 作为其参数):

    QAManager mgr;
    mgr = factory.createQATransactionalManager( null );

    也可以创建使用属性文件自定义的 QATransactionalManager 对象。属性文件在 createQATransactionalManager 方法中指定:

    mgr = factory.createQATransactionalManager( "qa_mgr.props" );

    其中,qa_mgr.props 是远程设备上属性文件的名称。

  3. 初始化 QAManager 对象。

    mgr.open();

现在,即可发送消息。以下过程在一个事务中发送两条消息。

 ♦  在一个事务中发送多条消息
  1. 初始化消息对象。

    QATextMessage msg_1;
    QATextMessage msg_2;
  2. 发送消息。

    以下代码在一个事务中发送两条消息:



    msg_1 = mgr.createTextMessage();
    if( msg_1 != null ) {
      msg_2 = mgr.createTextMessage();
      if( msg_2 != null ) {
        if( !mgr.putMessage( "jms_1\\queue_name", msg_1 ) ) {
          // Display message using mgr.getLastErrorMsg().
        } else {
          if( !mgr.putMessage( "jms_1\\queue_name", msg_2 ) ) {
            // Display message using mgr.getLastErrorMsg().
          } else {
            mgr.commit();
          }
        }
      }
    }

    commit 方法可提交当前事务并开始一个新事务。此方法提交全部 putMessage() 方法和 getMessage() 方法调用。

    注意

    第一个事务以对 open 方法的调用开始。