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

SQL Anywhere 12.0.1 » QAnywhere » 如何编写 QAnywhere 客户端应用程序 » 发送 QAnywhere 消息 » 事务性消息传递实现

 

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

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

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

    #include <qa.hpp>
    QAManagerFactory *   factory;
    
    factory = QAnywhereFactory_init();
    if( factory == NULL ) {
        // Fatal error.
    }
  2. 创建事务性管理器。

    QATransactionalManager *  mgr;
    mgr = factory->createQATransactionalManager( NULL );
    if( mgr == NULL ) {
        // Fatal error.
    }

    与非事务管理器相同,可以指定属性文件来自定义 QAnywhere 行为。在本例中,没有使用任何属性文件。

  3. 初始化管理器。

    if( !mgr->open() ) {
       // Display message using mgr->getLastErrorMsg().
    }

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

 ♦ 在一个事务中发送多条消息
  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();
          }
        }
        mgr->deleteMessage( msg_2 );
      }
      mgr->deleteMessage( msg_1 );
    }

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

    注意

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

 另请参见