Home arrow Java arrow Page 4 - Getting Started with Enterprise Java Beans (EJB) 3.0
JAVA

Getting Started with Enterprise Java Beans (EJB) 3.0


There are significant differences between earlier versions of Enterprise Java Beans (EJB) and EJB 3.0. At least some of these pertain to business components. EJB 3.0 makes it a lot easier to implement the complexity required by many enterprises in fewer steps. Keep reading to learn more.

Author Info:
By: A.P.Rajshekhar
Rating: 4 stars4 stars4 stars4 stars4 stars / 20
January 16, 2007
TABLE OF CONTENTS:
  1. · Getting Started with Enterprise Java Beans (EJB) 3.0
  2. · The difference between EJB 2.1 and EJB 3.0
  3. · Implementing an EJB Step By Step
  4. · EJB in the Real World

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.

blog comments powered by Disqus
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...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials