Configuring a Stand-alone consumer
From Resin 3.0
monelo Note: this does not work yet, any help would be greatly appreciated! My goal is to have a nice php form submit jobs to a queue and have a standalone java application respond.
when i run this app, here is the output:
- >java SimpleConsumer jms/Queue
Destination name is jms/Queue Progress JNDI API lookup failed: java.lang.NullPointerException
So that tells me it makes it to the lookup of the jms/ConnectionFactory item.
SimpleConsumer.java:
import javax.jms.*; import javax.naming.*; import java.util.Hashtable;
public class SimpleConsumer {
public static void main(String[] args) { String destName = null; Context jndiContext = null; ConnectionFactory connectionFactory = null; Connection connection = null; Session session = null; Destination dest = null; MessageConsumer consumer = null; TextMessage message = null;
if (args.length != 1) { System.out.println("java SimpleConsumer: dest_name"); System.exit(1); }
destName = new String(args[0]); System.out.println("Destination name is " + destName);
// Properties env = new Properties( );
Hashtable env = new Hashtable(); //env.put(Context.INITIAL_CONTEXT_FACTORY, "com.caucho.jms.ConnectionFactoryImpl"); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.caucho.naming.InitialContextFactoryImpl"); env.put(Context.URL_PKG_PREFIXES, "com.caucho.naming");
// ... specify the JNDI properties specific to the vendor
try { InitialContext jndi = new InitialContext(env); //jndiContext = new InitialContext(env); } catch (NamingException e) { System.out.println("Could not create JNDI API context: " + e.toString()); System.exit(1); } System.out.println("Progress");
try { connectionFactory = (ConnectionFactory) jndiContext.lookup( "jms/ConnectionFactory"); System.out.println("more Progress"); dest = (Destination) jndiContext.lookup(destName); } catch (Exception e) { System.out.println("JNDI API lookup failed: " + e.toString()); System.exit(1); }
try { connection = connectionFactory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); consumer = session.createConsumer(dest); connection.start();
while (true) { Message m = consumer.receive(1);
if (m != null) { if (m instanceof TextMessage) { message = (TextMessage) m; System.out.println("Reading message: " + message.getText()); } else { break; } } } } catch (JMSException e) { System.out.println("Exception occurred: " + e.toString()); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { } } } }
}