Creating a .NET JMS sample client

.NET clients can use the Java Messaging System (JMS) to create, send, and receive messages. You can also use JMS from Visual Basic .NET clients. The clients can use either a point-to-point or publish/subscribe messaging model, as illustrated in the following sample client. The point-to-point messaging model delivers messages to specific queues. The publish/subscribe messaging model defines a topic and publishes messages that can be read by all subscribers to the topic.

This sample client acts as both the producer and consumer of JMS messages. In real applications, .NET clients can also send messages to, and receive messages from, Java applications. You can compile this client application by running csc.exe:

using System;
using System.Threading;
using System.Net;
using com.sybase.jms.net;

public class JmsTest
{
  // To get this test to pass, first run:
  //
  // set-password testuser testpass1

  public static void Main()
  {
    int retry = 3;

Create a JmsProvider instance, and set its URL, user name, and password properties:

  JmsProvider jmsProvider = JmsProvider.GetInstance();
  jmsProvider.SetProviderURL("iiop://" +
     Dns.GetHostName() + ":2000");
  jmsProvider.SetUsername("testuser");
  jmsProvider.SetPassword("testpass1");

Create a JmsConnection object to act as the gateway between EAServer and the .NET client:

  JmsConnection jmsConn = jmsProvider.GetConnection();

Define the local-storage location for messages, which allows .NET clients to temporarily store messages locally, then send the messages when the clients connect to EAServer. This example uses the Windows registry for local storage. WorkOffline directs the client to work locally.

  jmsConn.SetLocalStore(LocalRegistry.GetStore());
  jmsConn.WorkOffline();
  Console.WriteLine("*** working offline ***");

Create a text message:

  Message msg1 =      MessageUtil.GetTextMessage("QueueQueue");

Send delivers the message to MyQueue1. Since the client is working in offline mode, the message is stored locally:

  string q1 = "MyQueue1";
  jmsConn.Send(q1, msg1);
  Console.WriteLine("<== sent message: " +      msg1.body.text());

  Thread.Sleep(3000);

Tell the client to work in online mode. Messages that are stored locally are sent to the remote server. The client can also receive messages from the remote server.

jmsConn.WorkOnline();
Console.WriteLine("*** working online ***");
Thread.Sleep(3000);

Calling Receive(q1, 1000) receives messages from MyQueue1 with a timeout value of one second. After the client receives a message, it calls Acknowledge to tell EAServer that the message was received and it is safe to delete it.

  for (int i = 0; i < retry; i++)
  {
     Message msg = jmsConn.Receive(q1, 1000);

     if (msg != null)
     {
        Console.WriteLine("==> received message :" +
          msg.body.text());
        jmsConn.Acknowledge(msg);
     }
  }

To publish a text message, define the topic, and create the message:

  String topic = "MyTopic";
  Message msg2 =   MessageUtil.GetTextMessage("TopicTopic");

Set the client ID, which identifies the client application:

  jmsConn.SetClientID("ccID");
  string queue = jmsConn.GetClientID();

Call Subscribe to register a subscription to the topic. This example uses the client ID as the subscribing queue ID:

jmsConn.Subscribe(queue, topic);

Working in offline mode, publish the message (msg2), which is stored locally. When the client changes to online mode, any queue that is subscribed to the topic receives the message:

  jmsConn.WorkOffline();
  Console.WriteLine("*** working offline");

  jmsConn.Publish(topic, msg2);
  Console.WriteLine("<== published message: " +      msg2.body.text());

  Thread.Sleep(3000);

Change to work online. Receive and acknowledge messages with the topic MyTopic:

  jmsConn.WorkOnline();
  Console.WriteLine("*** working online");
  Thread.Sleep(3000);

  for (int i = 0; i < retry; i++)
  {
     Message msg = jmsConn.Receive(queue, 1000);

     if (msg != null)
     {
        Console.WriteLine("==> received message from
           topic: " + msg.body.text());
        jmsConn.Acknowledge(msg);
     }
  }

Call Unsubscribe to tell EAServer that this queue is no longer interested in receiving messages with the specified topic.

   msConn.Unsubscribe(queue);
 }
}