dcsimg
December 5, 2016
Hot Topics:

Developing a CMP Bean

  • July 24, 2003
  • By Saleem Siddiqui, Michael Landy, & Jeff Swisher
  • Send Email »
  • More Articles »

Deploying Your Entity Bean

Deploying the bean is exactly the same as deploying the bean with a session bean. The entity bean we just created must be deployed on an application server before any client can access the bean. The process varies slightly depending on the version or brand of your application server. JBuilder insulates you from most of the differences, but nonetheless you have to know a little bit about your deployment environment. The process for deploying our bean is accomplished in two steps:

  1. Create an EAR node.

  2. Deploy it to the application server.

The topmost archive used for deployment is an Enterprise Archive File (EAR). The EAR file contains all the JAR and WAR files required for deployment and execution of our enterprise application. It also contains an application.xml file describing the manifest to the entire archive. In other words, it allows you to completely package your application into one deliverable, self-contained entity.

Let's now create an EAR file for our application, containing at this point only our session bean:

  1. Choose File, New from the menu and select the Enterprise tab.

  2. Select the EAR node to add to the project.

  3. Specify the name of the EAR file and node name. In this case, keep the name similar to the sample project SampleBeans.

  4. Right-click on the new node SampleBean.EARgrp and select Make. This creates a SampleBean.EAR, and contained within this is an application.xml descriptor.

  5. Right-click on the SampleBean.EARgrp and choose Deploy Your Bean. Note: Make sure that your application server is running; if it is not, start your application server and deploy the bean again.

Using Your Entity Bean

It is easiest to test any bean using the remote interface. If you want to test your local interface, you must communicate to that local interface with another object that encapsulates that call with a remote interface. For example, we will test our local interface using a session bean. But first let's test our entity bean's remote interface. The test client in this case only tests the finder method, as shown in Listing 7.

Listing 7 Test Client for Verifying the Operation of Our Employee Entity Bean (EmployeeTestClient.java)

package testclient;

import javax.naming.*;
import javax.rmi.*;

import com.sams.samples.entity.*;

/**
 * <p>Title: Empoyee Entity Test Client</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: SAMS Publishing</p>
 * @author SAMS Publishing
 * @version 1.0
 */

public class EmployeeTestClient {
 private EmployeeRemoteHome employeeRemoteHome = null;

 //Construct the EJB test client
 public EmployeeTestClient() {
  try {
   //get naming context
   Context ctx = new InitialContext();

   //look up jndi name
   Object ref = ctx.lookup("EmployeeRemote");

   //cast to Home interface
   employeeRemoteHome = (EmployeeRemoteHome) PortableRemoteObject.narrow(ref,EmployeeRemoteHome.class);

   Short empNo = new Short("5");

   EmployeeRemote employee = employeeRemoteHome.findByPrimaryKey(empNo);

   System.out.println(employee.getFirstName());
   System.out.println(employee.getLastName());
  }
  catch(Exception e) {
   e.printStackTrace();
  }
 }

 
 //-----------------------------------------------------------------------
 // Utility Methods
 //-----------------------------------------------------------------------

 public EmployeeRemoteHome getHome() {
  return employeeRemoteHome;
 }
 //Main method

 public static void main(String[] args) {
  EmployeeTestClient client = new EmployeeTestClient();
  // Use the getHome() method of the client object to call Home interface
  // methods that will return a Remote interface reference. Then
  // use that Remote interface reference to access the EJB.
 }
} 

The other option for our entity bean, or for that matter even a session bean, is a local interface. This local interface is available only to another bean running inside the same JVM. Listing 8 is the session bean developed to test the bean, and Listing 9 is the client used to exercise the session bean.


Note

The purpose of the local interface is simple—performance. This performance gain is accomplished primarily in passing data. With a remote interface, the data is passed by value. In other words, the data is copied and serialized to another object on the client. The local interface passes by reference, giving a huge performance increase.


Listing 8 Session Façade to Access the Employee Entity Bean (EmployeeFacadeBean.java)

package com.sams.samples.ejb;

import javax.ejb.*;
import javax.naming.*;
import javax.rmi.*;


public class EmployeeFacadeBean implements SessionBean {
 SessionContext sessionContext;
 public void ejbCreate() throws CreateException {
  /**@todo Complete this method*/
 }
 public void ejbRemove() {
  /**@todo Complete this method*/
 }
 public void ejbActivate() {
  /**@todo Complete this method*/
 }
 public void ejbPassivate() {
  /**@todo Complete this method*/
 }
 public void setSessionContext(SessionContext sessionContext) {
  this.sessionContext = sessionContext;
 }
 public java.lang.String getEmployeeFirstName(java.lang.Short empNo) {
  try {
   //get naming context
   Context ctx = new InitialContext();

   //look up jndi name
   Object ref = ctx.lookup("java:comp/env/ejb/employee");

   //cast to Home interface
   EmployeeHome employeeHome = (EmployeeHome) PortableRemoteObject.narrow(ref, EmployeeHome.class);

   Employee employee = employeeHome.findByPrimaryKey(empNo);

   return employee.getLastName();
  }
  catch(Exception e) {
   e.printStackTrace();
  }
  return null;
 }
}

Listing 9 Client Testing the Session Façade, Which in Turn Accesses the Employee Entity Bean (EmployeeFacadeTestClient.java)

package testclient;

import com.sams.samples.ejb.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;

/**
 * <p>Title: Session Facade Test Client</p>
 * <p>Description: Testing the entity beans local interface via the session</p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: SAMS Publishing</p>
 * @author unascribed
 * @version 1.0
 */

public class EmployeeFacadeTestClient {
 static final private String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
 static final private int MAX_OUTPUT_LINE_LENGTH = 100;
 private boolean logging = true;
 private EmployeeFacadeHome employeeFacadeHome = null;
 private EmployeeFacade employeeFacade = null;

 //Construct the EJB test client
 public EmployeeFacadeTestClient() {
  long startTime = 0;
  if (logging) {
   log("Initializing bean access.");
   startTime = System.currentTimeMillis();
  }

  try {
   //get naming context
   Context ctx = new InitialContext();

   //look up jndi name
   Object ref = ctx.lookup("EmployeeFacade");

   //cast to Home interface
   employeeFacadeHome = (EmployeeFacadeHome) PortableRemoteObject.narrow(ref, EmployeeFacadeHome.class);
   if (logging) {
    long endTime = System.currentTimeMillis();
    log("Succeeded initializing bean access.");
    log("Execution time: " + (endTime - startTime) + " ms.");
   }

   java.lang.Short empNo = new java.lang.Short("5");
   EmployeeFacade employeeFacade = this.create();
   System.out.println(employeeFacade.getEmployeeFirstName(empNo));
  }
  catch(Exception e) {
   if (logging) {
    log("Failed initializing bean access.");
   }
   e.printStackTrace();
  }
 }

 //-----------------------------------------------------------------------
 // Methods that use Home interface methods to generate a Remote interface
 // reference
 //-----------------------------------------------------------------------

 public EmployeeFacade create() {
  long startTime = 0;
  if (logging) {
   log("Calling create()");
   startTime = System.currentTimeMillis();
  }
  try {
   employeeFacade = employeeFacadeHome.create();
   if (logging) {
    long endTime = System.currentTimeMillis();
    log("Succeeded: create()");
    log("Execution time: " + (endTime - startTime) + " ms.");
   }
  }
  catch(Exception e) {
   if (logging) {
    log("Failed: create()");
   }
   e.printStackTrace();
  }

  if (logging) {
   log("Return value from create(): " + employeeFacade + ".");
  }
  return employeeFacade;
 }

 //-----------------------------------------------------------------------
 // Methods that use Remote interface methods to access data through the bean
 //-----------------------------------------------------------------------

 public String getEmployeeFirstName(Short empNo) {
  String returnValue = "";
  if (employeeFacade == null) {
   System.out.println("Error in getEmployeeFirstName(): " + ERROR_NULL_REMOTE);
   return returnValue;
  }
  long startTime = 0;
  if (logging) {
   log("Calling getEmployeeFirstName(" + empNo + ")");
   startTime = System.currentTimeMillis();
  }

  try {
   returnValue = employeeFacade.getEmployeeFirstName(empNo);
   if (logging) {
    long endTime = System.currentTimeMillis();
    log("Succeeded: getEmployeeFirstName(" + empNo + ")");
    log("Execution time: " + (endTime - startTime) + " ms.");
   }
  }
  catch(Exception e) {
   if (logging) {
    log("Failed: getEmployeeFirstName(" + empNo + ")");
   }
   e.printStackTrace();
  }

  if (logging) {
   log("Return value from getEmployeeFirstName(" + empNo + "): " + returnValue + ".");
  }
  return returnValue;
 }

 public void testRemoteCallsWithDefaultArguments() {
  if (employeeFacade == null) {
   System.out.println("Error in testRemoteCallsWithDefaultArguments(): " + ERROR_NULL_REMOTE);
   return ;
  }
  getEmployeeFirstName(null);
 }

 //-----------------------------------------------------------------------
 // Utility Methods
 //-----------------------------------------------------------------------

 private void log(String message) {
  if (message == null) {
   System.out.println("-- null");
   return ;
  }
  if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
   System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) + " ...");
  }
  else {
   System.out.println("-- " + message);
  }
 }
 //Main method

 public static void main(String[] args) {
  EmployeeFacadeTestClient client = new EmployeeFacadeTestClient();
  // Use the client object to call one of the Home interface wrappers
  // above, to create a Remote interface reference to the bean.
  // If the return value is of the Remote interface type, you can use it
  // to access the remote interface methods. You can also just use the
  // client object to call the Remote interface wrappers.
 }
}




Page 3 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel