Writing a Client

Listing 10.11 demonstrates how a client accesses an entity bean.

Listing 10.11. The Full Text of day10/Client.java
package day10;

import java.util.*;
import javax.naming.*;
import javax.ejb.*;

public class Client {
   public static void main(String[] args) {
      print("Starting Client . . .");
      Context initialContext = null;
      StudentHome studentHome = null;
      Student student = null;
      try {
         print("Looking up the student home via JNDI.");
         initialContext = new InitialContext();
         Object object = initialContext.lookup("day10/Student");
         studentHome = (StudentHome)
            javax.rmi.PortableRemoteObject.narrow(object,StudentHome.class);

         String studentId = "raghava";
         print("Creating a new student id:" + studentId + ".");
         object=(Student)studentHome.create(studentId,  "Raghava",
             "Kothapalli", "1234, People Dr. Pleasonton, CA");
         student=(Student)javax.rmi.PortableRemoteObject.narrow
                (object, Student.class);
         print("Locating the student id " + studentId
               + " using findByPrimaryKey method");
         student=studentHome.findByPrimaryKey(studentId);

         print("Locating all students with last name Kothapalli "
               + " using findByLastName method.");
         Collection col=studentHome.findByLastName("Kothapalli");
         Enumeration students=Collections.enumeration(col);
         while( students.hasMoreElements() ) {
            student=(Student)javax.rmi.PortableRemoteObject.narrow
                (students.nextElement(), Student.class);
            print("id:" + student.getStudentId() +
                " First name:"+ student.getFirstName());
         }
         print("Finding the total number of students using "
               + " getTotalNumberOfStudents method");
         int count = studentHome.getTotalNumberOfStudents();
         print("Count:"+ count );
      }
      catch(Exception ex) {
         ex.printStackTrace();
      }
   }
   static void print(String s) {
      System.out.println(s);
   }
}

In this code, the client locates the StudentHome home interface of the deployed enterprise bean via JNDI. The client uses the remote home interface to create a remote Student entity object, and then demonstrates the usage of finder and home methods.

Caution

A client program that is portable to all EJB containers must use javax.rmi.PortableRemoteObject.narrow(...) method to perform type-narrowing of the remote and remote home interfaces. For example, the following code snippet is not portable:

Student student = ... ;
Enumeration students=Collections.enumeration(col);
while( students.hasMoreElements() ) {
    student=(Student)students.nextElement();
}

The following code snippet is portable:

Student student = ... ;
Enumeration students=Collections.enumeration(col);
while( students.hasMoreElements() ) {
          student= (Student)javax.rmi.PortableRemoteObject.narrow
                     (students.nextElement(),Student.class);
          . . .
}


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.123.155