The local message store requires that you install QAnywhere. QAnywhere is located under the Synchronization and Messaging feature in the SQL Anywhere install program. In order to enable messaging in your SQL Anywhere database, you must install the QAnywhere schema into your SQL Anywhere database so that it can be used as a local message store. This is accomplished using the -sil option of the QAnywhere Agent for SQL Anywhere. The -sil option instructs the Agent to initialize the database as a local message store. The agent is simply used to initialize a local message store and is not needed any further. All QAnywhere objects created in the database belong to the ml_qa_message_group owner. Once your SQL Anywhere database has been initialized as a local message store, applications can use the QAnywhere client API to exchange messages.
Create a SQL Anywhere database. You can omit this step if you are going to use an existing SQL Anywhere database.
dbinit localmsgstore.db |
Install the QAnywhere schema into the SQL Anywhere database so that it can be used as a local message bus:
qaagent -sil -c "dbf=localmsgstore.db;uid=dba;pwd=sql" |
Create a sender application to put messages in the message store. For information on using the .NET version of the QAnywhere client API, see QAnywhere .NET API reference for clients.
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 ); } } } } |
Compile the program using the following command line. You must have Visual Studio installed on your machine.
csc /reference:"%SQLANY12%\Assembly\v2\iAnywhere.QAnywhere.Client.dll" sender.cs |
Create a receiver application to retrieve messages from the message store. For information on using the .NET version of the QAnywhere client API, see QAnywhere .NET API reference for clients.
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 ); } } } } |
Compile the program using the following command line. You must have Visual Studio installed on your machine.
csc /reference:"%SQLANY12%\Assembly\v2\iAnywhere.QAnywhere.Client.dll" receiver.cs |
Start the SQL Anywhere database:
dbsrv12 localmsgstore.db |
Run the sender application:
sender |
Run the receiver application:
receiver |
The string "Sample Text" is displayed.
Create a SQL Anywhere database. You can omit this step if you are going to use an existing SQL Anywhere database.
dbinit localmsgstore.db |
Install the QAnywhere schema into the SQL Anywhere database so that it can be used as a message bus.
qaagent -sil -c "dbf=localmsgstore.db;uid=dba;pwd=sql" |
Create a sender application to put messages in the message store. For information on using the .NET version of the QAnywhere client API, see QAnywhere Java API reference for clients.
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() ); } } } |
Compile the program using the following command line. You must have the Java JDK installed on your machine.
javac -cp "%SQLANY12%\java\qaclient.jar" sender.java |
Create a receiver application to retrieve messages from the message store. For information on using the Java version of the QAnywhere client API, see QAnywhere Java API reference for clients.
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() ); } } } |
Compile the program using the following command line. You must have the Java JDK installed on your machine.
javac -cp "%SQLANY12%\java\qaclient.jar" receiver.java |
Start the SQL Anywhere database:
dbsrv12 localmsgstore.db |
Run the sender application:
java -cp ".\;%SQLANY12%\java\qaclient.jar;%SQLANY12%\java\jodbc.jar" sender |
Run the receiver application:
java -cp ".\;%SQLANY12%\java\qaclient.jar;%SQLANY12%\java\jodbc.jar" receiver |
The string "Sample Text" is displayed.
Cleaning the message store By default, messages that have reached a final state remain in the message store. After a period of use, messages accumulate causing your database to grow. When you initialized your database for use as a message bus, a stored procedure called ml_qa_clearreceivedmessages was created. This stored procedure will delete messages that have reached a final state from the message store. A convenient way to manage the growth of messages in your database is to create a database event that, when triggered, invokes the ml_qa_clearreceivedmesssages stored procedure. For example, the following database event causes messages in a final state to be deleted every day at midnight:
CREATE EVENT message_cleanup SCHEDULE START TIME '12:00AM' EVERY 24 HOURS HANDLER begin call ml_qa_message_group.ml_qa_clearreceivedmessages() end |
Viewing Messages Use Sybase Central to view messages in your SQL Anywhere database by connecting to a client store using the QAnywhere Plug-in.
Discuss this page in DocCommentXchange.
|
Copyright © 2010, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.0 |