Writing a Client

Listing 11.9 demonstrates how a client accesses an entity bean.

Listing 11.9. The Full Text of day11Client.java
package day11;

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

public class Client {
   public static void main(String[] args) {
      print("Starting Client . . .
");
      Context initialContext = null;
      OrderHome orderHome = null;
      Order order = null;

      try {
         print("Looking up the order home via JNDI.
");
         initialContext = new InitialContext();
         Object object = initialContext.lookup("day11/Order");
         orderHome = (OrderHome)
            javax.rmi.PortableRemoteObject.narrow(object,OrderHome.class);

         order = (Order)orderHome.create("1", "Submitted", 100);
         String orderId = order.getOrderId();
         print("Created a new order:" + orderId + " .
");

         print("Locating the order " + orderId
               + " using findByPrimaryKey method.
");
         order=orderHome.findByPrimaryKey(orderId);

         print("Locating all orders with status Submitted "
               + " using findByStatus method.
");
         Collection col=orderHome.findByStatus("Submitted");
         Enumeration orders=Collections.enumeration(col);
         while( orders.hasMoreElements() ) {
            order=(Order)orders.nextElement();
            print("Order id:"+ order.getOrderId());
         }

         print("Finding the total amount of all orders using 
"
               + " getTotalAmountOfAllOrders home method.
");
         double totalAmount = orderHome.getTotalAmountOfAllOrders();
         print("Total amount:"+ totalAmount );

      } catch ( Exception e) {
         e.printStackTrace();
      }
   }
   static void print(String s) {
      System.out.println(s);
   }
}

The client locates the OrderHome home interface of the deployed enterprise bean via JNDI and then uses the remote home interface to create a remote Order entity object. The client then demonstrates the use of the finder and home methods.

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

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