2

I´m trying to reveive messages from an ActiveMQ topic. In the web console i see that numerous messages are enqueued in the topic, but running the following code doesn´t return anything:

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

Any hint would be appreciated.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Prefect73
  • 321
  • 3
  • 14

1 Answers1

2

In order for a Durable subscription to receive messages sent while it was offline it must first be registered on the Broker. You register it by creating an instance like you have done in the code given and then once it goes offline, via a call to close() etc messages sent to it's Topic will be stored so that it can read them later. If you have not registered this consumer already then those messages that were sent to the Topic are just dropped.

You also need a unique client Id for the Connection so that each time you reconnect you can re-subscribe the durable Topic consumer.

Register the durable topic consumer:

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

Consume Messages at a later time:

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));
}
Tim Bish
  • 17,475
  • 4
  • 32
  • 42
  • Thanks, that (almost) did it. I found that it´s inevitable to reference to that ClientId by passing the it as parameter to the CreateDurableConsumer method ("OBSnap" in my example). Strange enough i already tried to provide the ClientId, but i passed it as a parameter to the ConnectionFactory method, but that didn´t work. So it obviously has to be specified the explicit way. – Prefect73 Nov 13 '13 at 07:53
  • Can't really decipher that, so can't offer any insights – Tim Bish Nov 13 '13 at 11:23
  • to cut it short, your solution only works, if i specify the ClientId in the Consumer instantiation, like that: `consumer = session.CreateDurableConsumer(topic,"MyClientId",null, false);` – Prefect73 Nov 13 '13 at 13:48
  • sounds like you are using the Stomp client, which in that case this would make sense. – Tim Bish Nov 13 '13 at 14:44