c stomp durable topic consumer

Thu, Jul 30, 2009

If you’ve got the Apache ActiveMQ C++ CMS library working you’re ready to start durably consuming messages from a topic. If not, follow my instructions to bootstrap your CMS kit. Why would you want to be a durable consumer of messages? Well, you can disconnect at your leisure, come back later and your messages will be waiting for you. Here’s how to do it.

Let’s get the easy bit out of the way first, the message producer, which has to send messages marked as persistent to the topic, to let the broker know to keep them around for durable consumers. Here’s a simple Ruby message producer:

require ‘stomp’
client = Stomp::Client.open “stomp://localhost:61613”,
    { :headers => {‘client-id’ => ‘rubyclient’} }
client.send(‘/topic/mqtest.t’, “hello from the ruby client!”,
    ‘persistent’ => true)
the “client-id” header is important for persistent messages and on the consumer side, the client-id and subscription name have to match. Here’s the durable consumer, using the CMS library:
std::string stompURI =
    “tcp://localhost:61613”
    “?wireFormat=stomp”;

auto_ptr connectionFactory( ConnectionFactory::createCMSConnectionFactory(stompURI));

// We don’t need a username and password other than to get this method signature // This sets the client-id header on the connection std::string clientID = “gadfly”; Connection* conn = connectionFactory->createConnection(“”, “”, clientID); conn->start();

Session* session = conn->createSession(Session::AUTO_ACKNOWLEDGE);

ActiveMQTopic* topic = new ActiveMQTopic(“mqtest.t”);

// This sets the subscription name for the session - must match the client-id MessageConsumer* mc = session->createDurableConsumer(topic, “gadfly”, “”, true); mc->setMessageListener(this);

Now when you run the producer, if the consumer is running, it’ll get the message straight away. Turn off the consumer and send a few messages using the producer and when you start the consumer again, it’ll get all the missed messages.

comments powered by Disqus