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

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/ex3    
                        SendRecvClient.java TextMDB.java ejb-jar.xml
                        jboss.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/ex3/SendRecvClient.java
  
  Index: SendRecvClient.java
  ===================================================================
  package org.jboss.book.jms.ex3;
  
  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 N
  TextMessages to a Topic A and asynchronously receives the
  messages as modified by TestMDB from Topic B.
  
  @author  Scott.Stark at jboss.org
  @version $Revision: 1.1 $
  */
  public class SendRecvClient
  {
     static final int N = 10;
     static CountDown done = new CountDown(N);
     TopicConnection conn;
     TopicSession session;
     Topic topicA;
     Topic topicB;
  
     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 qcf = (TopicConnectionFactory) tmp;
        conn = qcf.createTopicConnection();
        topicA = (Topic) iniCtx.lookup("topic/jms.ex3.TopicA");
        topicB = (Topic) iniCtx.lookup("topic/jms.ex3.TopicB");
  
        session = conn.createTopicSession(false,
           TopicSession.AUTO_ACKNOWLEDGE);
        conn.start();
     }
  
     public void sendRecvAsync(String textBase)
        throws JMSException, NamingException, InterruptedException
     {
        System.out.println("Begin sendRecvAsync");
        // Setup the PTP connection, session
        setupPubSub();
        // Set the async listener for topicA
        TopicSubscriber recv = session.createSubscriber(topicA);
        recv.setMessageListener(new ExListener());
        // Send a few text msgs to topicB
        TopicPublisher send = session.createPublisher(topicB);
        for(int m = 0; m < 10; m ++)
        {
           TextMessage tm = session.createTextMessage(textBase+"#"+m);
           tm.setJMSReplyTo(topicA);
           send.publish(tm);
           System.out.println("sendRecvAsync, sent text="+tm.getText());
        }
        System.out.println("End sendRecvAsync");
     }
  
     public void stop()  throws JMSException
     {
        conn.stop();
     }
  
     public static void main(String args[]) throws Exception
     {
        SendRecvClient client = new SendRecvClient();
        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/ex3/TextMDB.java
  
  Index: TextMDB.java
  ===================================================================
  package org.jboss.book.jms.ex3;
  
  import javax.ejb.MessageDrivenBean;
  import javax.ejb.MessageDrivenContext;
  import javax.ejb.EJBException;
  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.TopicSession;
  import javax.jms.TextMessage;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  
  import org.apache.log4j.Logger;
  
  /** An MDB that transforms the TextMessages it receives and send the transformed
   messages to the Topic found in the incoming message JMSReplyTo header.
  
   @author  Scott.Stark at jboss.org
   @version $Revision: 1.1 $
   */
  public class TextMDB implements MessageDrivenBean, MessageListener
  {
     private static Logger log = Logger.getLogger(TextMDB.class);
  
     private MessageDrivenContext ctx = null;
     private TopicConnection conn;
     private TopicSession session;
  
     public TextMDB()
     {
        log.info("TextMDB.ctor, this="+hashCode());
        log.debug("ctor.StackTrace", new Throwable("ctor"));
     }
  
     public void setMessageDrivenContext(MessageDrivenContext ctx)
     {
        this.ctx = ctx;
        log.info("TextMDB.setMessageDrivenContext, this="+hashCode());
     }
  
     public void ejbCreate()
     {
        log.info("TextMDB.ejbCreate, this="+hashCode());
        try
        {
           setupPTP();
        }
        catch(Exception e)
        {
           log.error("Failed to init TextMDB", e);
           throw new EJBException("Failed to init TextMDB", e);
        }
     }
     public void ejbRemove()
     {
        log.info("TextMDB.ejbRemove, this="+hashCode());
        ctx = null;
        try
        {
           if( session != null )
              session.close();
           if( conn != null )
              conn.close();
        }
        catch(JMSException e)
        {
           log.error("ejbRemove error", e);
        }
     }
  
     public void onMessage(Message msg)
     {
        log.info("TextMDB.onMessage, this="+hashCode());
        try
        {
           TextMessage tm = (TextMessage) msg;
           String text = tm.getText() + "processed by: "+hashCode();
           Topic dest = (Topic) msg.getJMSReplyTo();
           sendReply(text, dest);
        }
        catch(Throwable t)
        {
           log.error("onMessage error", t);
        }
     }
  
     private void setupPTP()
        throws JMSException, NamingException
     {
        InitialContext iniCtx = new InitialContext();
        Object tmp = iniCtx.lookup("java:comp/env/jms/TCF");
        TopicConnectionFactory qcf = (TopicConnectionFactory) tmp;
        conn = qcf.createTopicConnection();
        session = conn.createTopicSession(false,
           TopicSession.AUTO_ACKNOWLEDGE);
        conn.start();
     }
     private void sendReply(String text, Topic dest)
        throws JMSException
     {
        log.info("TextMDB.sendReply, this="+hashCode()
           +", dest="+dest);
        TopicPublisher sender = session.createPublisher(dest);
        TextMessage tm = session.createTextMessage(text);
        sender.publish(tm);
        sender.close();
     }
  
  }
  
  
  
  1.1      date: 2006/11/01 18:14:14;  author: nrichards;  state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/jms/ex3/ejb-jar.xml
  
  Index: ejb-jar.xml
  ===================================================================
  <?xml version="1.0"?>
  <!DOCTYPE ejb-jar
     PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN"
     "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
  <ejb-jar>
      <enterprise-beans>
          <message-driven>
              <ejb-name>TextMDB</ejb-name>
              <ejb-class>org.jboss.book.jms.ex3.TextMDB</ejb-class>
              <transaction-type>Container</transaction-type>
              <acknowledge-mode>AUTO_ACKNOWLEDGE</acknowledge-mode>
              <message-driven-destination>
                  <destination-type>javax.jms.Topic</destination-type>
                  <subscription-durability>Durable</subscription-durability>
              </message-driven-destination>
              <resource-ref>
                  <res-ref-name>jms/TCF</res-ref-name>
                  <res-type>javax.jms.TopicConnectionFactory</res-type>
                  <res-auth>Container</res-auth>
              </resource-ref>
          </message-driven>
      </enterprise-beans>
  </ejb-jar>
  
  
  
  1.1      date: 2006/11/01 18:14:14;  author: nrichards;  state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/jms/ex3/jboss.xml
  
  Index: jboss.xml
  ===================================================================
  <?xml version="1.0"?>
  <jboss>
     <enterprise-beans>
        <message-driven>
           <ejb-name>TextMDB</ejb-name>
           <destination-jndi-name>topic/jms.ex3.TopicB</destination-jndi-name>
           <mdb-user>john</mdb-user>
           <mdb-passwd>needle</mdb-passwd>
           <mdb-subscription-id>JMSEx3</mdb-subscription-id>
           <resource-ref>
              <res-ref-name>jms/TCF</res-ref-name>
              <jndi-name>ConnectionFactory</jndi-name>
           </resource-ref>
        </message-driven>
     </enterprise-beans>
  </jboss>
  
  
  



More information about the jboss-cvs-commits mailing list