tags:

views:

85

answers:

1

I haven't touched any J2EE stuff in years and I need to whip up a quick JMS client for a demo. I'm using Eclipse, on OS X and I can't even get started because I can't seem to figure out how to get the required libraries.

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

+2  A: 

Every JMS implementation has its own set of libraries that specify how you get the initial connection factory. If you have an existing server from which to pull messages, you need to examine the documentation of that server to determine where to find the libraries to put in your classpath and how to create your initial connection factory. If you want to create a server for the purposes of the demonstration, I recommend using an embedded Active MQ broker.

Once you have your connection factory, polling for messages from a topic is pretty straightforward. Here is some example code which can be called to drain a topic of its current messages.

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

Note the use of a durable subscriber. This means that I don't have to try to maintain a single connection all the time and handle the errors if it times out somehow. But because the subscription is durable, the server knows to retain any messages the topic receives while I'm not connected and provide them on my next connection. This code would be the same regardless of the host OS. The only tricky part is the creation of the provider specific connection factory.

Jherico
Unfortunately I've been given connection information and need to pull from there (user, pw, and URL). I suppose I'll have to go back to the group that set up the topic and ask them. Thanks.
Yeah. You'll definitely need to ask what JMS implementation to use. ActiveMQ, OpenMQ, JBoss Messaging, etc.
Dave Ray