Simple JMS clients on OS X

I haven't touched on any J2EE stuff for years, and need to get a quick JMS client up and running for a demonstration. I am using Eclipse on OS X and I can't even get started because I can't figure out how to get the libraries I need.

This is supposed to be a simple standalone application (not running in a container) that pulls messages from a topic.

+2


source to share


1 answer


Each JMS implementation has its own set of libraries that define how you get the initial factory connection. If you have an existing server to pull messages from, you need to look at the documentation on that server to determine where to look for libraries to place on the classpath and how to create the original factory connection. If you want to build a server for the demo, I recommend using the built-in Active MQ .

Once you have a factory connection, polling posts from the topic is pretty simple. Here is some sample code you can call to leak the subject line of its current posts.



  // Implementation specific code
 public void drainTopic(TopicConnectionFactory factory, String topicName, String subscriberId)
    // set factory properties like the server ID
    Connection conn = factory.createConnection();
    conn.start();
    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic topic = session.createTopic(topicName);
    MessageConsumer consumer = session.createDurableSubscriber(topic, subscriberId);
    Message message;
    while (null != (message = consumer.receive(1000))) {
        // do work on the message 
    }
    conn.close();
}

      

Pay attention to the use of a strong subscriber. This means I don't have to keep a single connection all the time and handle errors if it goes away somehow. But since the subscription is long lasting, the server knows to keep the messages that the topic receives while I'm not connected and provide them the next time I connect. This code will be the same regardless of the host OS. The only tricky part is creating a provider-specific factory connection.

+2


source







All Articles