August 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Enterprise JavaBeans: Getting Started with EJB, Part 3

  • December 12, 2000
  • By Richard G. Baldwin
  • Send Email »
  • More Articles »

Java Programming, Lecture Notes #1312


Preface

This is one in a series of lessons designed to teach you about Sun's Enterprise JavaBeans(TM) (EJB).

This lesson is also the third lesson in a miniseries intended to get you up and running with Sun's Enterprise JavaBeans(TM) very quickly.

Preview

This lesson assumes that you have completed the previous lessons and that you now have the class files necessary to deploy your bean into the J2EE server.

In this and the next few lessons, I will walk you through the detailed steps necessary to deploy your new bean in the J2EE server.

You will learn about jar files and ear files and the difference between the two.

You will learn that the deployment descriptor is now packaged as an XML file in J2EE (instead of a serialized Properties object as was the case in earlier versions).

You will learn about J2EE Applications, which are higher-level constructs than enterprise beans.

You will learn about the three steps required for deployment of an enterprise bean.

Introduction

Writing, compiling, deploying, and executing a simple bean

The lessons in this miniseries will show you the steps involved in writing, compiling, deploying and executing a simple bean.  The discussion will be brief and to the point.

More details in subsequent lessons

Subsequent lessons will come back and fill in the gaps to help you more-fully understand each of the steps.

Subsequent lessons will also teach you about the different kinds of beans and how to use those beans in different situations.

Recommended supplementary reading

It is strongly recommended that you study the previous lesson entitled Enterprise JavaBeans(TM), Getting Started with EJB, Part 2 before embarking on this lesson.

Additional supplementary material that I have written is available at Gamelan.  As of the date of this writing, Gamelan does not maintain a consolidated index to my Java tutorials.  Therefore, they may be difficult to locate on the Gamelan site.  A consolidated index with links to the tutorials on the Gamelan site is available at Baldwin's Java Programming Tutorials.

The required steps

An earlier lesson entitled Getting Started with EJB, Part 1 contained a list of the required steps for writing, compiling, deploying and executing a simple bean.  Abbreviated versions of the first seven steps in that list are repeated below.

  • Download and install Java 2 Platform, Standard Edition from Sun.
  • Download and install the Java 2 Platform, Enterprise Edition.
  • Write the bean class and required helper classes.
  • Write home and remote interfaces.
  • Write a compilation batch file for the bean and the interfaces.
  • Compile the bean class, helper classes, and interfaces.
  • Use Sun's J2EE deploytool program to deploy the bean into the server, being careful to save the client jar file.  The client jar file contains some class files for stubs and skeletons that are required by the client.
Download and install software

The first two steps (shown in blue) require you to download and install the JDK and the J2EE.  I am assuming that you are able to accomplish those two steps with no help from me.

Write/compile bean and interfaces

The next four steps (shown in green) were covered in the previous lesson entitled Getting Started with EJB, Part 2.  In that lesson, I showed you explicitly how to accomplish all four of those steps.

Class files available

Having completed those steps, you should now have the class files necessary to deploy your bean into the J2EE server.  That is the subject of the last step in the above list (shown in black).

The last step is a big one

The last step is a big one, so I will subdivide it into several sequential lessons.

In this and the next couple of lessons, I will walk you through the detailed steps necessary to deploy your new bean in the J2EE server.  I will provide that information in cookbook fashion, meaning that I will tell you what to do but not necessarily why to do it.

Beans versus beans

Just in case you are reading this lesson without having read the earlier lessons in this series, I will tell you that an enterprise bean is not the same thing as a JavaBean Component(TM), as discussed in my series of tutorial lessons beginning with lesson number 500.

Sample Bean Program

Minimum programming requirement for a bean

The minimum programming requirement for an enterprise bean consists of one class definition and two interface definitions.

In the previous lesson, I developed and discussed the elements of a simple bean, including the two required interface definitions.  That is the bean that we will begin deploying in the J2EE server in this and the following lessons.  Complete listings of the source code for the bean and the two interfaces are shown near the end of this lesson.

Required class files

At this point, you should have the following three class files as a result of compiling the bean class and the two interfaces from the previous lesson.  Make sure that you have them all located conveniently in the same directory.

  • HelloBean.class
  • HelloHome.class
  • Hello.class
The first of these is the class file for the bean proper.  The second is the class file for the home interface.  The third is for the remote interface.

A note about packages

When you create your own beans, you will probably want to put them in packages.  However, for learning purposes at this level, the use of packages simply adds unneeded complexity.  Therefore, if you examine the source code for the bean and the two interfaces, you will find that I did not use package directives.  Therefore, this bean and the two interfaces are all part of the default package.

The J2EE Application

Eliminating some possible confusion

If you have done much reading on EJB, you have probably read that an enterprise bean is packaged inside a jar file.  The jar file contains the class files for the bean plus a couple of other files.

The deployment descriptor

One of the other files contained in the jar file is commonly referred to as the deployment descriptor.  Depending on the age of the book that you were reading, you may have seen the deployment descriptor described as a serialized version of a Java Properties object.

Another application of XML

At some point in the history of EJB, that was the case.  However, as of J2EE version 1.2.1, that has changed.  The deployment descriptor is now an XML file.

In case you are unfamiliar with XML files, I have published quite a few tutorial lessons on XML.  You will find a consolidated index to those lessons on my web site.

The application

Also, your reading may have led you to the conclusion that the jar file containing the EJB is deployed directly into the server.  That is true for some server products.  However, that is not true for J2EE from Sun.  Rather, with J2EE from Sun, you must first wrap the EJB jar file in an application.  Then the application is deployed into the server.

Jar files and ear files

The application is also packaged in an archive file, but the extension for the archive file is ear rather than jar.  Presumably this extension indicates enterprise archive.

As you are probably already aware, you can view and extract the contents of a jar file using an unzip program such as WinZip.  You can also view and extract the contents of an ear file using the same unzip program.

The HelloApp.ear file

After you finish deploying this simple enterprise bean, you will have a file named HelloApp.ear.  If you view the contents of that file, you will see that it contains the following files:

  • meta-inf\manifest.mf
  • meta-inf\application.xml
  • meta-inf\sun-j2ee-ri.xml
  • ejb-jar-ic.jar
A new subdirectory

If you extract the contents of the file, a new subdirectory named meta-inf will be created and the first three files listed above will be extracted into that directory.

The enterprise bean jar file

If you view the contents of the jar file named ejb-jar-ic.jar, you will find that it contains the following files:

  • meta-inf\ejb-jar.xml
  • meta-inf\manifest.mf
  • Hello.class
  • HelloBean.class
  • HelloHome.class
This is the file that actually contains the enterprise bean.  Typically that file contains a manifest file, a deployment descriptor, and all of the class files required to execute the bean.

Another jar file

While we are on the subject of jar files, let me mention that there is still another jar file involved in the use of J2EE.  The deployment process produces a jar file that contains material required by the client program when it accesses the server to execute the enterprise bean.

A higher-level construct

A J2EE application is a higher-level construct than an enterprise bean.

Since this series of lessons is concentrating on enterprise beans, I'm not going to go into this in any detail at this point.  Suffice it to say that in addition to enterprise beans, the J2EE application can also contain web components and J2EE Application Client components.

Web components

Web components consist of servlets and JSP files.  (I have published numerous lessons on servlets and JSP.  You can find a consolidated index to those lessons on my web site.)  Web components, when present, are packaged inside a war file.  The war file is in turn packaged inside the ear file.

Application Client components

J2EE Application Client components, when present, are packaged in a jar file, which is in turn packaged inside the ear file.

Steps for deployment

The process of deploying our enterprise bean into the server will consist of the following major steps.

  • Create the J2EE Application
  • Package the Enterprise Bean
  • Deploy the J2EE Application
There will be several individual steps that must be accomplished inside each major step.

Summary

This lesson has begun the process of walking you through the detailed steps necessary to deploy your new bean in the J2EE server.

You have learned about jar files and ear files.

You have learned that the deployment descriptor is now packaged as an XML file in J2EE.

You have learned that a J2EE Application is a higher-level construct than an enterprise bean.

You have learned about the three steps required for deployment of an enterprise bean.
 

Where To From Here?

The next lesson will pick up with the first step listed above, creating the J2EE Application.

Subsequent lessons will cover the packaging of the Enterprise Bean and the deployment of the J2EE Application.

Complete Program Listings

A complete listing of the bean program is shown in Listing 1 below.  Complete listings of the remote and home interfaces are shown in Listing 2 and Listing 3.
 
/* File HelloBean.java
Copyright 2000 R.G.Baldwin
Rev 9/15/00

This bean receives and returns a String object
via a single business method.  It also displays
information about its life cycle in the 
System.out log file or on the system comsole
depending on whether or not System.out has been
directed to the log files.

This is a stateless session bean.  According to
Sun, a session bean must meet the following
reuirements:

Implement the SessionBean interface.
Class is defined as public.
Class cannot be defined as abstract or final.
Implements one or more ejbCreate methods.
Implements the business methods.
Contains a public constructor with no 
  parameters.
Must not define the finalize method.

Tested using Sun j2ee and JDK 1.3 under 
WinNT 4.0 workstation.
***********************************************/
import javax.ejb.*;
import java.util.Date;

public class HelloBean implements SessionBean{
  SessionContext ctx;

  public void ejbCreate(){
    System.out.println("HelloBean Created " 
                                  + new Date());
  }//end ejbCreate()

  public String aBusinessMethod(String str){
    return "Hello " + str;
  }//end aBusinessMethod()

  public void ejbRemove(){
    System.out.println("HelloBean Removed " 
                                  + new Date());
  }//end ejbRemove()

  public void ejbActivate(){
    System.out.println("HelloBean Activated " 
                                  + new Date());
  }//end ejbActivate()

  public void ejbPassivate(){
    System.out.println("HelloBean Passivated " 
                                  + new Date());
  }//ejbPassivate()

  public void setSessionContext(
                            SessionContext ctx){
    this.ctx = ctx;
    System.out.println(
      "HelloBean got SessionContext " 
                                  + new Date());
  }//setSessionContext()

  public HelloBean(){}//public constructor

}//end class HelloBean
 

Listing 1


 
/* File HelloHome.java 
Copyright 2000 R.G.Baldwin
Rev 9/15/00
***********************************************/
import javax.ejb.*;

public interface HelloHome extends EJBHome{
  public Hello create()
                throws java.rmi.RemoteException, 
                      javax.ejb.CreateException;
}//end interface

Listing 2


 
/* File Hello.java
Copyright 2000 R.G.Baldwin
Rev 9/15/00
***********************************************/
import javax.ejb.*;
import java.rmi.*;

public interface Hello extends EJBObject{
  String aBusinessMethod(String s)
                         throws RemoteException;
}//end interface

Listing 3


Copyright 2000, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

baldwin.richard@iname.com






Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel