本地消息存储库要求安装 QAnywhere。QAnywhere 位于 SQL Anywhere 安装程序中的 "同步和消息传递" 功能下面。要在 SQL Anywhere 数据库中启用消息传递,必须将 QAnywhere 模式安装在 SQL Anywhere 数据库中,使其能够作为本地消息存储库使用。这是通过使用 SQL Anywhere 的 QAnywhere 代理的 -sil 选项来完成的。-sil 选项指示代理将数据库初始化为本地消息存储库。该代理只是用于初始化本地消息存储库,将来不再需要。在数据库中创建的所有 QAnywhere 对象都属于 ml_qa_message_group 所有者。一旦将 SQL Anywhere 数据库初始化为本地消息存储库,应用程序就可以使用 QAnywhere 客户端 API 来交换消息。
创建 SQL Anywhere 数据库。如果打算使用现成的 SQL Anywhere 数据库,此步骤可以省略。
dbinit localmsgstore.db |
在 SQL Anywhere 数据库中安装 QAnywhere 模式,使它能够被用作本地消息总线:
qaagent -sil -c "dbf=localmsgstore.db;uid=dba;pwd=sql" |
创建发送器应用程序以将消息放入消息存储库。有关使用使用 .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 ); } } } } |
使用以下命令行编译该程序。您的机器上必须安装有 Visual Studio。
csc /reference:"%SQLANY12%\Assembly\v2\iAnywhere.QAnywhere.Client.dll" sender.cs |
创建接收器应用程序以从消息存储库检索消息。有关使用使用 .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 ); } } } } |
使用以下命令行编译该程序。您的机器上必须安装有 Visual Studio。
csc /reference:"%SQLANY12%\Assembly\v2\iAnywhere.QAnywhere.Client.dll" receiver.cs |
启动 SQL Anywhere 数据库:
dbsrv12 localmsgstore.db |
运行发送器应用程序:
sender |
运行接收器应用程序:
receiver |
显示字符串 "Sample Text"。
创建 SQL Anywhere 数据库。如果打算使用现成的 SQL Anywhere 数据库,此步骤可以省略。
dbinit localmsgstore.db |
在 SQL Anywhere 数据库中安装 QAnywhere 模式,使它能够被用作消息总线。
qaagent -sil -c "dbf=localmsgstore.db;uid=dba;pwd=sql" |
创建发送器应用程序以将消息放入消息存储库。有关使用使用 .NET 版本 QAnywhere 客户端 API 的信息,请参见用于客户端的 QAnywhere Java API 参考。
import java.util.*; import ianywhere.qanywhere.client.*; public class sender { public static void main( Strings [] 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() ); } } } |
使用以下命令行编译该程序。您的机器上必须安装有 Java JDK。
javac -cp "%SQLANY12%\java\qaclient.jar" sender.java |
创建接收器应用程序以从消息存储库检索消息。有关使用使用 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() ); } } } |
使用以下命令行编译该程序。您的机器上必须安装有 Java JDK。
javac -cp "%SQLANY12%\java\qaclient.jar" receiver.java |
启动 SQL Anywhere 数据库:
dbsrv12 localmsgstore.db |
运行发送器应用程序:
java -cp ".\;%SQLANY12%\java\qaclient.jar;%SQLANY12%\java\jodbc.jar" sender |
运行接收器应用程序:
java -cp ".\;%SQLANY12%\java\qaclient.jar;%SQLANY12%\java\jodbc.jar" receiver |
显示字符串 "Sample Text"。
清理消息存储库 在缺省情况下,到达最终状态的消息将会留在消息存储库中。使用一段时间后,消息累计导致数据库增大。将数据库初始化为消息总线时,会创建名为 ml_qa_clearreceivedmessages 的存储过程。这一存储过程将会将已到达最终状态的消息从消息存储库中删除。一种管理数据库消息增长的便捷方法是创建一个数据库事件,一旦该事件被触发就调用 ml_qa_clearreceivedmesssages 存储过程。例如,以下数据库事件会在每天午夜时删除到达最终状态的消息:
CREATE EVENT message_cleanup SCHEDULE START TIME '12:00AM' EVERY 24 HOURS HANDLER begin call ml_qa_message_group.ml_qa_clearreceivedmessages() end |
查看消息 通过 QAnywhere 插件来连接客户端存储库后,就可使用 Sybase Central 来查看 SQL Anywhere 数据库中的消息。
![]() |
使用DocCommentXchange 讨论此页。
|
版权 © 2010, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.0 |