JavaTouch EJB3 Tutorial
 

- JavaTouch

- EJB 3
- NetBeans IDE

Javatouch EJB3 tutorial is intended to give a fast start to EJB 3 by helping develop a simple EJB3 application in quick time. Once you have the look and feel of EJB3 your learning curve is going to be small. We will create a small Stateless Session bean, deploy and then write a small client to use it. That’s all.. No fuzz!! Java is for fun not for serious..... JavaTouch is here for helping hand.... Enjoy EJB 3....

Software Requirements:
NetBeans IDE 5.5 with Java System Application Server PE 9.
If you are still not using NetBeans Then download NetBeans IDE 5.5 with Application Server free here..

 

Creating an Enterprise Application

1. Choose File > New Project (Ctrl-Shift-N) from the main menu.
2. Select Enterprise Application from the Enterprise category and click Next.
3. Name the project ConverterBean and set the server to Sun Java System Application Server.
4. Set the J2EE Version to Java EE 5, and select Create EJB Module and Create Web ApplicationModule, and Create Application Client Module  if unselected.
5. In the Main Class text box, type ConverterClient.
6. Click Finish.

 


Creating the EJB (Session Bean)

7. On ConverterBean-ejb select Right Hand Mouse(RHM) click menu New > Session Bean
8. In the dialog give EJB Name as ConverterBean and give a package (eg: com.javatouch.ejb). Select Session type as Stateless and Create Interface as Remote. Select Finish.

ConverterBean Enterprice Bean class and ConverterRemote Business Interface class will be generated by IDE.

9. Now add following two business methods to the Bean interface.

    public BigDecimal dollarToYen(BigDecimal dollars);
    public BigDecimal yenToEuro(BigDecimal yen);

10. Press Alt-Shift-F to generate any necessary import statements for the class.

package com.javatouch.ejb;

import java.math.BigDecimal;
import javax.ejb.Remote;
/**
 * This is the business interface for Converter enterprise bean.
 */
@Remote
public interface ConverterRemote {
    public BigDecimal dollarToYen(BigDecimal dollars);
    public BigDecimal yenToEuro(BigDecimal yen);
   
}

11. Now Open the ConverterBean class and add the relevant implementation of the business methods.Press Alt-Shift-F to generate any necessary import statements for the class.

package com.javatouch.ejb;

import java.math.BigDecimal;
import javax.ejb.Stateless;

@Stateless
public class ConverterBean implements com.javatouch.ejb.ConverterRemote {
    
    public ConverterBean() {
    }
    private BigDecimal yenRate = new BigDecimal("115.3100");
    private BigDecimal euroRate = new BigDecimal("0.0071");

   
    public BigDecimal dollarToYen(BigDecimal dollars) {
        BigDecimal result = dollars.multiply(yenRate);
        return result.setScale(2, BigDecimal.ROUND_UP);
    }
   
    public BigDecimal yenToEuro(BigDecimal yen) {
        BigDecimal result = yen.multiply(euroRate);
        return result.setScale(2, BigDecimal.ROUND_UP);
    }
   
}

12. Now build the ConverterBean-ejb project (Right Hand Mouse(RHM) click menu Build Project)
13. Now Run the ConverterBean-ejb project (Right Hand Mouse(RHM) click menu Run Project). This will deploy our EJB module in the server.

 

Coding the client:

14. Open the ConverterClient class in ConverterBean-app-client project created. (This is already created because we selected the Create Application Client Module at step 4)
15.Add main method body as follows. Press Alt-Shift-F to generate any necessary import statements for the class. (Make sure you select  javax.naming.Context for import of Context)

import com.javatouch.ejb.ConverterRemote;
import java.math.BigDecimal;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class ConverterClient {
    
    public ConverterClient() {
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      Context ctx;
      ConverterRemote converterBean;
      try {
        ctx = new InitialContext();
        converterBean = (ConverterRemote)ctx.lookup(ConverterRemote.class.getName());
            
        BigDecimal param = new BigDecimal("100.00");
        BigDecimal yenAmount = converterBean.dollarToYen(param);
            
        System.out.println("$" + param + " is " + yenAmount + " Yen.");
        BigDecimal euroAmount = converterBean.yenToEuro(yenAmount);
        System.out.println(yenAmount + " Yen is " + euroAmount+ " Euro.");
            
        System.exit(0);
      } catch (NamingException ex) {
        ex.printStackTrace();
      } catch (Exception ex) {
        System.err.println("Caught an unexpected exception!");
        ex.printStackTrace();
      }
    }
   
}

16. Now build the ConverterBean-app-client  (Right Hand Mouse(RHM) click menu Build Project)
17. Now Run the ConverterClient class ((Right Hand Mouse(RHM) click on ConverterClient file and select menu Run File)

You will see an out put like follows in the console.

$10110.00 is 1165784.10 Yen.
1165784.10 Yen is 8277.07 Euro.
BUILD SUCCESSFUL (total time: 3 seconds)


Download NetBean Project Source code for this tutorial here...

References:
- Java EE 5 Tutorial (Getting Started with Enterprise Beans)
- EJB 3.0 Enterprise Beans tutorials at NetBeans


Discuss this tutorial at JavaTouch Google Group

Copy Rights @ JavaTouch.
Author: Nalaka Gamage

Current Rating: Rate This Artical: