The ConverterClient.java source code illustrates the basic tasks performed by the client of an enterprise bean:
Creating an enterprise bean instance
Invoking a business method
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.
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);
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); System.out.println("$" + param + " is " + yenAmount + " Yen."); BigDecimal euroAmount = converter.yenToEuro(yenAmount); System.out.println(yenAmount + " Yen is " + euroAmount + " Euro."); System.exit(0); } catch (Exception ex) { System.err.println("Caught an unexpected exception!"); ex.printStackTrace(); } } }