Wednesday, January 5, 2011

Topic usage

As I wrote in ActiveMQ preparation part 1 "In the future I will try to make entire code of demonstator application available to download." so now I going to show you usage of the class AmqTopicClient.


1. Connect to the broker and declare a topic.


In the constructor, the first parameter of type string is the address of the broker and the second is the name of the topic.
Then add the event handlers for incoming messages and occuring errors. Finally connect to the broker.

  1. private AmqTopicClient amqTopicClient;
  2.  
  3. amqTopicClient = new AmqTopicClient(AMQuRL.Text, AmqTopic.Text);
  4. amqTopicClient.NewDataReceive += AmqTopicClient_NewDataReceive;
  5. amqTopicClient.AmqClientStatusError += AmqTopicClient_AmqClientStatusError;
  6. amqTopicClient.AmqConnect();
* This source code was highlighted with Source Code Highlighter.

2. Send simple message string or object type.


To send messages are two ways. Read carefully!
1. Asynchronous send a command to send a message.
2. Synchronous send a command to send a message.

Sure it seems confusing. But if anyone reviewed the code and read what I wrote now it follows that the application would asynchronously send messages that are sent asynchronously.
 Usage is very simple:
1. Asynchronous
-string

  1. amqTopicClient.TxtSend(textBox1.Text + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
* This source code was highlighted with Source Code Highlighter.
-object 

  1. amqTopicClient.Send((textBox1.Text + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"))as object);
* This source code was highlighted with Source Code Highlighter.
2. Synchronous is similar (just add "My" before method name):
-string

  1. amqTopicClient.MyTxtSend(textBox1.Text + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
* This source code was highlighted with Source Code Highlighter.
-object

  1. amqTopicClient.MySend((textBox1.Text + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"))as object);
* This source code was highlighted with Source Code Highlighter.

No comments:

Post a Comment