/**
* simple hello world consumer
*/
public class HelloConsumer
{
/**
* simple consumer
*/
public HelloConsumer() {
try {
Hashtable env;
Context ctx = null;
env = new Hashtable();
// Store the environment variables that tell JNDI which initial context
// to use and where to find the provider.
// For use with the File System JNDI Service Provider
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL, "file:///tmp/jndi");
try {
// Create the initial context.
ctx = new InitialContext(env);
} catch (NamingException ex) {
ex.printStackTrace();
System.exit(-1);
}
// creating a connection factory
ConnectionFactory cf= new com.sun.messaging.ConnectionFactory();
ConnectionFactory cf = null;
try {
// Lookup my connection factory from the admin object store.
// The name used here here must match the lookup name
// used when the admin object was stored.
System.out.println("Looking up Connection Factory object with lookup name: HelloConnectionFactory");
cf = (javax.jms.ConnectionFactory) ctx.lookup("HelloConnectionFactory");
System.out.println("Connection Factory object found.");
} catch (NamingException ne) {
System.err.println("Failed to lookup Connection Factory object.");
ne.printStackTrace();
System.exit(-1);
}
// create a connection
Connection connection = cf.createConnection();
// create a session
Session session = connection.createSession(
false /* not transacted */, Session.AUTO_ACKNOWLEDGE);
Queue queue = null;
try {
// Lookup my queue from the admin object store.
// The name I search for here must match the lookup name used when
// the admin object was stored.
System.out.println("Looking up Queue object with lookup name: HelloQueue");
queue = (javax.jms.Queue)ctx.lookup("HelloQueue");
System.out.println("Queue object found.");
} catch (NamingException ne) {
System.err.println("Failed to lookup Queue object.");
ne.printStackTrace();
System.exit(-1);
}
// create a consumer
MessageConsumer consumer = session.createConsumer(destination);
// now that everything is ready to go, start the connection
connection.start();
// receive our message
TextMessage m = (TextMessage)consumer.receive();
System.out.println(m.getText());
// close everything
consumer.close();
session.close();
connection.close();