User: pra     
  Date: 01/08/07 14:08:59

  Added:       src/examples/org/jboss/docs/jms/client
                        DestinationHelper.java DestinationHelper25.java
                        HelloPublisher.java HelloPublisher25.java
                        HelloReceiver.java HelloReceiver25.java
                        HelloSender.java HelloSender25.java
                        HelloSubscriber.java HelloSubscriber25.java
                        ManualJNDI.java
  Log:
  Added example code for new JMS chapter
  
  Revision  Changes    Path
  1.1                  
manual/src/examples/org/jboss/docs/jms/client/DestinationHelper.java
  
  Index: DestinationHelper.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  package org.jboss.docs.jms.client;
  
  import java.io.InputStream;
  
  import java.net.URL;
  import java.net.HttpURLConnection;
  
  import javax.management.MBeanServerFactory;
  import javax.management.MBeanServer;
  import javax.management.ObjectName;
  
  import javax.jms.Topic;
  import javax.jms.Queue;
  
  /**
   * Demonstration of how to administer JMS destinations programatically for 
   * JBossMQ.
   *
   * To use from a remote client, adapt the HOST and PORT variable to your local
   * settings.
   *
   * Only works up to and including JBoss 2.4.x
   *
   * @author Peter Antman
   * @version $Revision: 1.1 $
   */
  
  public class DestinationHelper {
     /**
      * Host where JBoss is
      */
     static final String HOST = "localhost";
  
     /**
      * Port of the JMX HTML admin gui
      */
     static final int PORT = 8082;
  
     /**
      * Base url to invoke
      */
     static final String BASE_URL_ARG = "/InvokeAction//JMS%3Aservice%3DJMSServer"; 
  
  
      /**
       * Create a destination. Works both if you are in the same VM as JBossMQ
       * or if you are a remote client.
       *
       * @param type Topic.class or Queue.class
       * @param name name of the destination, without prefix queue/ or topic/
       */
     public static void createDestination(Class type, String name) 
        throws Exception
     {
        String method = null;
        if (type == Topic.class) {
         method = "newTopic";
        }
        else if (type == Queue.class) {
         method = "newQueue";
        }
        invoke(method, name);
        
     }
  
      /**
       * Destroy a destination. Works both if you are in the same VM as JBossMQ
       * or if you are a remote client.
       *
       * @param type Topic.class or Queue.class
       * @param name name of the destination, without prefix queue/ or topic/
       */
     public static void destroyDestination(Class type, String name) 
        throws Exception
     {
        String method = null;
        if (type == Topic.class) {
         method = "destroyTopic";
        }
        else if (type == Queue.class) {
         method = "destroyQueue";
        }
        invoke(method, name);
     }
     
      /**
       * Helper method, decides if we are in the same VM as JBossMQ, or are a
       * remote client.
       *
       * @param method name of the MBean method to invoke
       * @param destName name of the destination
       */
     protected static void invoke(String method, String destName) 
        throws Exception
     {
        // Check if we can get the MBean server
        try {
         MBeanServer server = (MBeanServer)
         MBeanServerFactory.findMBeanServer(null).iterator().next();
         
         // We are native to the MBean server, invoke directly
         invokeViaMBean(method, destName);
        }catch(Exception ex) {
          
         // We are a standalone client, invoke via URL
         invokeViaUrl(method, destName);
        }
        
     }
     
      /**
       * Invoke the JMSServer Mbean via URL.
       *
       * @param method name of the MBean method to invoke
       * @param destName name of the destination
       */
     protected static void invokeViaUrl(String method, String destName) 
        throws Exception
      {
        // Construct the correct url, see to it that HOST and PORT are correct
        String action = "action=" + method + "?action=" + method + 
         "&param0%2Bjava.lang.String=" + destName;
  
        String arg = BASE_URL_ARG + "/" + action;
        
        URL url = new URL("http", HOST, PORT, arg);
  
        //Invoke the URL
  
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        con.connect();
  
        // Get the content
        InputStream is = con.getInputStream();
        java.io.ByteArrayOutputStream os = new  java.io.ByteArrayOutputStream();
        byte[] buff = new byte[1024];
        for(;;) {
          int size = is.read( buff );
          if (size == -1 ) {
              break;
          }
          os.write(buff, 0, size);
        }
        os.flush();
        
        // The response codes from JMX HTTP does not seem to work.
        if (con.getResponseCode() != HttpURLConnection.HTTP_OK ) {
          throw new Exception ("Could not invoke url: " + con.getResponseMessage() );
        } else {
          System.out.println("Invoked URL: " + method + " for destination " + destName 
+ "got resonse: " + os.toString());
        }
        
        
      }
      
      /**
       * Invoke the JMSServer Mbean via the MBean server.
       *
       * @param method name of the MBean method to invoke
       * @param destName name of the destination
       */
      protected static void invokeViaMBean(String method, String destName) 
        throws Exception
     {
        // Get the MBean server
        MBeanServer server = (MBeanServer)
         MBeanServerFactory.findMBeanServer(null).iterator().next();
        
        // Invoke the MBean
        server.invoke(new ObjectName("JMS", "service", "JMSServer"),
                    method,
                    new Object[] { destName },
                    new String[] { "java.lang.String" });
     }
     
      /**
       * Simple test. Creates topic destination myCreated if invoked without argument, 
and destroys myCreated if invoked with any argument.
       */
     public static void main(String[] args) {
        try {
         if (args.length >0){
            destroyDestination(Topic.class,"myCreated");
         }else {
            createDestination(Topic.class,"myCreated");
         }
  
  
        }catch(Exception ex) {
         System.out.println("Error in administering destination: " + ex);
         ex.printStackTrace();
        }
     }
     
  } // DestinationHelper
  
  
  
  1.1                  
manual/src/examples/org/jboss/docs/jms/client/DestinationHelper25.java
  
  Index: DestinationHelper25.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  package org.jboss.docs.jms.client;
  
  import java.io.InputStream;
  
  import java.net.URL;
  import java.net.HttpURLConnection;
  
  import javax.management.MBeanServerFactory;
  import javax.management.MBeanServer;
  import javax.management.ObjectName;
  
  import javax.jms.Topic;
  import javax.jms.Queue;
  
  /**
   * Demonstration of how to administer JMS destinations programatically for 
   * JBossMQ for 2.5.x. Remember that destinations are not persisted between server 
reboots.
   *
   * To use from a remote client, adapt the HOST and PORT variable to your local
   * settings.
   *
   * Only works up to and including JBoss 2.4.x
   *
   * @author Peter Antman
   * @version $Revision: 1.1 $
   */
  
  public class DestinationHelper25  {
      /**
       * Host where JBoss is
       */
      static final String HOST = "localhost";
  
      /**
       * Port of the JMX HTML admin gui
       */
      static final int PORT = 8082;
  
  
      /**
       * Base url to invoke, MBean PATH has changed in 2.5.x
       */
      static final String BASE_URL_ARG = "/InvokeAction//JBossMQ%3Aservice%3DServer"; 
  
  
      /**
       * Create a destination. Works both if you are in the same VM as JBossMQ
       * or if you are a remote client. New method names in 2.5.x.
       *
       * @param type Topic.class or Queue.class
       * @param name name of the destination, without prefix queue/ or topic/
       */
      public static void createDestination(Class type, String name) 
        throws Exception
      {
        String method = null;
        if (type == Topic.class) {
            method = "createTopic";
        }
        else if (type == Queue.class) {
            method = "createQueue";
        }
        invoke(method, name);
        
      }
      
      /**
       * Destroy a destination. Works both if you are in the same VM as JBossMQ
       * or if you are a remote client.
       *
       * @param type Topic.class or Queue.class
       * @param name name of the destination, without prefix queue/ or topic/
       */
      public static void destroyDestination(Class type, String name) 
        throws Exception
      {
        String method = null;
        if (type == Topic.class) {
            method = "destroyTopic";
        }
        else if (type == Queue.class) {
            method = "destroyQueue";
        }
        invoke(method, name);
      }
      
      /**
       * Helper method, decides if we are in the same VM as JBossMQ, or are a
       * remote client.
       *
       * @param method name of the MBean method to invoke
       * @param destName name of the destination
       */
      protected static void invoke(String method, String destName) 
        throws Exception
      {
        // Check if we can get the MBean server
        try {
            MBeanServer server = (MBeanServer)
                MBeanServerFactory.findMBeanServer(null).iterator().next();
         
            // We are native to the MBean server, invoke directly
            invokeViaMBean(method, destName);
        }catch(Exception ex) {
          
            // We are a standalone client, invoke via URL
            invokeViaUrl(method, destName);
        }
        
      }
  
      /**
       * Invoke the JMSServer Mbean via URL.
       *
       * @param method name of the MBean method to invoke
       * @param destName name of the destination
       */
      protected static void invokeViaUrl(String method, String destName) 
        throws Exception
      {
        // Construct the correct url, see to it that HOST and PORT are correct
        String action = "action=" + method + "?action=" + method + 
            "&param0%2Bjava.lang.String=" + destName;
  
        String arg = BASE_URL_ARG + "/" + action;
        
        URL url = new URL("http", HOST, PORT, arg);
  
        //Invoke the URL
  
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        con.connect();
  
        // Get the content
        InputStream is = con.getInputStream();
        java.io.ByteArrayOutputStream os = new  java.io.ByteArrayOutputStream();
        byte[] buff = new byte[1024];
        for(;;) {
            int size = is.read( buff );
            if (size == -1 ) {
                break;
            }
            os.write(buff, 0, size);
        }
        os.flush();
        
        // The response codes from JMX HTTP does not seem to work.
        if (con.getResponseCode() != HttpURLConnection.HTTP_OK ) {
            throw new Exception ("Could not invoke url: " + con.getResponseMessage() );
        } else {
            System.out.println("Invoked URL: " + method + " for destination " + 
destName + "got resonse: " + os.toString());
        }
        
        
      }
  
      /**
       * Invoke the JMSServer Mbean via the MBean server.
       *
       * @param method name of the MBean method to invoke
       * @param destName name of the destination
       */
      protected static void invokeViaMBean(String method, String destName) 
        throws Exception
      {
        // Get the MBean server
        MBeanServer server = (MBeanServer)
            MBeanServerFactory.findMBeanServer(null).iterator().next();
        
        // Invoke the MBean
        server.invoke(new ObjectName("JBossMQ", "service", "Server"),
                      method,
                      new Object[] { destName },
                      new String[] { "java.lang.String" });
      }
  
  
     
      /**
       * Simple test. Creates topic destination myCreated if invoked without argument, 
and destroys myCreated if invoked with any argument.
       */
      public static void main(String[] args) {
        try {
            if (args.length >0){
                destroyDestination(Topic.class,"myCreated");
            }else {
                createDestination(Topic.class,"myCreated");
            }
  
  
        }catch(Exception ex) {
            System.out.println("Error in administering destination: " + ex);
            ex.printStackTrace();
        }
      }
     
  } // DestinationHelper
  
  
  
  1.1                  
manual/src/examples/org/jboss/docs/jms/client/HelloPublisher.java
  
  Index: HelloPublisher.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  
  package org.jboss.docs.jms.client;
  
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  
  import javax.jms.TopicConnectionFactory;
  import javax.jms.TopicConnection;
  import javax.jms.TopicSession;
  import javax.jms.TopicPublisher;
  import javax.jms.Topic;
  import javax.jms.TextMessage;
  import javax.jms.Session;
  import javax.jms.JMSException;
  /**
   * <p>Simple JMS client, publish text messages to testTopic Topic.
   * </p>
   *
   * <p><b>NOTE</b>This code is showcase only. It may not provide a stable production 
example.</p>
   * @author Peter Antman
   * @version $Revision: 1.1 $
   */
  
  public class HelloPublisher {
  
     /** 
      * Topic connection, hold on to this so you may close it. 
      */
     TopicConnection topicConnection;
     
     /** 
      * Topic session, hold on to this so you may close it. Also used to create 
messages. 
      */
     TopicSession topicSession;
  
     /** 
      * Use this to publish messages.
      */
     TopicPublisher topicPublisher;
  
     /** 
      * Destination to publish to 
      */
     Topic topic;
     
  
     /**
      * Sets up all the JMS fixtures.
      *
      * Use close() when finished with object.
      *
      * @param factoryJNDI name of the topic connection factory to look up.
      * @param topicJNDI name of the topic destination to look up
      */
     public HelloPublisher(String factoryJNDI, String topicJNDI) throws JMSException, 
NamingException {
        // Get the initial context
        Context context = new InitialContext();
  
        // Get the connection factory
        TopicConnectionFactory topicFactory = 
                (TopicConnectionFactory)context.lookup(factoryJNDI);
        
        // Create the connection 
        topicConnection = topicFactory.createTopicConnection();
  
        // Create the session
        topicSession = topicConnection.createTopicSession(
                                                   // No transaction
                                                   false, 
                                                   // Auto ack
                                                   Session.AUTO_ACKNOWLEDGE);
        
        // Look up the destination
        topic = (Topic)context.lookup(topicJNDI);
        
        // Create a publisher
        topicPublisher = topicSession.createPublisher(topic);
    
     }
  
     /**
      * Publish the given String as a JMS message to the testTopic topic.
      */
     public void publish(String msg) throws JMSException {
  
        // Create a message
        TextMessage message = topicSession.createTextMessage();
        message.setText(msg);
  
        // Publish the message
        topicPublisher.publish(topic, message);
     }
  
     /**
      * Close session and connection. When done, no publishing is possible any more.
      */
     public void close() throws JMSException {
        topicSession.close();
        topicConnection.close();
     }
     
     /**
      * Run an example publishing 10 messages to testTopic. Only works up to and 
including JBoss 2.4.x
      */
     public static void main(String[] args) {
        try {
         
         //Create the HelloPublisher, giving it the name of the TopicConnection
         //Factory and the Topic destination to use in lookup.
         
         HelloPublisher publisher = new HelloPublisher(
            // Name of ConnectionFactory
            "TopicConnectionFactory",
            // Name of destination to publish to
            "topic/testTopic"
            );
         
         // Publish 10 messages
         for (int i = 1; i < 11; i++) {
            String msg = "Hello World no. " + i;
            System.out.println("Publishing message: " + msg);
            publisher.publish(msg);
         }
         
         // Close down your publisher
         publisher.close();
         
        }catch(Exception ex) {
         System.err.println("An exception occured while testing HelloPublisher: " + 
ex);
         ex.printStackTrace();
        }
        
     }
     
  } // HelloPublisher
  
  
  
  
  
  
  
  
  
  
  
  1.1                  
manual/src/examples/org/jboss/docs/jms/client/HelloPublisher25.java
  
  Index: HelloPublisher25.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  package org.jboss.docs.jms.client;
  
  import javax.naming.NamingException;
  
  import javax.jms.JMSException;
  /**
   * HelloPublisher working with JBoss 2.5.x. The only difference between this
   * class and HelloPublisher is the name of the ConnectionFactory
   *
   *
   * Created: Wed Jul 25 12:47:02 2001
   *
   * @author Peter Antman
   * @version $Revision: 1.1 $
   */
  
  public class HelloPublisher25 extends HelloPublisher{
     
     public HelloPublisher25(String factoryJNDI, String topicJNDI) throws 
JMSException, NamingException {
        super(factoryJNDI,topicJNDI);
     }
     
     /**
      * Run an example publishing 10 messages to testTopic. Only works from JBoss 
2.5.x and up.
      */
     public static void main(String[] args) {
        try {
         
         //Create the HelloPublisher, giving it the name of the TopicConnection
         //Factory and the Topic destination to use in lookup.
         
         HelloPublisher25 publisher = new HelloPublisher25(
            // Name of ConnectionFactory
            "ConnectionFactory",
            // Name of destination to publish to
            "topic/testTopic"
            );
         
         // Publish 10 messages
         for (int i = 1; i < 11; i++) {
            String msg = "Hello World no. " + i;
            System.out.println("Publishing message: " + msg);
            publisher.publish(msg);
         }
         
         // Close down your publisher
         publisher.close();
         
        }catch(Exception ex) {
         System.err.println("An exception occured while testing HelloPublisher25: " + 
ex);
         ex.printStackTrace();
        }
     }
     
  } // HelloPublisher25
  
  
  
  1.1                  manual/src/examples/org/jboss/docs/jms/client/HelloReceiver.java
  
  Index: HelloReceiver.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  
  package org.jboss.docs.jms.client;
  
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  
  import javax.jms.QueueConnectionFactory;
  import javax.jms.QueueConnection;
  import javax.jms.QueueSession;
  import javax.jms.QueueReceiver;
  import javax.jms.Queue;
  import javax.jms.Message;
  import javax.jms.TextMessage;
  import javax.jms.Session;
  import javax.jms.MessageListener;
  import javax.jms.JMSException;
  
  /**
   * <p>Simple JMS client, subsribes for messages on testQueue Queue.
   * </p>
   *
   * <p><b>NOTE</b>This code is showcase only. It may not provide a stable production 
example.</p>
   *
   * @author Peter Antman
   * @version $Revision: 1.1 $
  
   */
  
  public class HelloReceiver implements MessageListener {
     
     /** 
      * Queue connection, hold on to this so you may close it. 
      */
     QueueConnection queueConnection;
     
     /** 
      * Queue session, hold on to this so you may close it. Also used to create 
messages. 
      */
     QueueSession queueSession;
  
     /** 
      * Subsrciber
      */
     QueueReceiver queueReceiver;
  
     /** 
      * Destination to subscribe to 
      */
     Queue queue;
  
     /**
      * Sets up all the JMS fixtures.
      *
      * Use close() when finished with object.
      *
      * @param factoryJNDI name of the queue connection factory to look up.
      * @param queueJNDI name of the queue destination to look up
      */
     public HelloReceiver(String factoryJNDI, String queueJNDI) throws JMSException, 
NamingException 
     {
        // Get the initial context
        Context context = new InitialContext();
  
        // Get the connection factory
        QueueConnectionFactory queueFactory = 
         (QueueConnectionFactory)context.lookup(factoryJNDI);
        
        // Create the connection 
        queueConnection = queueFactory.createQueueConnection();
  
        // Create the session
        queueSession = queueConnection.createQueueSession(
         // No transaction
         false, 
         // Auto ack
         Session.AUTO_ACKNOWLEDGE);
        
        // Look up the destination
        queue = (Queue)context.lookup(queueJNDI);
        
        // Create a subsriber
        queueReceiver = queueSession.createReceiver(queue);
  
        // Set the message listener, which is this class since we implement
        // the MessageListener interface
        queueReceiver.setMessageListener(this);
        
        System.out.println("HelloReceiver subscribed to queue: " + queueJNDI);
        // OBS! For the message listener to receive any messages the connection
        // has to be started.
        queueConnection.start();
     }
      
     /**
      * Implementation of the MessageListener interface, messages will be received 
through this method.
      */
     public void onMessage(Message m) {
        // Unpack the message, be carefull when casting to correct message type
        // onMessage should not through any application exceptions.
        try {
         String msg = ((TextMessage)m).getText();
         System.out.println("HelloReceiver got message: " + msg);
        }catch(JMSException ex) {
         System.err.println("Could not get text message: " + ex);
         ex.printStackTrace();
        }
     }
      
     /**
      * Close session and connection.
      */
     public void close() throws JMSException {
        queueSession.close();
        queueConnection.close();
     }
     
     /**
      * Run an example subscribing to queue testQueue. Only works up to and including 
JBoss 2.4.x
      */
     public static void main(String[] args) {
        try {
         
         //Create the HelloSubsriber, giving it the name of the QueueConnection
         //Factory and the Queue destination to use in lookup.
         
         HelloReceiver receiver = new HelloReceiver(
            // Name of ConnectionFactory
            "QueueConnectionFactory",
            // Name of destination to publish to
            "queue/testQueue"
            );
         
        }catch(Exception ex) {
         System.err.println("An exception occured while testing HelloReceiver: " + ex);
         ex.printStackTrace();
        }
     }
     
  } // HelloReceiver
  
  
  
  1.1                  
manual/src/examples/org/jboss/docs/jms/client/HelloReceiver25.java
  
  Index: HelloReceiver25.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  package org.jboss.docs.jms.client;
  
  import javax.naming.NamingException;
  
  import javax.jms.JMSException;
  /**
   * HelloReceiver working with JBoss 2.5.x. The only difference between this
   * class and HelloSender is the name of the ConnectionFactory
   *
   *
   * Created: Wed Jul 25 12:38:08 2001
   *
   * @author Peter Antman
   * @version $Revision: 1.1 $
   */
  
  public class HelloReceiver25 extends HelloReceiver{
     
     public HelloReceiver25(String factoryJNDI, String queueJNDI) throws JMSException, 
NamingException {
        super(factoryJNDI,queueJNDI);
     }
     
     /**
      * Run an example subscribing to queue testQueue. Only works from JBoss 2.5.x and 
up.
      */
     public static void main(String[] args) {
        try {
         
         //Create the HelloSubsriber, giving it the name of the QueueConnection
         //Factory and the Queue destination to use in lookup.
         
         HelloReceiver25 receiver = new HelloReceiver25(
            // Name of ConnectionFactory
            "ConnectionFactory",
            // Name of destination to publish to
            "queue/testQueue"
            );
         
        }catch(Exception ex) {
         System.err.println("An exception occured while testing HelloReceiver: " + ex);
         ex.printStackTrace();
        }
     }
     
  } // HelloReceiver25
  
  
  
  1.1                  manual/src/examples/org/jboss/docs/jms/client/HelloSender.java
  
  Index: HelloSender.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  
  package org.jboss.docs.jms.client;
  
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  
  import javax.jms.QueueConnectionFactory;
  import javax.jms.QueueConnection;
  import javax.jms.QueueSession;
  import javax.jms.QueueSender;
  import javax.jms.Queue;
  import javax.jms.TextMessage;
  import javax.jms.Session;
  import javax.jms.JMSException;
  /**
   * <p>Simple JMS client, send text messages to testQueue Queue.
   * </p>
   *
   * <p><b>NOTE</b>This code is showcase only. It may not provide a stable production 
example.</p>
   * @author Peter Antman
   * @version $Revision: 1.1 $
   */
  
  public class HelloSender {
  
     /** 
      * Queue connection, hold on to this so you may close it. 
      */
     QueueConnection queueConnection;
     
     /** 
      * Queue session, hold on to this so you may close it. Also used to create 
messages. 
      */
     QueueSession queueSession;
  
     /** 
      * Use this to send messages.
      */
     QueueSender queueSender;
  
     /** 
      * Destination to send to 
      */
     Queue queue;
     
  
     /**
      * Sets up all the JMS fixtures.
      *
      * Use close() when finished with object.
      *
      * @param factoryJNDI name of the queue connection factory to look up.
      * @param queueJNDI name of the queue destination to look up
      */
     public HelloSender(String factoryJNDI, String queueJNDI) throws JMSException, 
NamingException {
        // Get the initial context
        Context context = new InitialContext();
  
        // Get the connection factory
        QueueConnectionFactory queueFactory = 
                (QueueConnectionFactory)context.lookup(factoryJNDI);
        
        // Create the connection 
        queueConnection = queueFactory.createQueueConnection();
  
        // Create the session
        queueSession = queueConnection.createQueueSession(
                                                   // No transaction
                                                   false, 
                                                   // Auto ack
                                                   Session.AUTO_ACKNOWLEDGE);
        
        // Look up the destination
        queue = (Queue)context.lookup(queueJNDI);
        
        // Create a sender
        queueSender = queueSession.createSender(queue);
    
     }
  
     /**
      * Send the given String as a JMS message to the testQueue queue.
      */
     public void send(String msg) throws JMSException {
  
        // Create a message
        TextMessage message = queueSession.createTextMessage();
        message.setText(msg);
  
        // Send the message
        queueSender.send(queue, message);
     }
  
     /**
      * Close session and connection. When done, no sending is possible any more.
      */
     public void close() throws JMSException {
        queueSession.close();
        queueConnection.close();
     }
     
     /**
      * Run an example sending 10 messages to testQueue. Only works up to and 
including JBoss 2.4.x
      */
     public static void main(String[] args) {
        try {
         
         //Create the HelloSender, giving it the name of the QueueConnection
         //Factory and the Queue destination to use in lookup.
         
         HelloSender sender = new HelloSender(
            // Name of ConnectionFactory
            "QueueConnectionFactory",
            // Name of destination to send to
            "queue/testQueue"
            );
         
         // Send 10 messages
         for (int i = 1; i < 11; i++) {
            String msg = "Hello World no. " + i;
            System.out.println("Sending message: " + msg);
            sender.send(msg);
         }
         
         // Close down your sender
         sender.close();
         
        }catch(Exception ex) {
         System.err.println("An exception occured while testing HelloSender: " + ex);
         ex.printStackTrace();
        }
        
     }
     
  } // HelloSender
  
  
  
  
  
  
  
  
  
  
  
  1.1                  manual/src/examples/org/jboss/docs/jms/client/HelloSender25.java
  
  Index: HelloSender25.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  package org.jboss.docs.jms.client;
  
  import javax.naming.NamingException;
  
  import javax.jms.JMSException;
  /**
   * HelloSender working with JBoss 2.5.x. The only difference between this
   * class and HelloSender is the name of the ConnectionFactory
   *
   *
   * Created: Wed Jul 25 12:47:02 2001
   *
   * @author Peter Antman
   * @version $Revision: 1.1 $
   */
  
  public class HelloSender25 extends HelloSender{
     
     public HelloSender25(String factoryJNDI, String queueJNDI) throws JMSException, 
NamingException {
        super(factoryJNDI,queueJNDI);
     }
     
     /**
      * Run an example publishing 10 messages to testQueue. Only works from JBoss 
2.5.x and up.
      */
     public static void main(String[] args) {
        try {
         
         //Create the HelloSender, giving it the name of the QueueConnection
         //Factory and the Queue destination to use in lookup.
         
         HelloSender25 sender = new HelloSender25(
            // Name of ConnectionFactory
            "ConnectionFactory",
            // Name of destination to publish to
            "queue/testQueue"
            );
         
         // Publish 10 messages
         for (int i = 1; i < 11; i++) {
            String msg = "Hello World no. " + i;
            System.out.println("Sending message: " + msg);
            sender.send(msg);
         }
         
         // Close down your sender
         sender.close();
         
        }catch(Exception ex) {
         System.err.println("An exception occured while testing HelloSender25: " + ex);
         ex.printStackTrace();
        }
     }
     
  } // HelloSender25
  
  
  
  1.1                  
manual/src/examples/org/jboss/docs/jms/client/HelloSubscriber.java
  
  Index: HelloSubscriber.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  
  package org.jboss.docs.jms.client;
  
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  
  import javax.jms.TopicConnectionFactory;
  import javax.jms.TopicConnection;
  import javax.jms.TopicSession;
  import javax.jms.TopicSubscriber;
  import javax.jms.Topic;
  import javax.jms.Message;
  import javax.jms.TextMessage;
  import javax.jms.Session;
  import javax.jms.MessageListener;
  import javax.jms.JMSException;
  
  /**
   * <p>Simple JMS client, subsribes for messages on testTopic Topic.
   * </p>
   *
   * <p><b>NOTE</b>This code is showcase only. It may not provide a stable production 
example.</p>
   *
   * @author Peter Antman
   * @version $Revision: 1.1 $
  
   */
  
  public class HelloSubscriber implements MessageListener {
     
     /** 
      * Topic connection, hold on to this so you may close it. 
      */
     TopicConnection topicConnection;
     
     /** 
      * Topic session, hold on to this so you may close it. Also used to create 
messages. 
      */
     TopicSession topicSession;
  
     /** 
      * Subsrciber
      */
     TopicSubscriber topicSubscriber;
  
     /** 
      * Destination to subscribe to 
      */
     Topic topic;
  
     /**
      * Sets up all the JMS fixtures.
      *
      * Use close() when finished with object.
      *
      * @param factoryJNDI name of the topic connection factory to look up.
      * @param topicJNDI name of the topic destination to look up
      */
     public HelloSubscriber(String factoryJNDI, String topicJNDI) throws JMSException, 
NamingException 
     {
        // Get the initial context
        Context context = new InitialContext();
  
        // Get the connection factory
        TopicConnectionFactory topicFactory = 
         (TopicConnectionFactory)context.lookup(factoryJNDI);
        
        // Create the connection 
        topicConnection = topicFactory.createTopicConnection();
  
        // Create the session
        topicSession = topicConnection.createTopicSession(
         // No transaction
         false, 
         // Auto ack
         Session.AUTO_ACKNOWLEDGE);
        
        // Look up the destination
        topic = (Topic)context.lookup(topicJNDI);
        
        // Create a subsriber
        topicSubscriber = topicSession.createSubscriber(topic);
  
        // Set the message listener, which is this class since we implement
        // the MessageListener interface
        topicSubscriber.setMessageListener(this);
        
        System.out.println("HelloSubscriber subscribed to topic: " + topicJNDI);
        // OBS! For the message listener to receive any messages the connection
        // has to be started
        topicConnection.start();
     }
      
     /**
      * Implementation of the MessageListener interface, messages will be received 
through this method.
      */
     public void onMessage(Message m) {
        // Unpack the message, be carefull when casting to correct message type
        // onMessage should not through any application exceptions.
        try {
         String msg = ((TextMessage)m).getText();
         System.out.println("HelloSubscriber got message: " + msg);
        }catch(JMSException ex) {
         System.err.println("Could not get text message: " + ex);
         ex.printStackTrace();
        }
     }
      
     /**
      * Close session and connection.
      */
     public void close() throws JMSException {
        topicSession.close();
        topicConnection.close();
     }
     
     /**
      * Run an example subscribing to topic testTopic. Only works up to and including 
JBoss 2.4.x
      */
     public static void main(String[] args) {
        try {
         
         //Create the HelloSubsriber, giving it the name of the TopicConnection
         //Factory and the Topic destination to use in lookup.
         
         HelloSubscriber subscriber = new HelloSubscriber(
            // Name of ConnectionFactory
            "TopicConnectionFactory",
            // Name of destination to publish to
            "topic/testTopic"
            );
         
        }catch(Exception ex) {
         System.err.println("An exception occured while testing HelloPublisher: " + 
ex);
         ex.printStackTrace();
        }
     }
     
  } // HelloSubscriber
  
  
  
  1.1                  
manual/src/examples/org/jboss/docs/jms/client/HelloSubscriber25.java
  
  Index: HelloSubscriber25.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  package org.jboss.docs.jms.client;
  
  import javax.naming.NamingException;
  
  import javax.jms.JMSException;
  /**
   * HelloSubscriber working with JBoss 2.5.x. The only difference between this
   * class and HelloSubsriber is the name of the ConnectionFactory
   *
   *
   * Created: Wed Jul 25 12:38:08 2001
   *
   * @author Peter Antman
   * @version $Revision: 1.1 $
   */
  
  public class HelloSubscriber25 extends HelloSubscriber{
     
     public HelloSubscriber25(String factoryJNDI, String topicJNDI) throws 
JMSException, NamingException {
        super(factoryJNDI,topicJNDI);
     }
     
     /**
      * Run an example subscribing to topic testTopic. Only works from JBoss 2.5.x and 
up.
      */
     public static void main(String[] args) {
        try {
         
         //Create the HelloSubsriber, giving it the name of the TopicConnection
         //Factory and the Topic destination to use in lookup.
         
         HelloSubscriber25 subscriber = new HelloSubscriber25(
            // Name of ConnectionFactory
            "ConnectionFactory",
            // Name of destination to publish to
            "topic/testTopic"
            );
         
        }catch(Exception ex) {
         System.err.println("An exception occured while testing HelloPublisher: " + 
ex);
         ex.printStackTrace();
        }
     }
     
  } // HelloSubscriber25
  
  
  
  1.1                  manual/src/examples/org/jboss/docs/jms/client/ManualJNDI.java
  
  Index: ManualJNDI.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  
  package org.jboss.docs.jms.client;
  
  import java.util.Hashtable;
  
  import javax.naming.InitialContext;
  import javax.naming.Context;
  import javax.naming.NamingException;
  
  import javax.jms.Topic;
  /**
   * Simple example how to manually get the InitialContext.
   *
   * May be used when jndi.properties is not available or when the default
   * context is not apropriate
   * @author Peter Antman
   * @version $Revision: 1.1 $
   */
  
  public class ManualJNDI {
  
     /**
      * Get an initial context for JBoss JNDI (localhost:1099)
      */
     public static Context getInitialContext() throws NamingException{
        
        // Populate with needed propertis
        Hashtable props = new Hashtable();
        props.put(Context.INITIAL_CONTEXT_FACTORY,
                "org.jnp.interfaces.NamingContextFactory");
        props.put(Context.PROVIDER_URL, "localhost:1099");
        props.put("java.naming.rmi.security.manager", "yes");
        props.put(Context.URL_PKG_PREFIXES,  "org.jboss.naming");
        
        // Get initial context with given properties
        return new InitialContext(props);
        
     } 
     
     /**
      * Test looking up a known topic, if no errors JNDI works.
      */
     public static void main(String[] args) {
        try {
         // Get the manually configured initial context
         Context context = getInitialContext();
         
         // Do the lookup
         Topic t = (Topic)context.lookup("topic/testTopic");
         System.out.println("Looing up topic/testTopic worked fine");
        }catch (Exception ex) {
         System.err.println("Could not lookup topic/testTopic");
         ex.printStackTrace();
        }
     }
     
  } // ManualJNDI
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to