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

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

 

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

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

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

    using iAnywhere.QAnywhere.Client;
  2. 创建 QATransactionalManager 对象。

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

    QAManager mgr;
    mgr = 
      QAManagerFactory.Instance.CreateQATransactionalManager( 
      null );

    请参见QAManagerFactory 类

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

    mgr = 
      QAManagerFactory.Instance.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 方法的调用开始。

另请参见