TIBCO.Rendezvous简单的发消息的过程

发布时间 2023-11-23 16:05:15作者: IT苦行僧-QF

C#代码实现发消息的过程.

首先需要安装,添加引用,

using TIBCO.Rendezvous;

然后其实就是简单4个步骤 ,即可把讯息发出去;

开启环境 ->实例化NetTransport ->生成需要发送的 Message->transport.Send(msg); 最后关闭环境;

 1             //开启环境;
 2             TIBCO.Rendezvous.Environment.Open();
 3             // 实例化一个用来发送讯息的NetTransport ;
 4             NetTransport transport = new NetTransport(service, network, daemon);
 5             // 实例化消息;
 6             Message msg = new Message();
 7             //消息主题;
 8             msg.SendSubject = subject;
 9             //添加消息内容字段
10             msg.AddField(name1, value1);
11             msg.AddField(name2, value2);
12             // 发送出去
13             transport.Send(msg);
14             //关闭环境
15             TIBCO.Rendezvous.Environment.Close();

下面给出官网的DEMO 文档代码 

  1 /// Copyright (c) 1998-$Date: 2013-12-20 07:48:17 -0800 (Fri, 20 Dec 2013) $ TIBCO Software Inc.
  2 /// All rights reserved.
  3 /// TIB/Rendezvous is protected under US Patent No. 5,187,787.
  4 /// For more information, please contact:
  5 /// TIBCO Software Inc., Palo Alto, California, USA
  6 using System;
  7 using System.Net;
  8 using TIBCO.Rendezvous;
  9  
 10 namespace TIBCO.Rendezvous.Examples
 11 {
 12     /// <summary>
 13     ///  RendezvousSender - sample Rendezvous message publisher.
 14     ///  This program publishes one or more string messages on a specified
 15     ///  subject.  Both the subject and the message(s) must be supplied as
 16     ///  command parameters.  Message(s) with embedded spaces should be quoted.
 17     ///  A field named "DATA" will be created to hold the string in each
 18     ///  message.
 19     ///  
 20     ///  Optionally the user may specify communication parameters for 
 21     ///  NetTransport constructor. If none are specified the following 
 22     ///  defaults are used:
 23     ///  
 24     ///  service     "rendezvous" or "7500/udp"
 25     ///  network     the result of gethostname
 26     ///  daemon      "tcp:7500"
 27     ///     
 28     ///  Normally a listener such as tibrvlisten should be started first.
 29     ///  
 30     ///  Examples:
 31     ///  
 32     ///  Publish two messages on subject a.b.c and default parameters:
 33     ///  RendezvousSender a.b.c "This is my first message" "This is my second message"
 34     ///  
 35     ///  Publish a message on subject a.b.c using port 7566:
 36     ///  RendezvousSender -service 7566 a.b.c message
 37     /// </summary>
 38     class SenderApplication
 39     {
 40         static string service = null;
 41         static string network = null;
 42         static string daemon = null;
 43         static int iterations = 1;
 44  
 45         const String FIELD_NAME = "DATA";
 46  
 47         /// <summary>
 48         /// The main entry point for the application.
 49         /// </summary>
 50         [MTAThread]
 51         static void Main(string[] arguments)
 52         {
 53             int argumentsCount = InitializeParameters(arguments);
 54  
 55             if (argumentsCount > (arguments.Length - 2))
 56             {
 57                 Usage();
 58             }
 59  
 60             try
 61             {
 62                 /* Create internal TIB/Rendezvous machinery */
 63                 if (TIBCO.Rendezvous.Environment.IsIPM())
 64                 {
 65                     /*
 66                      * Prior to using the Rendezvous IPM library please read the appropriate
 67                      * sections of the user guide to determine if IPM is the correct choice
 68                      * for your application; it is likely not.
 69                      *
 70                      * To use the shared Rendezvous IPM library in .NET on Windows,
 71                      * first make sure it is located in your system path before the standard
 72                      * Rendezvous library.
 73                      *
 74                      * The IPM shared library can be found in %TIBRV_HOME%\bin\ipm.
 75                      *
 76                      * The IPM static library can be found in %TIBRV_HOME%\lib\ipm.
 77                      *
 78                      * To configure IPM you can do one of the following:
 79                      *
 80                      * 1) Nothing, and accept the default IPM RV parameter values.
 81                      *
 82                      * 2) Place a file named "tibrvipm.cfg" in your PATH, and have
 83                      * IPM automatically read in configuration values.
 84                      *
 85                      * 3) Call Environment.SetRVParameters, prior to Environment.Open:
 86                      *
 87                      *   string[] parameters =
 88                      *    new string[] {"-reliability", "3", "-reuse-port", "30000"};
 89                      *   Environment.SetRVParameters(parameters);
 90                      *   Environment.Open();
 91                      *
 92                      * 4) Call Environment.Open(string pathname), and have IPM read
 93                      * in the configuration values:
 94                      *
 95                      *   Environment.Open(".\\tibrvipm.cfg");
 96                      *
 97                      * An example configuration file, "tibrvipm.cfg", can be found in the
 98                      * "%TIBRV_HOME%\examples\IPM" directory of the Rendezvous installation.
 99                      *
100                      */
101                     TIBCO.Rendezvous.Environment.Open(".\\tibrvipm.cfg");
102                 }
103                 else
104                 {
105                     TIBCO.Rendezvous.Environment.Open();
106                 }
107             }
108             catch(RendezvousException exception)
109             {
110                 Console.Error.WriteLine("Failed to open Rendezvous Environment: {0}", exception.Message);
111                 Console.Error.WriteLine(exception.StackTrace);
112                 System.Environment.Exit(1);
113             }
114  
115             // Create Network transport
116             Transport transport = null;
117             try
118             {
119                 transport = new NetTransport(service, network, daemon);
120             }
121             catch (RendezvousException exception)
122             {
123                 Console.Error.WriteLine("Failed to create NetTransport:");
124                 Console.Error.WriteLine(exception.StackTrace);
125                 System.Environment.Exit(1);
126             }
127             
128             // Create the message
129             Message message = new Message();
130  
131             // Set send subject into the message
132             try
133             {
134                 message.SendSubject = arguments[argumentsCount++];
135             }
136             catch (RendezvousException exception) 
137             {
138                 Console.Error.WriteLine("Failed to set send subject:");
139                 Console.Error.WriteLine(exception.StackTrace);
140                 System.Environment.Exit(1);
141             }
142  
143             try
144             {
145                 // Send one message for each parameter
146                 while (argumentsCount < arguments.Length)
147                 {
148                     Console.Out.WriteLine("Publishing: subject={0} \"{1}\"",
149                         message.SendSubject,
150                         arguments[argumentsCount]);
151                     message.AddField(FIELD_NAME, arguments[argumentsCount], 0);
152                     for (int i = 0; i < SenderApplication.iterations; i++)
153                     {
154                         transport.Send(message);
155                     }
156                     argumentsCount++;
157                 }
158             }
159             catch (RendezvousException exception)
160             {
161                 Console.Error.WriteLine("Error sending a message:");
162                 Console.Error.WriteLine(exception.StackTrace);
163                 System.Environment.Exit(1);
164             }
165  
166             // Close Environment, it will cleanup all underlying memory, destroy
167             // transport and guarantee delivery.
168             try
169             {
170                 TIBCO.Rendezvous.Environment.Close();
171             }
172             catch(RendezvousException exception)
173             {                
174                 Console.Error.WriteLine("Exception dispatching default queue:");
175                 Console.Error.WriteLine(exception.StackTrace);
176                 System.Environment.Exit(1);
177             }
178         }
179         
180         static void Usage()
181         {
182             Console.Out.Write("Usage: RendezvousSender [-service service] [-network network]");
183             Console.Out.Write("                        [-daemon daemon] <subject> <messages>");
184             System.Environment.Exit(1);
185         }
186  
187         static int InitializeParameters(string[] arguments)
188         {
189             int i = 0;
190             while(i < arguments.Length - 1 && arguments[i].StartsWith("-"))
191             {
192                 if (arguments[i].Equals("-service"))
193                 {
194                     service = arguments[i+1];
195                     i += 2;
196                 }
197                 else
198                     if (arguments[i].Equals("-network"))
199                 {
200                     network = arguments[i+1];
201                     i += 2;
202                 }
203                 else
204                     if (arguments[i].Equals("-daemon"))
205                 {
206                     daemon = arguments[i+1];
207                     i += 2;
208                 }
209                 else
210                     if (arguments[i].Equals("-iterations"))
211                 {
212                     iterations = Int32.Parse(arguments[i+1]);
213                     i += 2;
214                 }
215                 else
216                     Usage();
217             }
218             return i;
219         }
220     }    
221 }