Category Archives: RV

Minimal steps to create a TIBCO RV message and send it

The previous post was on creating a minimal TIBCO RV listener. So naturally it’s time to look at creating a minimal TIBCO RV “sender”. In other words, creating a message and sending it.

Note: The initial code was take from the C# TIB RV samples, but I wanted to distil these to a quick and simple step by step guide.

For this sample we’ll create a Console application in C# and we’ll assume you are getting the server, network etc. values from some key/value mechanism, whether this is a command line parser or config is down to your specific implementation.

So create a Windows Console project then follow the steps below (the full code will be listed at the end of this rambling)

In case you’ve come to this post without first reading the post on creating a minimal listener, I will repeat a fair amount of code/steps from that post here.

  1. Add references to TIBCO.Rendezvous and ensure that TIBCO.Rendezvous.netmodule resides in the same folder as TIBCO.Rendezvous (just in case you’re not referencing the TIB RV installation itself
  2. For this simple example we’ll hard code a few variables
    string server = arguments["server"];
    string network = arguments["network"];
    string daemon = arguments["daemon"];
    
  3. Next we need to open the Rendezvous environment using

    TIBCO.Rendezvous.Environment.Open();
    

    The above code should be enclosed in a try..catch block, either enclosing all of the following code or if you want the granularity, then each line in essence. For now we’ll just assume it’s all in a try..catch block

  4. We now need to create the transport

    Transport transport = new NetTransport(service, network, daemon);
    
  5. Now it’s time to create the message. The subject is set to “myapp.mysubject” and we add some data to a new field we’ve created named “Data”

    Message message = new Message { SendSubject = "myapp.mysubject" };
    message.AddField("Data", "Some Data");
    
  6. To send the message is simply a case of

    transport.Send(message);
    
  7. Finally when we’ve finished with RV we need to close the environment with

    TIBCO.Rendezvous.Environment.Close();
    

Before we look at the full code, here’s what the listener shows us if we listen to “myapp.>”

2013-11-21 15:58:49 (2013-11-21 15:58:49.281000000Z): subject=myapp.mysubject, message={Data=”Some Data”}

And as promised, the full code is

public void Process(CommandLineArguments arguments)
{
   string service = arguments["server"];
   string network = arguments["network"];
   string daemon = arguments["daemon";

   try
   {
      TIBCO.Rendezvous.Environment.Open();

      Transport transport = new NetTransport(service, network, daemon);

      Message message = new Message { SendSubject = "myapp.mysubject" };
      message.AddField("Data", "Some Data");
      transport.Send(message);
   }
   catch (Exception e)
   {
      Console.WriteLine(e.Message + "\n\n" + e.StackTrace);
   }
   finally
   {
      TIBCO.Rendezvous.Environment.Close();
   }
}

Minimal steps to create a TIBCO RV listener

Following is a list of the steps (and code) for creating a minimal TIB RV listener which will simply output messages to the console (therefore implementing the same sort of functionality as the tibrvlisten application.

Note: The initial code was take from the C# TIB RV samples, but I wanted to distil these to a quick and simple step by step guide.

For this sample we’ll create a Console application in C# and we’ll assume you are getting the server, network etc. values from some key/value mechanism, whether this is a command line parser or config is down to your specific implementation.

So create a Windows Console project then follow the steps below (the full code will be listed at the end of this rambling)

  1. Add references to TIBCO.Rendezvous and ensure that TIBCO.Rendezvous.netmodule resides in the same folder as TIBCO.Rendezvous (just in case you’re not referencing the TIB RV installation itself
  2. For this simple example we’ll hard code a few variables
    string server = arguments["server"];
    string network = arguments["network"];
    string daemon = arguments["daemon"];
    string topics[] = arguments["topics"].Split(',');
    
  3. Next we need to open the Rendezvous environment using

    TIBCO.Rendezvous.Environment.Open();
    

    The above code should be enclosed in a try..catch block, either enclosing all of the following code or if you want the granularity, then each line in essence. For now we’ll just assume it’s all in a try..catch block

  4. We now need to create the transport

    Transport transport = new NetTransport(service, network, daemon);
    
  5. Now create the listeners and attach a MessageReceived handler to each listener.

    TIBCO.Rendezvous.Listener[] listeners = new TIBCO.Rendezvous.Listener[topics.Length];
    for (int i = 0; i < listeners.Length; i++)
    {
       listeners[i] = new TIBCO.Rendezvous.Listener(Queue.Default, 
                         transport, topics[i], null);
       listeners[i].MessageReceived += OnMessageReceived;
    }
    

    The code for the OnMessageReceived handler looks like

    static void OnMessageReceived(object listener, 
                  MessageReceivedEventArgs messageReceivedEventArgs)
    {
       Message message = messageReceivedEventArgs.Message;
    
       Console.Out.WriteLine("{0}: subject={1}, reply={2}, message={3}",
    	DateTime.Now, message.SendSubject, message.ReplySubject, message);
    
       Console.Out.Flush();
    }
    
  6. Now for this example we’re going to go into a look and just keep calling the Queue.Default.Dispatch method to keep messages dispatching or block whilst no messages exist

    while (true)
    {
       try
       {
          Queue.Default.Dispatch();
       }
       catch (RendezvousException exception)
       {
          Console.Error.WriteLine("Exception dispatching default queue:");
          Console.Error.WriteLine(exception.StackTrace);
          break;
       }
    }
    
  7. Finally when we’ve finished with RV we need to close the environment with

    TIBCO.Rendezvous.Environment.Close();
    

Full code

public void Run(CommandLineArguments arguments)
{
   string service = arguments["server"];
   string network = arguments["network"];
   string daemon = arguments["daemon"];

   string[] topics = arguments["topics"].Split(',');
			
   try
   {
      TIBCO.Rendezvous.Environment.Open();

      Transport transport = new NetTransport(service, network, daemon);

      TIBCO.Rendezvous.Listener[] listeners = new TIBCO.Rendezvous.Listener[topics.Length];
      for (int i = 0; i < listeners.Length; i++)
      {
         listeners[i] = new TIBCO.Rendezvous.Listener(Queue.Default, 
                               transport, topics[i], null);
         listeners[i].MessageReceived += OnMessageReceived;
      }

      while (true)
      {
         try
         {
            Queue.Default.Dispatch();
         }
         catch (RendezvousException exception)
         {
             Console.Error.WriteLine("Exception dispatching default queue:");
             Console.Error.WriteLine(exception.StackTrace);
             break;
         }
      }
   }
   catch (Exception e)
   {
      Console.WriteLine(e.Message + "\n\n" + e.StackTrace);
   }
   finally
   {
      TIBCO.Rendezvous.Environment.Close();
   }
}

static void OnMessageReceived(object listener, 
         MessageReceivedEventArgs messageReceivedEventArgs)
{
   Message message = messageReceivedEventArgs.Message;

   Console.Out.WriteLine("{0}: subject={1}, reply={2}, message={3}",
		DateTime.Now, message.SendSubject, message.ReplySubject, message);

   Console.Out.Flush();
}