Version 14

    WARNING:  THIS EXAMPLE IS OUT OF DATE AND FAIRLY CRAPPY see: http://docs.jboss.org/jbossas/jboss4guide/r5/html/ch6.chapt.htmld0e12917 for a much better one.

     

    Queue Send/Receive

     

    The following code shows how to send and receive messages from a Queue.

     

      Code

    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.QueueSender;
    import javax.jms.QueueSession;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    
    public class Client
    {
    
       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");
          Queue queue = (Queue) ctx.lookup("queue/testQueue");
    
          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");
             QueueSender sender = qs.createSender(queue);
    
             log.info("Creating message");
             TextMessage message = qs.createTextMessage("hello");
    
             log.info("Sending message");
             sender.send(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();
          }
       }
    }
    

     

    Instructions

    To compile it:
    javac -classpath JBOSS_HOME/client/jbossall-client.jar Client.java
    or windows
    javac -classpath JBOSS_HOME\client\jbossall-client.jar Client.java
    
    To run it:
    java -classpath .:JBOSS_HOME/client/jbossall-client.jar Client
    or windows
    java -classpath .;JBOSS_HOME\client\jbossall-client.jar Client
    

     

    Output

    Creating jndi context - alternatively use a jndi.properties
    Looking up queue
    Looking up connection factory
    Creating connection
    Creating session
    Creating sender
    Creating message
    Sending message
    Creating receiver
    Try to receive message, it will not work
    You have to start the connection before receiving messages
    This receive will work
    Got message: SpyTextMessage {
    Header { 
       jmsDestination  : QUEUE.testQueue
       jmsDeliveryMode : 2
       jmsExpiration   : 0
       jmsPriority     : 4
       jmsMessageID    : ID:3-10797528788341
       jmsTimeStamp    : 1079752878834
       jmsCorrelationID: null
       jmsReplyTo      : null
       jmsType         : null
       jmsRedelivered  : false
       jmsProperties   : {}
       jmsPropReadWrite: false
       msgReadOnly     : true
       producerClientId: ID:3
    }
    Body {
        text            :hello
    }
    }