Writing the Client

Listing 6.7 demonstrates how a client accesses a stateful bean.

Listing 6.7. The Full Text of day06/Client.java
package day06;

import java.util.*;
import javax.naming.*;
import javax.ejb.*;
public class Client {
  public static void main(String[] args) {
    print("Starting Client . . .
");
    Context initialContext = null;
    EnrollmentCartHome enrollmentCartHome = null;
    EnrollmentCart enrollmentCart = null;
    print("Demonstration of a simple client . . . 
");
    try {
       // Looking up the enrollment cart home via JNDI
       initialContext = new InitialContext();
       Object object = initialContext.lookup("day06/EnrollmentCartHome");
       enrollmentCartHome = (EnrollmentCartHome)
        javax.rmi.PortableRemoteObject.narrow(object,
                                      EnrollmentCartHome.class);
       print("Creating an enrollment cart.
");
       enrollmentCart =  (EnrollmentCart) enrollmentCartHome.create();
       String[] courseIds = { "CS101", "CS102", "CS103"};
       print("Adding some courses to our enrollment cart.
");
       enrollmentCart.addCourses(courseIds);
       String[] moreCourseIds = { "CS201", "CS202", "CS203"};
       print("Adding some more courses to our enrollment cart.
");
       enrollmentCart.addCourses(moreCourseIds);
       print("Getting the collection of courses in our enrollment cart.
");
       Collection collection = enrollmentCart.getCourses();
       print("Removing our enrollment cart.
");
       enrollmentCart.remove();

    } catch ( Exception e) {
       e.printStackTrace();
    }


    print("Demonstration of exceptions . . .
");
    try {
       print("Now trying to access enrollment cart that was removed.
");
       String[] courseIds = { "CS501" };
       enrollmentCart.addCourses(courseIds);
    } catch ( Exception e) {
      print("Exception caught while trying to access ");
      print(" enrollment cart that was removed.
");
      print(e.toString());
    }
    print("Demonstration of Activation/Passivation . . .
");
    try {
       EnrollmentCart carts[] = new EnrollmentCart[15];
       for ( int i = 0; i < 15 ; i++ ) {
          print("Creating cart " + i );
          carts[i] = enrollmentCartHome.create();
          String[] courseIds = { "CS601" };
          carts[i].addCourses(courseIds);
          Thread.sleep(1000);
       }
       for(int i = 0; i < 10; i++ ) {
          print("Removing cart" + i );
          carts[i].remove();
       }
    }
    catch(Exception e) {
       e.printStackTrace();
    }
  }
  static void print(String s) {
     System.out.println(s);
  }
}
					

The client locates the EnrollmentCartHome home interface of the deployed enterprise bean via JNDI, and then uses the remote home interface to create a remote EnrollmentCart session object. The client then calls the addCourses() business method, followed by the getCourses() business method, and removes the cart by calling remove method on the remote interface. Later, the client tries to access the session object that was removed earlier. This results in a java.rmi.NoSuchObjectException exception.

The client also demonstrates the activation and passivation by creating multiple instances of the bean. As you know, the container starts to passivate the instances when the number of instances in the cache reaches the threshold set in the vendor-specific deployment descriptor.

Caution

The EJB container may remove the session object in the following scenarios: a) A timeout due to client inactivity while the instance is in the passive state; b) A shutdown or crash of the container; c) A system exception thrown from the instance's method. All the object references and handles for the session object become invalid. If your client attempts to access the session object, the container will throw a java.rmi.NoSuchObjectException exception if the client is a remote client, or the javax.ejb.NoSuchObjectLocalException exception if the client is a local client.


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

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