.NET使用C#连接TIBCO的JMS消息服务

 
  
using System;
using System.Collections.Generic;
using System.Text;
using TIBCO.EMS;

namespace EII.JMS
{
public class MsgReceiver
{
private TopicSubscriber subscriber;
private TopicConnection connection;

public string ServerUrl { get ; set ; }
public string UserName { get ; set ; }
public string PassWord { get ; set ; }
public string TopicName { get ; set ; }
public string DurableName { get ; set ; }
public string ClientID { get ; set ; }

/// <summary>
/// JMS消息接收器
/// </summary>
/// <param name="serverUrl"> 服务器地址 </param>
/// <param name="userName"> 用户名 </param>
/// <param name="passWord"> 密码 </param>
/// <param name="topicName"> Topic名称 </param>
/// <param name="durableName"> Durable名称 </param>
/// <param name="clientID"> 客户端ID </param>
public MsgReceiver( string serverUrl, string userName, string passWord, string topicName, string durableName, string clientID)
{
this .ServerUrl = serverUrl;
this .UserName = userName;
this .PassWord = passWord;
this .TopicName = topicName;
this .DurableName = durableName;
this .ClientID = clientID;
}

/// <summary>
/// 连接服务器
/// </summary>
/// <returns></returns>
public bool ConnectServer()
{
try
{
TopicConnectionFactory factory
= new TIBCO.EMS.TopicConnectionFactory(ServerUrl);
connection
= factory.CreateTopicConnection(UserName, PassWord);

if (ClientID != null )
{
connection.ClientID
= ClientID;
}

TopicSession session
= connection.CreateTopicSession( false , Session.AUTO_ACKNOWLEDGE);
Topic topic
= session.CreateTopic(TopicName);
subscriber
= session.CreateDurableSubscriber(topic, DurableName);
connection.Start();
return true ;
}
catch (EMSException e)
{
throw new Exception(e.ToString());
}
}

/// <summary>
/// 接收消息
/// </summary>
/// <returns></returns>
public string Receive()
{
try
{
BytesMessage message
= subscriber.Receive() as BytesMessage;

if (message != null )
{
byte [] file = new byte [message.BodyLength];
message.ReadBytes(file);

return System.Text.Encoding.UTF8.GetString(file);
}
else
{
return null ;
}
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// 关闭连接
/// </summary>
public void CloseConn()
{
try
{
this .connection.Close();
}
catch (Exception ex)
{
throw ex;
}
}
}
}

 

转载于:https://www.cnblogs.com/kxlf/archive/2010/03/19/1689545.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值