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

 

有关TIBCO RV的介绍资料几乎是一搜一大堆,但是相关的C#代码基本上还是空白没有的.

对于新手来说,需要使用TIBCO RV通常是比较困难的 ,需要学习一大堆的相关资料.

这里简单写一下C#代码实现发消息的过程.

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

using TIBCO.Rendezvous;

 

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

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

 

            //开启环境;
            TIBCO.Rendezvous.Environment.Open();
            // 实例化一个用来发送讯息的NetTransport ;
            NetTransport transport = new NetTransport(service, network, daemon);
            // 实例化消息;
            Message msg = new Message();
            //消息主题;
            msg.SendSubject = subject;
            //添加消息内容字段
            msg.AddField(name1, value1);
            msg.AddField(name2, value2);
            // 发送出去
            transport.Send(msg);
            //关闭环境
            TIBCO.Rendezvous.Environment.Close();

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

相信这份文档会对初次接触TIBCO.Rendezvous 的开发者极大的帮助.

/// Copyright (c) 1998-$Date: 2013-12-20 07:48:17 -0800 (Fri, 20 Dec 2013) $ TIBCO Software Inc.
/// All rights reserved.
/// TIB/Rendezvous is protected under US Patent No. 5,187,787.
/// For more information, please contact:
/// TIBCO Software Inc., Palo Alto, California, USA
using System;
using System.Net;
using TIBCO.Rendezvous;

namespace TIBCO.Rendezvous.Examples
{
	/// <summary>
	///  RendezvousSender - sample Rendezvous message publisher.
	///  This program publishes one or more string messages on a specified
	///  subject.  Both the subject and the message(s) must be supplied as
	///  command parameters.  Message(s) with embedded spaces should be quoted.
	///  A field named "DATA" will be created to hold the string in each
	///  message.
	///  
	///  Optionally the user may specify communication parameters for 
	///  NetTransport constructor. If none are specified the following 
	///  defaults are used:
	///  
	///  service     "rendezvous" or "7500/udp"
	///  network     the result of gethostname
	///  daemon      "tcp:7500"
	///	 
	///  Normally a listener such as tibrvlisten should be started first.
	///  
	///  Examples:
	///  
	///  Publish two messages on subject a.b.c and default parameters:
	///  RendezvousSender a.b.c "This is my first message" "This is my second message"
	///  
	///  Publish a message on subject a.b.c using port 7566:
	///  RendezvousSender -service 7566 a.b.c message
	/// </summary>
	class SenderApplication
	{
		static string service = null;
		static string network = null;
		static string daemon = null;
		static int iterations = 1;

		const String FIELD_NAME = "DATA";

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[MTAThread]
		static void Main(string[] arguments)
		{
			int argumentsCount = InitializeParameters(arguments);

			if (argumentsCount > (arguments.Length - 2))
			{
				Usage();
			}

			try
			{
				/* Create internal TIB/Rendezvous machinery */
				if (TIBCO.Rendezvous.Environment.IsIPM())
				{
					/*
					 * Prior to using the Rendezvous IPM library please read the appropriate
					 * sections of the user guide to determine if IPM is the correct choice
					 * for your application; it is likely not.
					 *
					 * To use the shared Rendezvous IPM library in .NET on Windows,
					 * first make sure it is located in your system path before the standard
					 * Rendezvous library.
					 *
					 * The IPM shared library can be found in %TIBRV_HOME%\bin\ipm.
					 *
					 * The IPM static library can be found in %TIBRV_HOME%\lib\ipm.
					 *
					 * To configure IPM you can do one of the following:
					 *
					 * 1) Nothing, and accept the default IPM RV parameter values.
					 *
					 * 2) Place a file named "tibrvipm.cfg" in your PATH, and have
					 * IPM automatically read in configuration values.
					 *
					 * 3) Call Environment.SetRVParameters, prior to Environment.Open:
					 *
					 *   string[] parameters =
					 *	new string[] {"-reliability", "3", "-reuse-port", "30000"};
					 *   Environment.SetRVParameters(parameters);
					 *   Environment.Open();
					 *
					 * 4) Call Environment.Open(string pathname), and have IPM read
					 * in the configuration values:
					 *
					 *   Environment.Open(".\\tibrvipm.cfg");
					 *
					 * An example configuration file, "tibrvipm.cfg", can be found in the
					 * "%TIBRV_HOME%\examples\IPM" directory of the Rendezvous installation.
					 *
					 */
					TIBCO.Rendezvous.Environment.Open(".\\tibrvipm.cfg");
				}
				else
				{
					TIBCO.Rendezvous.Environment.Open();
				}
			}
			catch(RendezvousException exception)
			{
				Console.Error.WriteLine("Failed to open Rendezvous Environment: {0}", exception.Message);
				Console.Error.WriteLine(exception.StackTrace);
				System.Environment.Exit(1);
			}

			// Create Network transport
			Transport transport = null;
			try
			{
				transport = new NetTransport(service, network, daemon);
			}
			catch (RendezvousException exception)
			{
				Console.Error.WriteLine("Failed to create NetTransport:");
				Console.Error.WriteLine(exception.StackTrace);
				System.Environment.Exit(1);
			}
			
			// Create the message
			Message message = new Message();

			// Set send subject into the message
			try
			{
				message.SendSubject = arguments[argumentsCount++];
			}
			catch (RendezvousException exception) 
			{
				Console.Error.WriteLine("Failed to set send subject:");
				Console.Error.WriteLine(exception.StackTrace);
				System.Environment.Exit(1);
			}

			try
			{
				// Send one message for each parameter
				while (argumentsCount < arguments.Length)
				{
					Console.Out.WriteLine("Publishing: subject={0} \"{1}\"",
						message.SendSubject,
						arguments[argumentsCount]);
					message.AddField(FIELD_NAME, arguments[argumentsCount], 0);
					for (int i = 0; i < SenderApplication.iterations; i++)
					{
						transport.Send(message);
					}
					argumentsCount++;
				}
			}
			catch (RendezvousException exception)
			{
				Console.Error.WriteLine("Error sending a message:");
				Console.Error.WriteLine(exception.StackTrace);
				System.Environment.Exit(1);
			}

			// Close Environment, it will cleanup all underlying memory, destroy
			// transport and guarantee delivery.
			try
			{
				TIBCO.Rendezvous.Environment.Close();
			}
			catch(RendezvousException exception)
			{				
				Console.Error.WriteLine("Exception dispatching default queue:");
				Console.Error.WriteLine(exception.StackTrace);
				System.Environment.Exit(1);
			}
		}
		
		static void Usage()
		{
			Console.Out.Write("Usage: RendezvousSender [-service service] [-network network]");
			Console.Out.Write("                        [-daemon daemon] <subject> <messages>");
			System.Environment.Exit(1);
		}

		static int InitializeParameters(string[] arguments)
		{
			int i = 0;
			while(i < arguments.Length - 1 && arguments[i].StartsWith("-"))
			{
				if (arguments[i].Equals("-service"))
				{
					service = arguments[i+1];
					i += 2;
				}
				else
					if (arguments[i].Equals("-network"))
				{
					network = arguments[i+1];
					i += 2;
				}
				else
					if (arguments[i].Equals("-daemon"))
				{
					daemon = arguments[i+1];
					i += 2;
				}
				else
					if (arguments[i].Equals("-iterations"))
				{
					iterations = Int32.Parse(arguments[i+1]);
					i += 2;
				}
				else
					Usage();
			}
			return i;
		}
	}	
}

 

 


 

 

 

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值