AddViaTopicMDBean.ejb Sample

This topic inludes the source code for the AddViaTopicMDBean.ejb Sample.

Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/EJBs/messageDriven/

Sample Source Code


01 package messageDriven;
02 
03 import java.util.Date;
04 import javax.ejb.*;
05 import javax.jms.*;
06 import javax.naming.InitialContext;
07 import javax.naming.NamingException;
08 import weblogic.ejb.*;
09 
10 /**
11  * This Message-Driven Bean listens to the topic jms/SamplesAppMDBTopic
12  * and creates a new token, using the entity bean SimpleTokenBean.
13 
14  * @ejbgen:message-driven default-transaction="NotSupported" max-beans-in-free-pool="2"  
15  *   ejb-name = AddViaTopicMD
16  *   destination-jndi-name = jms/SamplesAppMDBTopic
17  *   destination-type="javax.jms.Topic"
18  *
19  *  @ejbgen:ejb-local-ref link="SimpleToken_M"
20  */
21 public class AddViaTopicMDBean
22   extends GenericMessageDrivenBean
23   implements MessageDrivenBean, MessageListener
24 {
25   private SimpleTokenHome_M tokenHome;
26   private String MDBname;
27   private int objectHash;
28   
29   public void ejbCreate() {
30      MDBname =  this.getClass().getName();
31      objectHash = this.hashCode();
32      try {
33         javax.naming.Context ic = new InitialContext();
34         tokenHome = (SimpleTokenHome_M)ic.lookup("java:/comp/env/ejb/SimpleToken_M")
35      }
36      catch(NamingException ne) {
37         System.out.println("Encountered the following naming exception: " + ne.getMessage());        
38      }
39   }
40 
41   public void onMessage(Message msg) {
42      try {
43         TextMessage aMsg = (TextMessage)msg;
44         int tokenName = Integer.parseInt(aMsg.getText());
45         tokenHome.create(tokenName,(new Date(System.currentTimeMillis()).toLocaleString()),MDBname, objectHash);
46         Thread.sleep(1000);
47      }
48      catch(Exception e) {
49         System.out.println("Encountered the following exception: " + e.getMessage());
50      }
51   }
52 }
53