JMS over HTTP
gberish Mar 19, 2005 4:28 PMHi,
The EJB and JMS forums said JMS over HTTP is easy, but it still defeats me. I hope this is simple enough to fit in them beginners forum, because I need a simple beginning point.
MessageTest below is a client that sends and retreives a JMS message that says "Success" from a Queue whose JNDI name is queue/goIn.
The type parameter in mb.setUp (int type) in main() (Where type = [REG | HTTP]) determines whether the send and receive go over HTTP.
type = REG works fine: See "Screen Print REG" below the code
type = HTTP fails as shown: See "Screen Print HTTP" below that.
Are there some deployment xml files I need to modify?
It seems that this simple model would be useful to other new guys too, so I hope it is as simple as the more advanced foriums say..
import java.lang.reflect.Method; import java.util.Properties; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.ConnectionFactory; import javax.jms.QueueConnectionFactory; import javax.jms.QueueReceiver; import javax.jms.QueueSession; import javax.jms.QueueSender; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class MessageTest { public static void main (String[] args) { MessageTest mb = new MessageTest (); /** * The type parameter in setUp (int type) * may be changed manually between * REG & HTTP **/ mb.setUp (REG); } // method public void setUp (int type) { System.out.println ("Type = " + TYPE [type]); try { // get initial context Properties p = new Properties(); switch (type) { case REG: p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); p.put(Context.PROVIDER_URL, "jnp://localhost:1099"); p.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces"); break; case HTTP: p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.HttpNamingContextFactory"); p.put(Context.PROVIDER_URL, "http://localHost:80/invoker/JMXInvokerServlet"); p.put(javax.naming.Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); break; } System.out.println ("Calling InitialContext"); ctx = new InitialContext(p); System.out.println ("InitialContext Found"); // lookup connection factory factory = (QueueConnectionFactory)ctx.lookup("ConnectionFactory"); System.out.println ("Connection Factory instantiated"); // lookup destination destination = (Queue)ctx.lookup("queue/goIn"); System.out.println ("Destination instantiated"); // create connection connection = factory.createQueueConnection(); System.out.println ("Connection instantiated"); // create session session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); System.out.println ("Session instantiated"); // create sender sender = session.createSender(destination); System.out.println ("Sender instantiated"); // create receiver receiver = session.createReceiver(destination); System.out.println ("Receiver instantiated"); // create message w/text & selector property message = session.createTextMessage("Success"); System.out.println ("Message instantiated"); message.setStringProperty ("Player", "one"); System.out.println ("Message Property 'Player' set to 'one'"); // starting connection connection.start (); System.out.println ("Connection Started"); // send message sender.send(message); System.out.println ("Message Sent"); // receive message message = (TextMessage) receiver.receive (); System.out.println ("Message Received: " + message.getText ()); } catch (NamingException e) { System.out.println ("Naming Exception"); e.printStackTrace (); } catch (JMSException e) { System.out.println ("JMS Execption"); e.printStackTrace (); } finally { if (connection != null) { try { connection.stop (); connection.close(); } catch (JMSException e) { } } } System.out.println("SetUp Done."); } // members static final int REG = 0; static final int HTTP = 1; static final String[] TYPE = {"REG", "HTTP"}; int type; Context ctx; Queue destination; QueueConnection connection; QueueConnectionFactory factory; QueueReceiver receiver; QueueSession session; QueueSender sender; TextMessage message; }Screen Print REG:
Type = REG Calling InitialContext InitialContext Found Connection Factory instantiated Destination instantiated Connection instantiated Session instantiated Sender instantiated Receiver instantiated Message instantiated Message Property 'Player' set to 'one' Connection Started Message Sent Message Received: Success SetUp Done. Press any key to continue . . .Screen Print HTTP
Type = HTTP Calling InitialContext Naming Exception javax.naming.NamingException: Failed to retrieve Naming interface [Root exception is java.io.FileNot FoundException: http://localHost:80/invoker/JMXInvokerServlet] at org.jboss.naming.HttpNamingContextFactory.getInitialContext(HttpNamingContextFactory.java :69) at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247) at javax.naming.InitialContext.init(InitialContext.java:223) at javax.naming.InitialContext.<init>(InitialContext.java:197) at MessageTest.setUp(MessageTest.java:58) at MessageTest.main(MessageTest.java:29) Caused by: java.io.FileNotFoundException: http://localHost:80/invoker/JMXInvokerServlet at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java: 39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorIm pl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:494) at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1186) at java.security.AccessController.doPrivileged(Native Method) at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:11 80) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:877) at org.jboss.naming.HttpNamingContextFactory.getNamingServer(HttpNamingContextFactory.java:1 18) at org.jboss.naming.HttpNamingContextFactory.getInitialContext(HttpNamingContextFactory.java :65) ... 6 more Caused by: java.io.FileNotFoundException: http://localHost:80/invoker/JMXInvokerServlet at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1131) at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:1850) at java.net.URLConnection.getHeaderFieldInt(URLConnection.java:573) at java.net.URLConnection.getContentLength(URLConnection.java:468) at org.jboss.naming.HttpNamingContextFactory.getNamingServer(HttpNamingContextFactory.java:1 13) ... 7 more SetUp Done. Press any key to continue . . .