Day 15

Quiz

A1:

Point-to-point and publisher-subscriber.

A2:

Synchronous and asynchronous.

A3:

First, the messaging should be asynchronous; that means the receiver need not wait to receive the message. The JMS provider should notify the receiver/consumer when a message comes on the queue.

Second, the message must be delivered once and only once.

A4:

A header zone, containing the header as name-value pairs; a properties zone, containing the selector properties for the message; and a body zone, which contains the content of the message.

Exercise

A1:

The following code listing shows a HelloWorldPublisher.java program containing the main() method, and a message-driven bean called MessageHelloBean. HelloWorldPublisher publishes the HelloWorld message on the topic hello_topic. When the message arrives on the topic, the container notifies the message-driven bean through the onMessage() method. This method then uses the WebLogic log and displays the method. Next, the ejb-jar.xml and weblogic-ejb-jar.xml files are listed. You will see that the topic on which the application publishes the message is mapped in the "destination-jndi-name" tag.

 package com.sams.learnweblogic7.ejb.jms;  import java.rmi.RemoteException; import java.util.Properties; import javax.jms.*; import javax.ejb.*; import javax.naming.*; import javax.rmi.*; public class HelloWorldPublisher {   static private String MESSAGE_TOPIC_NAME = "hello_topic";   private String messageUrl;   private Context messageContext;   private TopicConnection messageTopicConnection;   public HelloWorldPublisher(String mesgUrl) throws NamingException{     messageUrl = msgUrl;     try{       //first create the context object.       Properties h = new Properties();       h.put(Context.INITIAL_CONTEXT_FACTORY,               "weblogic.jndi.WLInitialContextFactory");       h.put(Context.PROVIDER_URL, messageUrl);       messageContext = new InitialContext(h);       // Create the connection       TopicConnectionFactory topicConnFactory = (TopicConnectionFactory)               messageContext.lookup("weblogic.jms.ConnectionFactory");       messageTopicConnection = topicConnFactory.createTopicConnection();       messageTopicConnection.start();     }     catch(Exception e) {       e.printStackTrace();     }   }//end of constructor public static void main(String[] args) throws Exception {     System.out.println("*****  Beginning of message Hello world  *****");     HelloWorldPublisher msgPublisher = null;     try {       msgPublisher = new HelloWorldPublisher("t3://localhost:7001");     } catch (NamingException e){       e.printStackTrace();     }     try {            Topic myTopic = null;            TopicSession mySession = null;            try {                   mySession =                           messageTopicConnection.createTopicSession                           (false,Session.AUTO_ACKNOWLEDGE);                   myTopic = (Topic)                          messageContext.lookup(MESSAGE_TOPIC_NAME);            }            catch(NamingException ex) {               myTopic = mySession.createTopic(MESSAGE_TOPIC_NAME);               messageContext.bind(MESSAGE_TOPIC_NAME, newTopic);            }            TopicPublisher topicPublisher =                    session.createPublisher(myTopic);            TextMessage helloWorldMessage = session.createTextMessage();            helloWorldMessage.setText("Hello World");            topicPublisher.publish(helloWorldMessage);     }     catch (Exception e) {       e.printStackTrace();     }     System.out.println("*****  End message.Client  *****");   } } 

The message-driven bean is as follows:

 package com.sams.learnweblogic7.ejb.jms;  import weblogic.rmi.*; import javax.ejb.*; import javax.jms.*; import javax.naming.*; public class MessageHelloBean implements MessageDrivenBean, MessageListener {   private static final boolean VERBOSE = true;   private MessageDrivenContext messageContext;   // Using WebLogic's log service   private void log(String s) {     System.out.println(s);   }   /**    * Per the EJB specifications    */   public void ejbActivate() {     log("MessageHelloBean's ejbActivate called");   }   /**    * Per the EJB specifications    */   public void ejbRemove() {     log("MessageHelloBean's ejbRemove called");   }   /**    * Per the EJB specifications    */   public void ejbPassivate() {     log("MessageHelloBean's ejbPassivate called");   }   /**    * This method sets the session context.    */   public void setMessageDrivenContext(MessageDrivenContext msgCtx) {     messageContext = msgCtx;   }   public void ejbCreate () throws CreateException {    //empty for this application   }  /**    * Implementation of MessageListener    * Retrieve the text message and display on WebLogic's log.    */   public void onMessage(Message mesg) {     TextMessage helloTextMsg = (TextMessage) mesg;     try {       String text = helloTextMsg.getText();       log(text);     }     catch(JMSException e) {       e.printStackTrace();     }   } } 

Here is the ejb-jar.xml file:

 <!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>MessageHelloBean</ejb-name>       <ejb-class>com.sams.learnweblogic7.ejb.jms.MessageHelloBean </ejb-               class>       <transaction-type>Container</transaction-type>       <message-driven-destination>         <destination-type>javax.jms.Topic</destination-type>       </message-driven-destination>     </message-driven>   </enterprise-beans> <ejb-bar> 

The weblogic-ejb-jar.xml file is as follows:

 <?xml version="1.0"?>  <!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.         //DTD WebLogic 7.0.0 EJB//EN" "http://www.bea.com/servers/wls700/dtd/weblogic-ejb-jar.dtd"> <weblogic-ejb-jar>   <weblogic-enterprise-bean>     <ejb-name>MessageHelloBean</ejb-name>     <message-driven-descriptor>       <pool>         <max-beans-in-free-pool>200</max-beans-in-free-pool>         <initial-beans-in-free-pool>20</initial-beans-in-free-pool>       </pool>       <destination-jndi-name>hello_topic</destination-jndi-name>     </message-driven-descriptor>     <jndi-name>MessageHelloBean</jndi-name>   </weblogic-enterprise-bean> </weblogic-ejb-jar> 



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net