AddViaQMDBean_Dup.ejb Sample

This topic inludes the source code for the AddViaQMDBean_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 queue jms/SamplesAppMDBQ
12  * for messages with the message selector "Command = 'Add'", and creates
13  * a new token, using the entity bean SimpleTokenBean.
14  
15  * @ejbgen:message-driven default-transaction="NotSupported" 
16  *   message-selector="Command = 'Add'"    
17  *   max-beans-in-free-pool="2" 
18  *   ejb-name = AddViaQMD_Dup
19  *   destination-jndi-name = jms/SamplesAppMDBQ
20  *   destination-type = "javax.jms.Queue"
21  *
22  *  @ejbgen:ejb-local-ref link="SimpleToken_M"
23  */
24 public class AddViaQMDBean_Dup extends GenericMessageDrivenBean implements MessageDrivenBean, MessageListener
25 {
26   private SimpleTokenHome_M tokenHome;
27   private String MDBname;
28   private int objectHash;
29 
30   public void ejbCreate() {
31      MDBname =  this.getClass().getName();
32      objectHash = this.hashCode();
33      try {
34         javax.naming.Context ic = new InitialContext();
35         tokenHome = (SimpleTokenHome_M)ic.lookup("java:/comp/env/ejb/SimpleToken_M")
36      }
37      catch(NamingException ne) {
38         System.out.println("Encountered the following naming exception: " + ne.getMessage());        
39      }
40   }
41 
42   public void onMessage(Message msg) {
43      try {
44         MapMessage aMsg = (MapMessage)msg;
45         int tokenName = Integer.parseInt(aMsg.getString("tokenName"));    
46         tokenHome.create(tokenName,(new Date(System.currentTimeMillis()).toLocaleString()),MDBname, objectHash);
47         Thread.sleep(1000);
48      }
49      catch(Exception e) {
50         System.out.println("Encountered the following exception: " + e.getMessage());
51      }
52   }
53 }
54