zoukankan      html  css  js  c++  java
  • .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;
    }
    }
    }
    }
  • 相关阅读:
    vulcanjs 包类型
    vulcanjs schemas&& collections
    vulcanjs 核心架构概念
    vulcanjs 开源工具方便快速开发react graphql meteor 应用
    ory Oathkeeper Ecosystem
    ory Oathkeeper docker-compose 安装运行
    benthos stream nats 集成试用
    benthos 几个方便的帮助命令
    benthos 通过配置文件配置 stream 说明
    benthos 通过rest api 配置 stream 说明
  • 原文地址:https://www.cnblogs.com/kxlf/p/1689545.html
Copyright © 2011-2022 走看看