Simple Bean Examples

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Simple Bean Examples

  1. #1
    Join Date
    May 2005

    Simple Bean Examples

    hi guys,

    I'm just starting to get into all the j2ee stuff. Recently I've gone over a couple examples with EJBs. I'm wondering if any of you have any more simple EJBs that I could check out. (probably the first ones you wrote!)

    I learn by example very well, and most of the EJB samples I've found are little much to start with. Anything you can hook me up with would be great! Send it however you like.

  2. #2
    Join Date
    May 2005
    the one that i am posting is an example of Message driven bean. needs JMS......
    i used weblogic8.1
    let me post the web.xml, weblogic-ejb-jar.xml, and
    hope that helps

    <!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' ''>


    <?xml version="1.0"?>

    <!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB//EN" "" >


    import java.util.*;
    import javax.ejb.*;
    import javax.jms.*;

    public class EmailMDB implements MessageDrivenBean, MessageListener {

    private MessageDrivenContext context;

    // --------------------------------------------------------------
    // EJB Methods from MessageDrivenBean interface
    // --------------------------------------------------------------
    public void ejbCreate() {
    System.out.println("EmailMDB: ejbCreate called");

    public void ejbRemove() {
    System.out.println("EmailMDB: ejbRemove called");

    public void setMessageDrivenContext(MessageDrivenContext context) {
    System.out.println("setMessageDrivenContext called");
    this.context = context;

    public void onMessage(Message message) {
    System.out.println("EmailMDB: onMessage called");
    TextMessage mapmessage = (TextMessage) message;
    try {

    // Go through the map message and create a map for sendmail(Map)
    System.out.println("Sending email: EmailHelper.sendmail(mail)" + mapmessage.getText());

    } catch(Exception e) {

    import java.rmi.*;
    import java.util.*;

    import javax.jms.*;
    import javax.naming.*;

    * EmailClient is responsible for sending an email mapmessage to the queue
    public class EmailClient {
    private static String QUEUE_NAME = "myqueue";
    private static String CONNECTION_FACTORY = "javax.jms.QueueConnectionFactory";
    private static String PROVIDER_URL = "t3://localhost:7001";

    private QueueSender sender;
    private QueueSession session;

    * Constructor: Setup JMS for publishing
    public EmailClient() {
    try {
    Context ctx = getInitialContext();

    // Lookup a JMS connection factory
    QueueConnectionFactory conFactory =
    (QueueConnectionFactory) ctx.lookup(CONNECTION_FACTORY);
    // Create a JMS connection
    QueueConnection connection = conFactory.createQueueConnection();

    // Create a JMS session object
    session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

    // Lookup a JMS queue
    Queue chatQueue = (Queue) ctx.lookup(QUEUE_NAME);

    // Create a JMS sender
    sender = session.createSender(chatQueue);
    } catch(Exception e) {

    * Method: sendmail(Map mail)
    public void sendmail() throws Exception {
    TextMessage message = session.createTextMessage();

    message.setText("this is working");
    // send the mapmessage to the Queue

    * Method: getInitialContext()
    * Login to JNDI
    * @return Context The initial context
    private Context getInitialContext() throws NamingException {
    Properties env = new Properties();
    env.put("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory");
    env.put("java.naming.provider.url", PROVIDER_URL);

    return new InitialContext(env);

    * Static Method: java com.customware.client.EmailClient to_addr [from_addr] [subject] [body]
    public static void main(String args[]) throws Exception {

    System.out.println("\nBeginning EmailClient\n");
    EmailClient client = new EmailClient();

    System.out.println("Sending mail message");

    all the best

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center