2

我正在尝试从 ActiveMQ 主题中接收消息。在 Web 控制台中,我看到许多消息在主题中排队,但运行以下代码不会返回任何内容:

IConnectionFactory factory = new ConnectionFactory(new Uri("tcp://localhost:61616?wireformat.version=2"));
using (IConnection connection = factory.CreateConnection())
{
     connection.Start();
     ISession session = connection.CreateSession();
     ActiveMQTopic topic = new ActiveMQTopic("MARKETADAPTERS.ORDERBOOKSNAPSHOT");
     consumer = session.CreateDurableConsumer(topic,"OBSnap",null, false);
     message = (ActiveMQTextMessage)consumer.Receive(TimeSpan.FromSeconds(vTimeOutSecs));
}

任何提示将不胜感激。

4

1 回答 1

2

为了使 Durable 订阅能够接收离线时发送的消息,它必须首先在 Broker 上注册。您可以通过创建一个实例来注册它,就像您在给定的代码中所做的那样,然后一旦它离线,通过调用 close() 等发送到它的主题的消息将被存储,以便以后可以读取它们。如果您尚未注册此使用者,则发送到该主题的那些消息将被丢弃。

您还需要连接的唯一客户端 ID,以便每次重新连接时都可以重新订阅持久主题消费者。

注册持久主题消费者:

IConnectionFactory factory = new ConnectionFactory(new Uri("tcp://localhost:61616?wireformat.version=2"));
using (IConnection connection = factory.CreateConnection())
{
    connectio.ClientId = "MyClientId";
    connection.Start();
    ISession session = connection.CreateSession();
    ActiveMQTopic topic = new ActiveMQTopic("MARKETADAPTERS.ORDERBOOKSNAPSHOT");
    consumer = session.CreateDurableConsumer(topic,"OBSnap",null, false);
}

稍后使用消息:

IConnectionFactory factory = new ConnectionFactory(new Uri("tcp://localhost:61616?wireformat.version=2"));
using (IConnection connection = factory.CreateConnection())
{
    connection.ClientId = "MyClientId";
    connection.Start();
    ISession session = connection.CreateSession();
    ActiveMQTopic topic = new ActiveMQTopic("MARKETADAPTERS.ORDERBOOKSNAPSHOT");
    consumer = session.CreateDurableConsumer(topic,"OBSnap",null, false);
    message = (ActiveMQTextMessage)consumer.Receive(TimeSpan.FromSeconds(vTimeOutSecs));
}
于 2013-11-12T20:44:09.033 回答