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. |