background image

Invoking a Business Method

<< Creating the converter Application Client | Creating the converter Web Client >>
<< Creating the converter Application Client | Creating the converter Web Client >>

Invoking a Business Method

Creating a Reference to an Enterprise Bean Instance
Java EE application clients refer to enterprise bean instances by annotating static fields with the
@EJB
annotation. The annotated static field represents the enterprise bean's business interface,
which will resolve to the session bean instance when the application client container injects the
resource references at runtime.
@EJB
private static Converter converter;
The field is static because the client class runs in a static context.
Invoking a Business Method
Calling a business method is easy: you simply invoke the method on the injected Converter
object. The EJB container will invoke the corresponding method on the ConverterBean
instance that is running on the server. The client invokes the dollarToYen business method in
the following lines of code.
BigDecimal param = new BigDecimal (
"100.00");
BigDecimal amount = currencyConverter.dollarToYen(param);
ConverterClient
Source Code
The full source code for the ConverterClient program follows.
package com.sun.tutorial.javaee.ejb;
import java.math.BigDecimal;
import javax.ejb.EJB;
public class ConverterClient {
@EJB
private static Converter converter;
public ConverterClient(String[] args) {
}
public static void main(String[] args) {
ConverterClient client = new ConverterClient(args);
client.doConversion();
}
public void doConversion() {
try {
BigDecimal param = new BigDecimal(
"100.00");
BigDecimal yenAmount = converter.dollarToYen(param);
Creating the converter Application Client
Chapter 21 · Getting Started with Enterprise Beans
649