无法使用EMS.NET API从主题检索消息 [英] Not able to retrieve messages from topic using EMS.NET API
问题描述
我正在尝试编写一个简单的应用程序,以通过使用输入向主题发送消息并显示在该主题上发布的消息. 有两个命令行可执行文件-一个用于发布者,另一个用于订阅者. 当我发布有关某个主题的消息时,我可以看到这些消息已提交给该主题.
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.
以下命令显示有关该主题的消息(请参阅F1.gif):-
The following command shows that there are messages on the topic (see F1.gif):-
show stat EMS.Test.Topic
以下命令显示消息已被订阅者使用(请参阅F2.gif)
The following command shows that the messages are getting consumed by the subscribers (see F2.gif)
show stat consumers topic=EMS.Test.Topic
但是,我无法检索EMS .NET API的消息.它卡在Message msg = subscriber.Receive();
上.我确保连接详细信息和身份验证详细信息正确无误,因为它们在发布消息时使用.
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;
}
推荐答案
.NET代码中存在一个愚蠢的错误.接下来的while循环永不返回,因此没有返回.收到消息时,我需要中断while循环. h !!!!
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;
}
}
这篇关于无法使用EMS.NET API从主题检索消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!