2013-05-08 56 views
1

我正在嘗試編寫一個簡單的應用程序,以使用輸入和顯示主題上發佈的消息將消息發送到主題。 有兩個命令行可執行文件 - 一個用於發佈者,另一個用於訂閱者。 當我發佈關於主題的消息時,我可以看到提交給該主題的消息。無法使用EMS.NET API從主題檢索消息

下面的命令表明,有在主題的消息(見F1.gif): -

show stat EMS.Test.Topic 

enter image description here

下面的命令顯示的消息越來越被用戶所消耗(見F2.gif)

show stat consumers topic=EMS.Test.Topic 

enter image description here

但是,我無法檢索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; 
     } 

回答

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

} 
+0

嗨,即使對我來說,控制停止在subscriber.receive。如果(msg == null)本身,它不會行。如果主題中沒有消息,則控件不會移至下一行本身。所以我想知道以上是否是正確的解決方案... – user1447718 2014-03-10 04:08:48

相關問題