Java
  Home arrow Java arrow Page 4 - Getting Started with Enterprise Java Beans...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Getting Started with Enterprise Java Beans (EJB) 3.0
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 19
    2007-01-16

    Table of Contents:
  • Getting Started with Enterprise Java Beans (EJB) 3.0
  • The difference between EJB 2.1 and EJB 3.0
  • Implementing an EJB Step By Step
  • EJB in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Getting Started with Enterprise Java Beans (EJB) 3.0 - EJB in the Real World


    (Page 4 of 4 )

    For the purpose of introducing EJB, I will be developing a simple investment growth calculator. The main files are:

    • CalculatorInterface.java - The business interface of the Session Bean.
    • CalculatorBean.java - The stateless Session Bean that implements the logic.
    • CalculatorClient.java - The client for the Calculator Bean.

    The code for the business interface is as shown below:

    public interface CalculatorInterface { public double calculate (int start, int end, double growthrate, double saving); }

    It declares a method that calculates the growth rate of investments.

    Next comes the Bean implementation. First the import:

    import javax.ejb.*;

    Only the classes of the javax.ejb package are required. Next comes the annotation that declares the Bean to be a Stateless Session EJB.

    @Stateless

    Next comes the actual implementation. The Bean implements the business interface:

    public class CalculatorBean implements CalculatorInterface{ public double calculate (int start, int end, double growthrate, double
    saving) { double tmp = Math.pow(1.+growthrate/12., 12.*(end - start) + 1); return saving*12.*(tmp - 1)/ growthrate; } }

    The complete code looks like this:

    import javax.ejb.*; @Stateless public class CalculatorBean implements CalculatorInterface{ public double calculate(int start,int end, double growthrate,double
    saving) { double tmp = Math.pow(1.+ growthrate/ 12.,12.*(end-start)+1); returnsaving*12.*(tmp-1)/growthrate; } }

    Next is the Calculator Client. The client takes the values from the command line and displays the result. Here is the code:

    public class CalculatorClient { public static void main(String[] args) { @Inject CalculatorBean; Calculator calculatorBean;         Stringresult; int start=25; int end=65; doublegrowthrate=0.08; doublesaving=300.0; try{ start=Integer.parseInt(args[0]); end=Integer.parseInt(args[1]); growthrate=Double.parseDouble(args[2])); saving=Double.parseDouble(args[3]); NumberFormatnf=NumberFormat.getInstance(); nf.setMaximumFractionDigits(2); result=nf.format(calculatorBean.calculate
    (start,end,growthrate,saving)); }catch(Exceptione){ //e.printStackTrace(); result="Notvalid"; }        System.out.println(result); } }

    The client first obtains a reference to the Bean through dependency injection. Then it parses each command line argument into the corresponding double values and passes them to the calculator method of the Bean. The result is then displayed after formatting so that the result is shown only up to two digits after the decimal point. That covers the client.

    This brings us to the end of the discussion. Here we have just skimmed the surface of EJB 3.0. The details of each EJB types and other characteristics as well as services provided by EJB 3.0 will be tackled in the future. Till then...


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · There has been a major overhaul of EJB development process between EJB 2.1 and EJB...
       · The article describes clearly about developing the EJB but it should also provide...
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek