Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 6
public interface Calculator {
public double calculate (int start, int end,
double growthrate, double saving);
}
The session bean class simply implements the business interface. You must tell the EJB 3.0 container that this POJO class
is a session bean by annotating it with the Stateless or Stateful annotations. A stateful session bean maintains the client state across several different service requests. On the contrary,
stateless session bean requests are served by a random bean instance each time. Their behaviors match those of the old EJB
2.1 stateful and stateless session beans. The EJB 3.0 container figures out when to instantiate the bean object and makes
it available via the business interface. Below is the code for the session bean implementation class:
@Stateless
public class CalculatorBean implements Calculator {
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;
}
}
You can also specify multiple interfaces for a session bean—one for local clients and one for remote clients. Just use the
@Local and @Remote annotations to differentiate the interfaces. The following snippet shows that the CalculatorBean session bean implements both a local and a remote interface. If you do not have the @Local and @Remote annotations, the session bean interface defaults to a local interface.
@Stateless
@Local ({Calculator.class})
@Remote ({RemoteCalculator.class})
public class CalculatorBean implements Calculator, RemoteCalculator {
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;
}
public String getServerInfo () {
return "This is the JBoss EJB 3.0 TrailBlazer";
}
}
The session bean client obtains a stub object of the bean via JNDI (Java Naming and Directory Interface). Provided by the container, the stub object implements the session bean's business interface. All calls made to the stub object are routed to the container and invoked against the managed bean instances. For stateless session beans, you can obtain a new stub every time you make a call. For stateful session beans, you must cache the stub on the client side so the container knows to provide you with the same bean instance for each subsequent call. The following snippet shows how to make a call to the session bean. Later in the article, you will learn a simpler way to obtain a bean stub object.
InitialContext ctx = new InitialContext();
cal = (Calculator) ctx.lookup(Calculator.class.getName());
double res = cal.calculate(start, end, growthrate, saving);
To achieve loose coupling, the application defers the creation, pooling, and destruction of the session bean instance to the EJB 3.0 container (i.e., the Inversion of Control design pattern). And the application works only with the business interfaces.
Archived Discussions (Read only)