zoukankan      html  css  js  c++  java
  • C# EMS Client

    从 C# 客户端连接 Tibco EMS

    下面例子简要介绍 C# 客户端怎样使用 TIBCO.EMS.dll 来连接 EMS 服务器.

    using System;
    using System.Diagnostics;
    using System.Threading;
    using TIBCO.EMS;
    
    namespace TestEMS
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Test started");
                new Program().Run();
                Console.ReadLine();
            }
    
            private void Run()
            {
                StartEMSServer();
                CreateEMSServerTopicPublisher();
                CreateClientTopicSubscriber("Owner LIKE '%Rich Newman%'"); // Pass "" for no message selector
                EMSServerPublishThisMessage("Hello World", "Owner", "Rich Newman");
            }
    
            #region EMS Server
            private const string tibcoEMSPath = @"C:	ibcoems5.0in";
            private readonly string tibcoEMSExecutable = tibcoEMSPath + "tibemsd.exe";
            private Process tibcoEMSProcess;
            public void StartEMSServer()
            {
                tibcoEMSProcess = new Process();
                ProcessStartInfo processStartInfo = new ProcessStartInfo(tibcoEMSExecutable);
                tibcoEMSProcess.StartInfo = processStartInfo;
                processStartInfo.WorkingDirectory = tibcoEMSPath;
                bool started = tibcoEMSProcess.Start();
                Thread.Sleep(500);
            }
    
            TopicConnection publisherConnection;
            TopicSession publisherSession;
            TopicPublisher emsServerPublisher;
            private void CreateEMSServerTopicPublisher()
            {
                TopicConnectionFactory factory = new TIBCO.EMS.TopicConnectionFactory("localhost");
                publisherConnection = factory.CreateTopicConnection("", ""); // Username, password
                publisherSession = publisherConnection.CreateTopicSession(false, Session.AUTO_ACKNOWLEDGE);
                Topic generalTopic = publisherSession.CreateTopic("GeneralTopic");
                emsServerPublisher = publisherSession.CreatePublisher(generalTopic);
    
                publisherConnection.Start();
            }
    
            internal void EMSServerPublishThisMessage(string message, string propertyName, string propertyValue)
            {
                TextMessage textMessage = publisherSession.CreateTextMessage();
                textMessage.Text = message;
                textMessage.SetStringProperty(propertyName, propertyValue);
                emsServerPublisher.Publish(textMessage);
                Console.WriteLine("EMS Publisher published message: " + message);
            }
    
            #endregion
    
            #region EMS Client
            TopicConnection subscriberConnection;
            TopicSession subscriberSession;
            private void CreateClientTopicSubscriber(string messageSelector)
            {
                TopicConnectionFactory factory = new TIBCO.EMS.TopicConnectionFactory("localhost");
                subscriberConnection = factory.CreateTopicConnection("", "");  // Username, password
                subscriberConnection.Start();
                subscriberSession = subscriberConnection.CreateTopicSession(false, Session.AUTO_ACKNOWLEDGE);
                Topic clientTopic = subscriberSession.CreateTopic("GeneralTopic");
                TopicSubscriber clientTopicSubscriber = subscriberSession.CreateSubscriber(clientTopic, messageSelector, true);
                clientTopicSubscriber.MessageHandler += new EMSMessageHandler(test_MessageHandler);
            }
    
            void test_MessageHandler(object sender, EMSMessageEventArgs args)
            {
                Console.WriteLine("EMS Client received message: " + args.Message.ToString());
            }
    
            #endregion
        }
    }


  • 相关阅读:
    [Tips] 树莓派VNC登录
    [Tips] 联通宽带+华为路由器,如何进行NAT
    [Tips] 树莓派4B 风扇安装
    [Tips] 家庭树莓派,如何外网访问
    [Tips] 命令行获取设备的外网IP
    MySQL 如何让自增id设置为从1开始
    MySQL报错:Packet for query is too large (2,588 > 2,048).
    Java 实现 Timstamp 和 String 互相转换
    MySQL修改 mysql-bin 日志保存天数以及文件大小限制
    Linux Shell 中的年月日 时分秒
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3503474.html
Copyright © 2011-2022 走看看