【问题标题】:Not able to retrieve messages from topic using EMS.NET API无法使用 EMS.NET API 从主题中检索消息
【发布时间】:2013-05-09 00:08:19
【问题描述】:

我正在尝试编写一个简单的应用程序来从使用输入向主题发送消息并显示在主题上发布的消息。 有两个命令行可执行文件 - 一个用于发布者,另一个用于订阅者。 当我在某个主题上发布消息时,我可以看到消息被提交到该主题。

以下命令显示该主题有消息(见F1.gif):-

show stat EMS.Test.Topic

以下命令显示消息正在被订阅者消费(参见 F2.gif)

show stat consumers topic=EMS.Test.Topic

但是,我无法通过 EMS .NET API 检索消息。它卡在Message msg = subscriber.Receive(); 上。我确保连接详细信息和身份验证详细信息正确,因为它们在发布消息时使用。

public string ReceiveMessagesFromTopic(string topicName)
        {
            TopicConnection connection = null;
            string messageFromPublisher = string.Empty;
            try
            {
                var factory = new TIBCO.EMS.TopicConnectionFactory(serverUrl);
                connection = factory.CreateTopicConnection(userName, password);
                TopicSession session = connection.CreateTopicSession(false, Session.AUTO_ACKNOWLEDGE);
                Topic topic = session.CreateTopic(topicName);
                TopicSubscriber subscriber = session.CreateSubscriber(topic);
                connection.Start();
                while (true)
                {
                   Message msg = subscriber.Receive();
                    if (msg == null)
                    {
                        break;
                    }
                    if (msg is TextMessage)
                    {
                        TextMessage tm = (TextMessage) msg;
                        messageFromPublisher = tm.Text;
                    }

                }
                connection.Close();
            }
            catch (EMSException e)
            {
                if (connection!=null)
                {
                    connection.Close();
                }


                throw;
            }

            return messageFromPublisher;
        }

【问题讨论】:

    标签: tibco-ems ems tibco-topic


    【解决方案1】:

    我的 .NET 代码中有一个愚蠢的错误。下面的 while 循环永远不会返回,所以没有返回。当我收到消息时,我需要打破 while 循环。呸!!!!

     while (true)
     {
             Message msg = subscriber.Receive();
    
             if (msg == null)
             {
                 break;
             }
             if (msg is TextMessage)
             {
                 TextMessage tm = (TextMessage) msg;
                 messageFromPublisher = tm.Text;
                 break;
             }
    
     }
    

    【讨论】:

    • 嗨,即使对我来说,控件也停止在subscriber.receive。如果 (msg == null) 本身,它不会排队。如果主题中没有消息,则控件本身不会移动到下一行。所以想知道上面是否是正确的解决方案......
    猜你喜欢
    • 2018-08-25
    • 1970-01-01
    • 2018-08-29
    • 2019-06-08
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    相关资源
    最近更新 更多