[jboss-cvs] jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/jms/ex1 ...

Norman Richards norman.richards at jboss.com
Wed Nov 1 13:14:14 EST 2006


  User: nrichards
  Date: 06/11/01 13:14:14

  Added:       jbossas/j2ee/examples/src/main/org/jboss/book/jms/ex1        
                        DurableTopicClient.java DurableTopicRecvClient.java
                        DurableTopicSetup.java SendRecvClient.java
                        TopicRecvClient.java TopicSendClient.java
                        TopicSendRecvClient.java log4j.xml
  Log:
  modified for j2ee guide
  
  Revision  Changes    Path
  1.1      date: 2006/11/01 18:14:14;  author: nrichards;  state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/jms/ex1/DurableTopicClient.java
  
  Index: DurableTopicClient.java
  ===================================================================
  package org.jboss.book.jms.ex1;
  
  import javax.jms.JMSException;
  import javax.jms.Message;
  import javax.jms.MessageListener;
  import javax.jms.Topic;
  import javax.jms.TopicConnection;
  import javax.jms.TopicConnectionFactory;
  import javax.jms.TopicPublisher;
  import javax.jms.TopicSubscriber;
  import javax.jms.TopicSession;
  import javax.jms.TextMessage;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  
  import EDU.oswego.cs.dl.util.concurrent.CountDown;
  
  /** A complete JMS client example program that sends a
  TextMessage to a Queue and asynchronously receives the
  message from the same Queue.
  
  @author  Scott.Stark at jboss.org
  @version $Revision: 1.1 $
  */
  public class DurableTopicClient
  {
     static CountDown done = new CountDown(1);
     TopicConnection  conn = null;
     TopicSession session = null;
     Topic topic = null;
  
     public static class ExListener implements MessageListener
     {
        public void onMessage(Message msg)
        {
           done.release();
           TextMessage tm = (TextMessage) msg;
           try
           {
              System.out.println("onMessage, recv text="
                 + tm.getText());
           }
           catch(Throwable t)
           {
              t.printStackTrace();
           }
        }
     }
  
     public void setupPubSub()
        throws JMSException, NamingException
     {
        InitialContext iniCtx = new InitialContext();
        Object tmp = iniCtx.lookup("TopicConnectionFactory");
        TopicConnectionFactory tcf = (TopicConnectionFactory) tmp;
        conn = tcf.createTopicConnection("jduke", "theduke");
        conn.setClientID("OtherSubscriptions");
        topic = (Topic) iniCtx.lookup("topic/testTopic");
        session = conn.createTopicSession(false,
           TopicSession.AUTO_ACKNOWLEDGE);
        conn.start();
     }
  
     public void sendRecvAsync(String text)
        throws JMSException, NamingException
     {
        System.out.println("Begin sendRecvAsync");
        // Setup the pub/sub connection, session
        setupPubSub();
        // Set the async listener
        TopicSubscriber recv = session.createDurableSubscriber(topic, "AnotherExample");
        recv.setMessageListener(new ExListener());
        // Send a text msg
        TopicPublisher send = session.createPublisher(topic);
        TextMessage tm = session.createTextMessage(text);
        send.publish(tm);
        System.out.println("sendRecvAsync, sent text="
           + tm.getText());
        send.close();
        System.out.println("End sendRecvAsync");
     }
  
     public void stop()  throws JMSException
     {
        conn.stop();
        session.close();
        conn.close();
     }
  
     public static void main(String args[]) throws Exception
     {
        DurableTopicClient client = new DurableTopicClient();
        client.sendRecvAsync("A text msg");
        client.done.acquire();
        client.stop();
        System.exit(0);
     }
  
  }
  
  
  
  1.1      date: 2006/11/01 18:14:14;  author: nrichards;  state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/jms/ex1/DurableTopicRecvClient.java
  
  Index: DurableTopicRecvClient.java
  ===================================================================
  package org.jboss.book.jms.ex1;
  
  import javax.jms.JMSException;
  import javax.jms.Message;
  import javax.jms.MessageListener;
  import javax.jms.Topic;
  import javax.jms.TopicConnection;
  import javax.jms.TopicConnectionFactory;
  import javax.jms.TopicPublisher;
  import javax.jms.TopicSubscriber;
  import javax.jms.TopicSession;
  import javax.jms.TextMessage;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  
  /**
   * A JMS client example program that synchronously receives a message a Topic
   *
   * @author  Scott.Stark at jboss.org
   * @version $Revision: 1.1 $
   */
  public class DurableTopicRecvClient
  {
      TopicConnection  conn = null;
      TopicSession session = null;
      Topic topic = null;
      
      public void setupPubSub()
          throws JMSException, 
                 NamingException
      {
          InitialContext iniCtx = new InitialContext();
          Object tmp = iniCtx.lookup("ConnectionFactory");
          TopicConnectionFactory tcf = (TopicConnectionFactory) tmp;
          conn = tcf.createTopicConnection("john", "needle");
          topic = (Topic) iniCtx.lookup("topic/testTopic");
          session = conn.createTopicSession(false,
                                            TopicSession.AUTO_ACKNOWLEDGE);
          conn.start();
      }
      
      public void recvSync()
          throws JMSException, 
                 NamingException
      {
          System.out.println("Begin recvSync");
          // Setup the pub/sub connection, session
  
          setupPubSub();
          // Wait upto 5 seconds for the message
          TopicSubscriber recv = 
              session.createDurableSubscriber(topic, "jms-ex1dtps");
          Message msg = recv.receive(5000);
          if (msg == null) {
              System.out.println("Timed out waiting for msg");
          } else {
              System.out.println("DurableTopicRecvClient.recv, msgt=" + msg);
          }
      }
      
      public void stop()
          throws JMSException
      {
          conn.stop();
          session.close();
          conn.close();
      }
      
      public static void main(String args[])
          throws Exception
      {
          System.out.println("Begin DurableTopicRecvClient, now=" + 
                             System.currentTimeMillis());
          DurableTopicRecvClient client = new DurableTopicRecvClient();
          client.recvSync();
          client.stop();
          System.out.println("End DurableTopicRecvClient");
          System.exit(0);
      }
  }
  
  
  
  1.1      date: 2006/11/01 18:14:14;  author: nrichards;  state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/jms/ex1/DurableTopicSetup.java
  
  Index: DurableTopicSetup.java
  ===================================================================
  package org.jboss.book.jms.ex1;
  
  import javax.jms.JMSException;
  import javax.jms.Topic;
  import javax.jms.TopicConnection;
  import javax.jms.TopicConnectionFactory;
  import javax.jms.TopicSubscriber;
  import javax.jms.TopicSession;
  import javax.naming.InitialContext;
  
  /** A client that creates a durable topic subscriber and exits simply to
  create the durable topic using the JBossMQ dynamic durable topic capability.
  
  
  @author  Scott.Stark at jboss.org
  @version $Revision: 1.1 $
  */
  public class DurableTopicSetup
  {
  
     public static void main(String args[]) throws Exception
     {
        System.out.println("Begin DurableTopicSetup");
        InitialContext iniCtx = new InitialContext();
  
        Object tmp = iniCtx.lookup("ConnectionFactory");
        TopicConnectionFactory tcf = (TopicConnectionFactory) tmp;
        TopicConnection conn = tcf.createTopicConnection("john", "needle");
        Topic topic = (Topic) iniCtx.lookup("topic/testTopic");
        TopicSession session = conn.createTopicSession(false,
                                                       TopicSession.AUTO_ACKNOWLEDGE);
  
        TopicSubscriber recv = session.createDurableSubscriber(topic, "jms-ex1dtps");
        session.createSubscriber(topic);
        
        conn.close();
        System.out.println("End DurableTopicSetup");
        System.exit(0);
     }
  
  }
  
  
  
  1.1      date: 2006/11/01 18:14:14;  author: nrichards;  state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/jms/ex1/SendRecvClient.java
  
  Index: SendRecvClient.java
  ===================================================================
  package org.jboss.book.jms.ex1; 
  import javax.jms.JMSException; 
  import javax.jms.Message; 
  import javax.jms.MessageListener;
   
  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.TextMessage; 
  import javax.naming.InitialContext; 
  import javax.naming.NamingException; 
  import EDU.oswego.cs.dl.util.concurrent.CountDown; 
  import org.apache.log4j.Logger; 
  import org.jboss.util.ChapterExRepository;
  
   /**
    * A complete JMS client example program that sends a 
    * TextMessage to a Queue and asynchronously receives the 
    * message from the same Queue. 
    * 
    * @author Scott.Stark at jboss.org 
    * @version $Revision: 1.1 $ 
    */ 
  
  public class SendRecvClient { 
      static Logger log;
      static CountDown done = new CountDown(1);
  
      QueueConnection conn;
      QueueSession session;
      Queue que;
  
      public static class ExListener 
          implements MessageListener 
      { 
          public void onMessage(Message msg) { 
              done.release();
              TextMessage tm = (TextMessage) msg;
              try { 
                  log.info("onMessage, recv text=" + tm.getText());
              } catch(Throwable t) { 
                  t.printStackTrace();
              }
          } 
      }
  
      public void setupPTP() 
          throws JMSException, 
                 NamingException 
      { 
          InitialContext iniCtx = new InitialContext();
   
          Object tmp = iniCtx.lookup("ConnectionFactory");
          QueueConnectionFactory qcf = (QueueConnectionFactory) tmp;
          conn = qcf.createQueueConnection();
          que = (Queue) iniCtx.lookup("queue/testQueue");
          session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
          conn.start();
      } 
  
      public void sendRecvAsync(String text) 
          throws JMSException, 
                 NamingException 
      { 
          log.info("Begin sendRecvAsync");
          // Setup the PTP connection, session 
          
          setupPTP();
          
          // Set the async listener 
          QueueReceiver recv = session.createReceiver(que);
          
          recv.setMessageListener(new ExListener());
          
          // Send a text msg 
          QueueSender send = session.createSender(que);
          
          TextMessage tm = session.createTextMessage(text);
          send.send(tm);
          log.info("sendRecvAsync, sent text=" + tm.getText());
          send.close();
          log.info("End sendRecvAsync");
      } 
  
      public void stop() 
          throws JMSException 
      { 
          conn.stop();
          session.close();
          conn.close();
      } 
  
      public static void main(String args[]) 
          throws Exception 
      { 
          ChapterExRepository.init(SendRecvClient.class);
          log = Logger.getLogger("SendRecvClient");
          log.info("Begin SendRecvClient, now=" + System.currentTimeMillis());
          SendRecvClient client = new SendRecvClient();
          client.sendRecvAsync("A text msg");
          client.done.acquire();
          client.stop();
          log.info("End SendRecvClient");
          System.exit(0);
      }
  } 
  
  
  
  1.1      date: 2006/11/01 18:14:14;  author: nrichards;  state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/jms/ex1/TopicRecvClient.java
  
  Index: TopicRecvClient.java
  ===================================================================
  package org.jboss.book.jms.ex1;
  
  import javax.jms.JMSException;
  import javax.jms.Message;
  import javax.jms.MessageListener;
  import javax.jms.Topic;
  import javax.jms.TopicConnection;
  import javax.jms.TopicConnectionFactory;
  import javax.jms.TopicPublisher;
  import javax.jms.TopicSubscriber;
  import javax.jms.TopicSession;
  import javax.jms.TextMessage;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  
  /** 
   * A JMS client example program that synchronously receives a message a Topic
   *
   * @author  Scott.Stark at jboss.org
   * @version $Revision: 1.1 $
   */
  public class TopicRecvClient
  {
      TopicConnection  conn = null;
      TopicSession session = null;
      Topic topic = null;
      
      public void setupPubSub()
          throws JMSException, 
                 NamingException
      {
          InitialContext iniCtx = new InitialContext();
          Object tmp = iniCtx.lookup("ConnectionFactory");
          TopicConnectionFactory tcf = (TopicConnectionFactory) tmp;
          conn = tcf.createTopicConnection();
          topic = (Topic) iniCtx.lookup("topic/testTopic");
          session = conn.createTopicSession(false,
                                            TopicSession.AUTO_ACKNOWLEDGE);
          conn.start();
      }
      
      public void recvSync()
          throws JMSException, 
                 NamingException
      {
          System.out.println("Begin recvSync");
          // Setup the pub/sub connection, session
          setupPubSub();
          // Wait upto 5 seconds for the message
          TopicSubscriber recv = session.createSubscriber(topic);
          Message msg = recv.receive(5000);
          if (msg == null) {
              System.out.println("Timed out waiting for msg");
          } else {
              System.out.println("TopicSubscriber.recv, msgt=" + msg);
          }
      }
  
      public void stop() 
          throws JMSException
      {
          conn.stop();
          session.close();
          conn.close();
      }
      
      public static void main(String args[]) 
          throws Exception
      {
          System.out.println("Begin TopicRecvClient, now=" + 
                             System.currentTimeMillis());
          TopicRecvClient client = new TopicRecvClient();
          client.recvSync();
          client.stop();
          System.out.println("End TopicRecvClient");
          System.exit(0);
      }
      
  }
  
  
  
  1.1      date: 2006/11/01 18:14:14;  author: nrichards;  state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/jms/ex1/TopicSendClient.java
  
  Index: TopicSendClient.java
  ===================================================================
  package org.jboss.book.jms.ex1;
  
  import javax.jms.JMSException;
  import javax.jms.Message;
  import javax.jms.MessageListener;
  import javax.jms.Topic;
  import javax.jms.TopicConnection;
  import javax.jms.TopicConnectionFactory;
  import javax.jms.TopicPublisher;
  import javax.jms.TopicSubscriber;
  import javax.jms.TopicSession;
  import javax.jms.TextMessage;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  
  /** A JMS client example program that sends a TextMessage to a Topic
   *
   * @author  Scott.Stark at jboss.org
   * @version $Revision: 1.1 $
   */
  public class TopicSendClient
  {
      TopicConnection  conn = null;
      TopicSession session = null;
      Topic topic = null;
      
      public void setupPubSub()
          throws JMSException, 
                 NamingException
      {
          InitialContext iniCtx = new InitialContext();
          Object tmp = iniCtx.lookup("ConnectionFactory");
          TopicConnectionFactory tcf = (TopicConnectionFactory) tmp;
          conn = tcf.createTopicConnection();
          topic = (Topic) iniCtx.lookup("topic/testTopic");
          session = conn.createTopicSession(false,
                                            TopicSession.AUTO_ACKNOWLEDGE);
          conn.start();
      }
      
      public void sendAsync(String text)
          throws JMSException, 
                 NamingException
      {
          System.out.println("Begin sendAsync");
          // Setup the pub/sub connection, session
          setupPubSub();
  
          // Send a text msg
          TopicPublisher send = session.createPublisher(topic);
          TextMessage tm = session.createTextMessage(text);
          send.publish(tm);
          System.out.println("sendAsync, sent text=" + tm.getText());
          send.close();
          System.out.println("End sendAsync");
      }
      
      public void stop() 
          throws JMSException
      {
          conn.stop();
          session.close();
          conn.close();
      }
      
      public static void main(String args[]) throws Exception
      {
          System.out.println("Begin TopicSendClient, now="+System.currentTimeMillis());
  
          TopicSendClient client = new TopicSendClient();
          client.sendAsync("A text msg, now="+System.currentTimeMillis());
          client.stop();
          System.out.println("End TopicSendClient");
          System.exit(0);
      }
  }
  
  
  
  1.1      date: 2006/11/01 18:14:14;  author: nrichards;  state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/jms/ex1/TopicSendRecvClient.java
  
  Index: TopicSendRecvClient.java
  ===================================================================
  package org.jboss.book.jms.ex1;
  
  import javax.jms.JMSException;
  import javax.jms.Message;
  import javax.jms.MessageListener;
  import javax.jms.Topic;
  import javax.jms.TopicConnection;
  import javax.jms.TopicConnectionFactory;
  import javax.jms.TopicPublisher;
  import javax.jms.TopicSubscriber;
  import javax.jms.TopicSession;
  import javax.jms.TextMessage;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  
  import EDU.oswego.cs.dl.util.concurrent.CountDown;
  
  /** A complete JMS client example program that sends a
  TextMessage to a Topic and asynchronously receives the
  message from the same Topic.
  
  @author  Scott.Stark at jboss.org
  @version $Revision: 1.1 $
  */
  public class TopicSendRecvClient
  {
      static CountDown done = new CountDown(1);
      TopicConnection  conn = null;
      TopicSession session = null;
      Topic topic = null;
      
      public static class ExListener 
          implements MessageListener
      {
          public void onMessage(Message msg)
          {
              done.release();
              TextMessage tm = (TextMessage) msg;
              try {
                  System.out.println("onMessage, recv text=" + tm.getText());
              }  catch(Throwable t) {
                  t.printStackTrace();
              }
          }
      }
      
      public void setupPubSub()
          throws JMSException, 
                 NamingException
      {
          InitialContext iniCtx = new InitialContext();
          Object tmp = iniCtx.lookup("ConnectionFactory");
          TopicConnectionFactory tcf = (TopicConnectionFactory) tmp;
          conn = tcf.createTopicConnection();
          topic = (Topic) iniCtx.lookup("topic/testTopic");
          session = conn.createTopicSession(false,
                                            TopicSession.AUTO_ACKNOWLEDGE);
          conn.start();
      }
      
      public void sendRecvAsync(String text)
          throws JMSException, 
                 NamingException
      {
          System.out.println("Begin sendRecvAsync");
          // Setup the PubSub connection, session
          setupPubSub();
  
          // Set the async listener
          TopicSubscriber recv = session.createSubscriber(topic);
          recv.setMessageListener(new ExListener());
          // Send a text msg
          TopicPublisher send = session.createPublisher(topic);
          TextMessage tm = session.createTextMessage(text);
          send.publish(tm);
          System.out.println("sendRecvAsync, sent text=" + tm.getText());
          send.close();
          System.out.println("End sendRecvAsync");
      }
      
      public void stop()
          throws JMSException
      {
          conn.stop();
          session.close();
          conn.close();
      }
  
      public static void main(String args[]) throws Exception
      {
          System.out.println("Begin TopicSendRecvClient, now=" + 
                             System.currentTimeMillis());
  
          TopicSendRecvClient client = new TopicSendRecvClient();
          client.sendRecvAsync("A text msg, now="+System.currentTimeMillis());
          client.done.acquire();
          client.stop();
  
          System.out.println("End TopicSendRecvClient");
          System.exit(0);
      }
  
  }
  
  
  
  1.1      date: 2006/11/01 18:14:14;  author: nrichards;  state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/jms/ex1/log4j.xml
  
  Index: log4j.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8" ?>
  <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
  
  <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
  
     <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out"/>
        <param name="Threshold" value="INFO"/>
  
        <layout class="org.apache.log4j.PatternLayout">
           <param name="ConversionPattern" value="[%p,%c{1}] %m%n"/>
        </layout>
     </appender>
  
     <appender name="FILE" class="org.apache.log4j.FileAppender">
        <param name="File" value="logs/${chapter.ex}.log"/>
        <param name="Append" value="false"/>
  
        <layout class="org.apache.log4j.PatternLayout">
           <param name="ConversionPattern" value="[%p,%c{1}] %m%n"/>
        </layout>
     </appender>
  
     <category name="org.jboss.mq">
        <priority value="TRACE" class="org.jboss.logging.XLevel"/>
     </category>
  
     <root>
        <level value="DEBUG"/>
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE"/>
     </root>
  </log4j:configuration>
  
  
  



More information about the jboss-cvs-commits mailing list