{"id":776,"date":"2013-11-21T14:08:33","date_gmt":"2013-11-21T14:08:33","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=776"},"modified":"2013-11-21T16:12:02","modified_gmt":"2013-11-21T16:12:02","slug":"minimal-steps-to-create-a-tibco-rv-listener","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/minimal-steps-to-create-a-tibco-rv-listener\/","title":{"rendered":"Minimal steps to create a TIBCO RV listener"},"content":{"rendered":"<p>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.<\/p>\n<p><em>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.<\/em><\/p>\n<p>For this sample we&#8217;ll create a Console application in C# and we&#8217;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.<\/p>\n<p>So create a Windows Console project then follow the steps below (the full code will be listed at the end of this rambling)<\/p>\n<ol>\n<li>Add references to TIBCO.Rendezvous and ensure that TIBCO.Rendezvous.netmodule resides in the same folder as TIBCO.Rendezvous (just in case you&#8217;re not referencing the TIB RV installation itself<\/li>\n<li>For this simple example we&#8217;ll hard code a few variables\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstring server = arguments&#x5B;&quot;server&quot;];\r\nstring network = arguments&#x5B;&quot;network&quot;];\r\nstring daemon = arguments&#x5B;&quot;daemon&quot;];\r\nstring topics&#x5B;] = arguments&#x5B;&quot;topics&quot;].Split(',');\r\n<\/pre>\n<\/li>\n<li>\nNext we need to open the Rendezvous environment using<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nTIBCO.Rendezvous.Environment.Open();\r\n<\/pre>\n<p>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&#8217;ll just assume it&#8217;s all in a try..catch block\n<\/li>\n<li>\nWe now need to create the transport <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nTransport transport = new NetTransport(service, network, daemon);\r\n<\/pre>\n<\/li>\n<li>\nNow create the listeners and attach a MessageReceived handler to each listener.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nTIBCO.Rendezvous.Listener&#x5B;] listeners = new TIBCO.Rendezvous.Listener&#x5B;topics.Length];\r\nfor (int i = 0; i &lt; listeners.Length; i++)\r\n{\r\n   listeners&#x5B;i] = new TIBCO.Rendezvous.Listener(Queue.Default, \r\n                     transport, topics&#x5B;i], null);\r\n   listeners&#x5B;i].MessageReceived += OnMessageReceived;\r\n}\r\n<\/pre>\n<p>The code for the OnMessageReceived handler looks like<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstatic void OnMessageReceived(object listener, \r\n              MessageReceivedEventArgs messageReceivedEventArgs)\r\n{\r\n   Message message = messageReceivedEventArgs.Message;\r\n\r\n   Console.Out.WriteLine(&quot;{0}: subject={1}, reply={2}, message={3}&quot;,\r\n\tDateTime.Now, message.SendSubject, message.ReplySubject, message);\r\n\r\n   Console.Out.Flush();\r\n}\r\n<\/pre>\n<\/li>\n<li>\nNow for this example we&#8217;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<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nwhile (true)\r\n{\r\n   try\r\n   {\r\n      Queue.Default.Dispatch();\r\n   }\r\n   catch (RendezvousException exception)\r\n   {\r\n      Console.Error.WriteLine(&quot;Exception dispatching default queue:&quot;);\r\n      Console.Error.WriteLine(exception.StackTrace);\r\n      break;\r\n   }\r\n}\r\n<\/pre>\n<\/li>\n<li>\nFinally when we&#8217;ve finished with RV we need to close the environment with<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nTIBCO.Rendezvous.Environment.Close();\r\n<\/pre>\n<\/li>\n<\/ol>\n<p>Full code<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic void Run(CommandLineArguments arguments)\r\n{\r\n   string service = arguments&#x5B;&quot;server&quot;];\r\n   string network = arguments&#x5B;&quot;network&quot;];\r\n   string daemon = arguments&#x5B;&quot;daemon&quot;];\r\n\r\n   string&#x5B;] topics = arguments&#x5B;&quot;topics&quot;].Split(',');\r\n\t\t\t\r\n   try\r\n   {\r\n      TIBCO.Rendezvous.Environment.Open();\r\n\r\n      Transport transport = new NetTransport(service, network, daemon);\r\n\r\n      TIBCO.Rendezvous.Listener&#x5B;] listeners = new TIBCO.Rendezvous.Listener&#x5B;topics.Length];\r\n      for (int i = 0; i &lt; listeners.Length; i++)\r\n      {\r\n         listeners&#x5B;i] = new TIBCO.Rendezvous.Listener(Queue.Default, \r\n                               transport, topics&#x5B;i], null);\r\n         listeners&#x5B;i].MessageReceived += OnMessageReceived;\r\n      }\r\n\r\n      while (true)\r\n      {\r\n         try\r\n         {\r\n            Queue.Default.Dispatch();\r\n         }\r\n         catch (RendezvousException exception)\r\n         {\r\n             Console.Error.WriteLine(&quot;Exception dispatching default queue:&quot;);\r\n             Console.Error.WriteLine(exception.StackTrace);\r\n             break;\r\n         }\r\n      }\r\n   }\r\n   catch (Exception e)\r\n   {\r\n      Console.WriteLine(e.Message + &quot;\\n\\n&quot; + e.StackTrace);\r\n   }\r\n   finally\r\n   {\r\n      TIBCO.Rendezvous.Environment.Close();\r\n   }\r\n}\r\n\r\nstatic void OnMessageReceived(object listener, \r\n         MessageReceivedEventArgs messageReceivedEventArgs)\r\n{\r\n   Message message = messageReceivedEventArgs.Message;\r\n\r\n   Console.Out.WriteLine(&quot;{0}: subject={1}, reply={2}, message={3}&quot;,\r\n\t\tDateTime.Now, message.SendSubject, message.ReplySubject, message);\r\n\r\n   Console.Out.Flush();\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[56,55],"tags":[],"class_list":["post-776","post","type-post","status-publish","format-standard","hentry","category-rv","category-tibco"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/776","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=776"}],"version-history":[{"count":10,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/776\/revisions"}],"predecessor-version":[{"id":788,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/776\/revisions\/788"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}