2 Replies Latest reply on Nov 7, 2006 7:02 AM by Guy Coleman

    Frustration with basic client and broken examples.

    Dave Belfer-Shevett Newbie

      We have a need to implement some basic P2P JMS queues in Jboss 4.0.4-GA, and I'm trying to set up a simple example using the sample queue/A JMS queue. The problem is the example in the JBoss wiki does not work, and trying to figure out the 'proper' way to do it is intensely frustrating. What with SpyDestination references and the like - this seems to be a path that I shold not be taking.

      The example code I tried is here:
      http://wiki.jboss.org/wiki/Wiki.jsp?page=QueueExample

      Here's my sample code - it's a bit verbose, but perhaps someone can figure out if I'm mixing approaches or what...

      import javax.naming.NamingException;
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      
      import java.util.Properties;
      
      import javax.annotation.Resource;
      import javax.jms.Message;
      import javax.jms.Queue;
      import javax.jms.QueueConnection;
      import javax.jms.QueueConnectionFactory;
      import javax.jms.QueueReceiver;
      import javax.jms.QueueSender;
      import javax.jms.QueueSession;
      import javax.jms.Session;
      import javax.jms.TextMessage;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.Reference;
      import org.jboss.mq.SpyQueue;
      
      
      public class Test extends HttpServlet {
      
       static Logger logger = Logger.getLogger(Test.class);
      
       InitialContext initialContext;
       @Resource(mappedName="queue/A") Queue queue;
      
       // snip snip
       QueueConnection qc;
       log.info("Creating jndi context - alternatively use a jndi.properties");
       Properties properties = new Properties();
       properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
       properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
       properties.put(Context.PROVIDER_URL, "localhost");
      
       InitialContext ctx = new InitialContext(properties);
      
       log.info("Looking up connection factory");
       QueueConnectionFactory qcf = (QueueConnectionFactory) ctx.lookup("UIL2ConnectionFactory");
      
       log.info("Creating connection");
       qc = qcf.createQueueConnection();
       log.info("Creating session");
       QueueSession qs = qc.createQueueSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
      
       log.info("Creating sender");
       QueueSender sender = qs.createSender (queue);
      
       log.info("Creating message");
       TextMessage message = qs.createTextMessage("hello");
      
      
       log.info("Sending message");
       // This is the error -
       //11:49:13,031 INFO [STDOUT] Sending message
       //11:49:13,031 ERROR [STDERR] java.lang.UnsupportedOperationException: Not constructed with identifyed destination. Usage of method not allowed
       //11:49:13,031 ERROR [STDERR] at org.jboss.mq.SpyMessageProducer.send(SpyMessageProducer.java:204)
       //11:49:13,031 ERROR [STDERR] at com.areteinc.servlets.Test.doGet(Test.java:72)
      
       sender.send(queue,message);
      
       log.info("Creating receiver");
       QueueReceiver receiver = qs.createReceiver(queue);
      
       log.info("Try to receive message, it will not work");
       Message received = receiver.receiveNoWait();
       if (received != null)
       throw new RuntimeException("Should not get a message if the connection is not started!");
      
       log.info("You have to start the connection before receiving messages");
       qc.start();
      
       log.info("This receive will work");
       received = receiver.receiveNoWait();
      
       log.info("Got message: " + received);
      // snip snip
      


      If I put in a destination type of 'queue' in the sender() call, it tells me this is not a SpyDestination, and we go off spinning in 'spy' land.

      Not having a simple, clear example of using JBossMQ, or a clear example of using JBoss Messaging is very frurstating. Help please! We're dead in the water right now.

        • 1. Re: Frustration with basic client and broken examples.
          Dave Belfer-Shevett Newbie

          I have gotten the example to successfully run, but I'm not 100% -why- it runs now and didn't before. Here's the updated code, hopefully someone can validate whether this should be put into the wiki or not:

          Do we really need the direct references to the Spy* classes?

          package com.areteinc.queue;
          
          
          import java.util.Properties;
          
          import javax.jms.Message;
          import javax.jms.Queue;
          import javax.jms.QueueConnection;
          import javax.jms.QueueConnectionFactory;
          import javax.jms.QueueReceiver;
          import javax.jms.QueueSession;
          import javax.jms.Session;
          import javax.jms.TextMessage;
          import javax.naming.Context;
          import javax.naming.InitialContext;
          import org.jboss.mq.SpyQueueSender;
          
          public class QueueExample
          {
          
           // @Resource(mappedName="queue/A")
           // static SpyDestination queue;
           public static void main(String[] args) throws Exception
           {
           log.info("Creating jndi context - alternatively use a jndi.properties");
           Properties properties = new Properties();
           properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
           properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
           properties.put(Context.PROVIDER_URL, "localhost");
          
           InitialContext ctx = new InitialContext(properties);
          
           log.info("Looking up queue");
          
           Context remoteCtx = new InitialContext(properties);
           Queue queue = (Queue)remoteCtx.lookup("queue/A");
          
           log.info("Looking up connection factory");
           QueueConnectionFactory qcf = (QueueConnectionFactory) ctx.lookup("UIL2ConnectionFactory");
          
           log.info("Creating connection");
           QueueConnection qc = qcf.createQueueConnection();
           try
           {
           log.info("Creating session");
           QueueSession qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
          
           log.info("Creating sender");
           SpyQueueSender sender = (SpyQueueSender)qs.createSender(queue);
          
           log.info("Creating message");
           TextMessage message = qs.createTextMessage("hello");
          
           log.info("Sending message");
           sender.send(queue,message);
          
           log.info("Creating receiver");
           QueueReceiver receiver = qs.createReceiver(queue);
          
           log.info("Try to receive message, it will not work");
           Message received = receiver.receiveNoWait();
           if (received != null)
           throw new RuntimeException("Should not get a message if the connection is not started!");
          
           log.info("You have to start the connection before receiving messages");
           qc.start();
          
           log.info("This receive will work");
           received = receiver.receiveNoWait();
          
           log.info("Got message: " + received);
           }
           finally
           {
           qc.close();
           }
           }
          
           public static class log
           {
           public static void info(String message)
           {
           System.out.println(message);
           }
           public static void error(String message, Throwable t)
           {
           System.err.println(message);
           t.printStackTrace();
           }
           }
          }


          • 2. Re: Frustration with basic client and broken examples.
            Guy Coleman Newbie

            In your first code snippet you're using annotations to get the reference to the queue: @Resource(mappedName="queue/A") Queue queue;

            In the second you're using a JNDI lookup.

            I've not used annotations to do this sort of thing so don't know much about how that's supposed to work, but that appears to be the difference.