# AddViaQMDBean.ejb Sample

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