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。QAnywhere 位于 SQL Anywhere 安装程序中的 "同步和消息传递" 功能下面。要在 SQL Anywhere 数据库中启用消息传递,必须将 QAnywhere 模式安装在 SQL Anywhere 数据库中,使其能够作为本地消息存储库使用。这是通过使用 SQL Anywhere 的 QAnywhere 代理的 -sil 选项来完成的。-sil 选项指示代理将数据库初始化为本地消息存储库。该代理只是用于初始化本地消息存储库,将来不再需要。在数据库中创建的所有 QAnywhere 对象都属于 ml_qa_message_group 所有者。一旦将 SQL Anywhere 数据库初始化为本地消息存储库,应用程序就可以使用 QAnywhere 客户端 API 来交换消息。

 ♦ 创建本地消息存储库(.NET 示例)
  1. 创建 SQL Anywhere 数据库。如果打算使用现成的 SQL Anywhere 数据库,此步骤可以省略。

    dbinit localmsgstore.db
  2. 在 SQL Anywhere 数据库中安装 QAnywhere 模式,使它能够被用作本地消息总线:

    qaagent -sil -c "dbf=localmsgstore.db;uid=dba;pwd=sql"
  3. 创建发送器应用程序以将消息放入消息存储库。 有关使用 .NET 版本 QAnywhere 客户端 API 的信息,请参见用于客户端的 QAnywhere .NET API 参考



    using System;
    using System.IO;
    using iAnywhere.QAnywhere.Client;
    namespace sender
    {
    class sender
        {
            public static void Main() {
                try {
                    // QAnywhere initialization
                    QAManager mgr = QAManagerFactory.Instance.CreateQAManager();
                    // Be sure to set the DATABASE_TYPE property
                    mgr.SetProperty( "DATABASE_TYPE", "sqlanywhere" );
                    mgr.SetProperty( "CONNECT_PARAMS", "dbf=localmsgstore.db;uid=dba;pwd=sql" );
                    mgr.Open( AcknowledgementMode.IMPLICIT_ACKNOWLEDGEMENT );
                    mgr.Start();
                    // Create a text message
                    QATextMessage msg = mgr.CreateTextMessage();
                    msg.Text = "Sample text";
                    // Queue the message
                    mgr.PutMessage( "dbqueue", msg );
                    // QAnywhere finalization
                    mgr.Stop();
                    mgr.Close();
                    } catch( Exception exc ) {
                    Console.WriteLine( exc.Message );
                }
            }
        }
    }
  4. 使用以下命令行编译该程序。您的计算机上必须安装 Visual Studio。

    csc /reference:"%SQLANY12%\Assembly\v2\iAnywhere.QAnywhere.Client.dll" sender.cs
  5. 创建接收器应用程序以从消息存储库检索消息。 有关使用 .NET 版本 QAnywhere 客户端 API 的信息,请参见用于客户端的 QAnywhere .NET API 参考



    using System;
    using System.IO;
    using iAnywhere.QAnywhere.Client;
    namespace receiver
    {
    class receiver
    {
    public static void Main() {
    try {
    // QAnywhere initialization
    QAManager mgr = QAManagerFactory.Instance.CreateQAManager();
    // Be sure to set the DATABASE_TYPE property
    mgr.SetProperty( "DATABASE_TYPE", "sqlanywhere" );
    mgr.SetProperty( "CONNECT_PARAMS", "dbf=localmsgstore.db;uid=dba;pwd=sql" );
    mgr.Open( AcknowledgementMode.IMPLICIT_ACKNOWLEDGEMENT );
    mgr.Start();
    // Get the message
    QATextMessage msg = (QATextMessage)mgr.GetMessage( "dbqueue" );
    // Display the text
    Console.WriteLine( msg.Text );
    // QAnywhere finalization
    mgr.Stop();
    mgr.Close();
    } catch( Exception exc ) {
    Console.WriteLine( exc.Message );
    }
    }
    }
    }
  6. 使用以下命令行编译该程序。您的计算机上必须安装 Visual Studio。

    csc /reference:"%SQLANY12%\Assembly\v2\iAnywhere.QAnywhere.Client.dll" receiver.cs
  7. 启动 SQL Anywhere 数据库:

    dbsrv12 localmsgstore.db
  8. 运行发送器应用程序:

    sender
  9. 运行接收器应用程序:

    receiver

    显示字符串 "Sample Text"

 ♦ 创建本地消息存储库(Java 示例)
  1. 创建 SQL Anywhere 数据库。如果打算使用现成的 SQL Anywhere 数据库,此步骤可以省略。

    dbinit localmsgstore.db
  2. 在 SQL Anywhere 数据库中安装 QAnywhere 模式,使它能够被用作消息总线。

    qaagent -sil -c "dbf=localmsgstore.db;uid=dba;pwd=sql"
  3. 创建发送器应用程序以将消息放入消息存储库。 有关使用 .NET 版本 QAnywhere 客户端 API 的信息,请参见用于客户端的 QAnywhere Java API 参考



    import java.util.*;
    import ianywhere.qanywhere.client.*;
    public class sender
    {
    public static void main( String [] args ) {
    try {
    // QAnywhere initialization
    QAManager mgr = QAManagerFactory.getInstance().createQAManager();
    // Be sure to specify the DATABASE_TYPE
    mgr.setProperty( "DATABASE_TYPE", "sqlanywhere" );
    mgr.setProperty( "CONNECT_PARAMS", "dbf=localmsgstore.db;uid=dba;pwd=sql" );
    mgr.open( AcknowledgementMode.IMPLICIT_ACKNOWLEDGEMENT );
    mgr.start();
    // Create a text message
    QATextMessage msg = mgr.createTextMessage();
    msg.setText( "Sample text" );
    // Queue the message
    mgr.putMessage( "dbqueue", msg );
    // QAnywhere finalization
    mgr.stop();
    mgr.close();
    } catch( Exception exc ) {
    System.out.println( exc.getMessage() );
    }
    }
    }
  4. 使用以下命令行编译该程序。您的计算机上必须安装 Java JDK。

    javac  -cp "%SQLANY12%\java\qaclient.jar" sender.java
  5. 创建接收器应用程序以从消息存储库检索消息。 有关使用 Java 版本 QAnywhere 客户端 API 的信息,请参见用于客户端的 QAnywhere Java API 参考



    import java.util.*;
    import ianywhere.qanywhere.client.*;
    public class receiver
    {
    public static void main( Strings [] args ) {
    try {
    // QAnywhere initialization
    QAManager mgr = QAManagerFactory.getInstance().createQAManager();
    // Be sure to set the DATABASE_TYPE property.
    mgr.setProperty( "DATABASE_TYPE", "sqlanywhere" );
    mgr.setProperty( "CONNECT_PARAMS", "dbf=localmsgstore.db;uid=dba;pwd=sql" );
    mgr.open( AcknowledgementMode.IMPLICIT_ACKNOWLEDGEMENT );
    mgr.start();
    // Get the message
    QATextMessage msg = (QATextMessage)mgr.getMessage( "dbqueue" );
    // Display the text
    System.out.println( msg.getText() );
    // QAnywhere finalization
    mgr.stop();
    mgr.close();
    } catch( Exception exc ) {
    System.out.println( exc.getMessage() );
    }
    }
    }
  6. 使用以下命令行编译该程序。您的计算机上必须安装 Java JDK。

    javac -cp "%SQLANY12%\java\qaclient.jar" receiver.java
  7. 启动 SQL Anywhere 数据库:

    dbsrv12 localmsgstore.db
  8. 运行发送器应用程序:

    java -cp ".\;%SQLANY12%\java\qaclient.jar;%SQLANY12%\java\jodbc.jar" sender
  9. 运行接收器应用程序:

    java -cp ".\;%SQLANY12%\java\qaclient.jar;%SQLANY12%\java\jodbc.jar" receiver

    显示字符串 "Sample Text"

 清理消息存储库
 查看消息