Client View of Bean  «Prev  Next»

Build EJB Client - Exercise

Calling the bean's business methods

Objective:Build the code for a complete client.

Exercise scoring

This exercise is worth a total of 4 points. To receive full credit, you'll need to examine each line of code for the HelloClient.java.
This code will act as a model for the BankCustomer.java client you will write and test in the next module.

Build EJB Overview

View the Calling Beans Business Methods below and view HelloClient.java which combines the code fragments you have just been introduced to into one program.
import javax.naming.*;
import javax.ejb.*;
import java.rmi.*;
import javax.rmi.*; // Note the extension to RMI for PortableRemoteObject()
public class HelloClient {

 public static void main2(String[] args) {

  HelloHome home = null;
  String response = null;
  Hello bean = null;
  System.out.println("Looking up the home object");
  try {
   Context initialContext = new InitialContext();
   Object objRef = initialContext.lookup("MyHello");
   home = (HelloHome) PortableRemoteObject.narrow(objRef, HelloHome.class);
  } 
  catch (NamingException e) {
   System.err.println("JNDI Naming Exception");
   System.err.println(e.getMessage());
   return;
  } 
  System.out.println("Creating the bean instance");
  try {
   bean = home.create();
  } 
  catch (CreateException e) {
   System.err.println("CreateException on HelloHome");
   System.err.println(e.getMessage());
   return;
  } 
  catch (RemoteException e) {
   System.err.println("RemoteException: create() on HelloHome");
   System.err.println(e.getMessage());
   return;
  } 

  System.out.println("Calling the business methods");
  try {
   System.out.println("calling String sayHello()");
   response = bean.sayHello();
   System.out.println("got " + response);
  }  
  catch (RemoteException e) {
   System.err.println("RemoteException while invoking sayHello()");
   System.err.println(e.getMessage());
   return;
  } 
  catch (HelloException e) {
   System.err.println("HelloException while invoking sayHello()");
   System.err.println(e.getMessage());
  }
  try {
   System.out.println("calling void sayHello(\"Mickey\")");
   response = bean.sayHello("Mickey");
   System.out.println("got " + response);
  } 
  catch (RemoteException e) {
   System.err.println("RemoteException while invoking sayHello()");
   System.err.println(e.getMessage());
   return;
  }
  System.out.println("Removing the bean");
  try {
   bean.remove();
  } 
  catch (RemoteException e) {
   System.err.println("RemoteException while removing bean");
   System.err.println(e.getMessage());
   return;
  } 
  catch (RemoveException e) {
   System.err.println("RemoveException while removing bean");
   System.err.println(e.getMessage());
   return;
  }
 }
}

Instructions

Take some time and examine the code.
After examining the code, ask yourself the following question:
All the exceptions that are caught are subclasses of Exception.
Why does the code not simply catch Exception in one large try/catch block as follows?

try{
  // All the code goes here
}
catch (Exception e){
  System.out.println("EJB failure!");
}

Submitting your exercise

Once you have entered your response into the text box, click the Submit button to submit the exercise.