Not able to retrieve messages from topic using EMS.NET API

不问归期 提交于 2020-01-25 06:35:08

问题


I am trying to write a simple application to send messages to a topic from use input and show messages published on topic. There are two command line executables - one for publisher and another for subscriber. When I publish messages on a topic, I can see the messages getting submitted to the topic.

The following command shows that there are messages on the topic (see F1.gif):-

show stat EMS.Test.Topic

The following command shows that the messages are getting consumed by the subscribers (see F2.gif)

show stat consumers topic=EMS.Test.Topic

However, I am not able to retrieve messages the EMS .NET API. It gets stuck on Message msg = subscriber.Receive();. I made sure the connection details and authentication details are correct because they are used when publishing the messages.

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;
        }

回答1:


There was a silly mistake in my .NET code. the following while loop never returns so there is no return. I need to break the while loop when I get a message. Duh!!!!

 while (true)
 {
         Message msg = subscriber.Receive();

         if (msg == null)
         {
             break;
         }
         if (msg is TextMessage)
         {
             TextMessage tm = (TextMessage) msg;
             messageFromPublisher = tm.Text;
             break;
         }

 }


来源:https://stackoverflow.com/questions/16444577/not-able-to-retrieve-messages-from-topic-using-ems-net-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!