AddViaTopicMDBean_Dup.ejb Sample

This topic inludes the source code for the AddViaTopicMDBean_Dup.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_Dup
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_Dup extends GenericMessageDrivenBean implements MessageDrivenBean, MessageListener
22 {
23   private SimpleTokenHome_M tokenHome;
24   private String MDBname;
25   private int objectHash;
26   
27   public void ejbCreate() {
28      MDBname =  this.getClass().getName();
29      objectHash = this.hashCode();
30      try {
31         javax.naming.Context ic = new InitialContext();
32         tokenHome = (SimpleTokenHome_M)ic.lookup("java:/comp/env/ejb/SimpleToken_M")
33      }
34      catch(NamingException ne) {
35         System.out.println("Encountered the following naming exception: " + ne.getMessage());        
36      }
37   }
38 
39   public void onMessage(Message msg) {
40      try {
41         TextMessage aMsg = (TextMessage)msg;
42         int tokenName = Integer.parseInt(aMsg.getText());
43         tokenHome.create(tokenName,(new Date(System.currentTimeMillis()).toLocaleString()),MDBname, objectHash);
44         Thread.sleep(1000);
45      }
46      catch(Exception e) {
47         System.out.println("Encountered the following exception: " + e.getMessage());
48      }
49   }
50 }
51