Packaging and Deploying the EJBOverview

Packaging and Deploying the EJB Overview

Once we have the bean working in the JDeveloper environment, we can package it in preparation for deployment to an application server. We'll do this by using JDeveloper in the next section, but first, let's take a look at the steps that would be involved were we to do it manually.

The J2EE specification defines a way to package Web applications and EJB applications into modules and a way to package these modules into an enterprise application. An enterprise application is packaged in an EAR file. The EAR file, in turn, can contain either EJB modules packaged as JAR files, Web applications packaged as WAR files, or both.

  • Enterprise application EAR file

  • EJB module JAR file

    • EJBs (Java classes)

    • Deployment descriptor

  • Web application WAR file

    • Servlets

    • JSPs

Note that, although they are called EAR, JAR, and WAR files, they are all the same type of file archives produced using the Java jar utility. The extension serves only to indicate what type of contents the archive contains. Figure 11-1 shows the assembly of an EAR file.

Figure 11-1. Enterprise application archive assembly.

graphics/11fig01.gif

Creating JAR, WAR, and EAR files

Having written our EJB and compiled it, the resulting .class files and the deployment descriptor must be placed in a specific directory structure.

 META-INF\ejb-jar.xml classes 

META-INF is a directory that contains the deployment descriptor, ejb-jar.xml (as presented above), listing all the EJBs that we are packaging in this EJB module (which is only one in this example, of course). The classes that each EJB comprises are also specified in the ejb-jar.xml.

If the EJB's class files were not included in packages, they would be located in the top-level directory as peers of the META-INF directory. But classes are generally organized into packages. In this example, JDeveloper put our interfaces into a package named mypackage2 and our bean implementation into a subpackage of this, mypackage2\impl. The directory structure must correspond to this package structure (as must the entries in the deployment descriptor). The directory and file structure for our HelloWorld EJB is, therefore, as follows:

  • META-INF\ejb-jar.xml

  • Mypackage2\HelloWorld.class

  • Mypackage2\HelloWorldHome.class

  • Mypackage2\impl\HelloWorldBean.class

We bundle the EJB and the deployment descriptor into a JAR file using the standard Java jar utility. Assuming that we are in the top-level directory, we could use the following command:

 jar -cvf HelloWorld.jar * 

This produces a file, HelloWorld.jar.

If we were building a full-blown Web application, we might also have Web components such as servlets and JSPs which we would also bundle using the Java jar utility. In this case, however, we would use a different extension, calling the file HelloWebApp.war, for example, to indicate that it is a Web module. We'll skip that step here because it doesn't apply to our example.

Once we've created our EJB JAR file and, hypothetically, a Web module WAR file, we can bundle them into an enterprise application archive an EAR file. Like the EJB JAR format, the EAR requires that we have a META-INF directory with an XML descriptor. This XML file, the application descriptor, lists all the JAR files and WAR files (and possibly dependency libraries and other resources) in the application. This is what the directory structure generally looks like:

 META-INF\application.xml ejb-module.jar web-module.war 

The EJB JAR files and Web module WAR files are usually placed in the top-level directory, but they can also be in subdirectories; for a large application with many EJB and Web modules, this might make more sense. Their entries in the application descriptor obviously must indicate the directories they are in, if this is the case.

The application descriptor for our HelloWorld application, application.xml, might look like this:

[View full width]

<?xml version = '1.0' encoding = 'windows-1252'?> <!DOCTYPE application PUBLIC "-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN" graphics/ccc.gif"http://java.sun.com/j2ee/dtds/application_1_2.dtd"> <application> <display-name>HelloWorld</display-name> <module> <ejb>HelloWorld.jar</ejb> </module> </application>

This is what our directory structure looks like:

 META-INF\application.xml HelloWorld.jar 

To generate the EAR file using Java jar utility, we could run the following command in the top-level directory:

 jar -cvfM HelloWorld.ear * 

The well-specified format of an EAR file and its components, such as the EJB JAR file and Web application WAR file, help ensure the portability of an enterprise application between different vendors' application servers. An EAR file can (at least in principle) be deployed on any J2EE-compliant application server.

Unlike the preceding steps developing and packaging the EJB the step of deploying the EJB is not specified by the J2EE specification and depends on the application server. OC4J provides an administration tool, admin.jar, in the same directory as oc4j.jar for this task. It's also possible simply to edit the server.xml file (in the OC4J config directory) and add an entry for the new EAR file. The OC4J server will notice the change and deploy it automatically.

Packaging and Deploying the EJB with JDeveloper

One of the unstated points of the preceding section is that packaging and deploying an EJB is the type of well-defined, laborious task that is best automated. Indeed, every application server vendor provides some kind of tool (or tools) for producing and deploying EAR files.

To package and deploy our HelloWorld EJB using JDeveloper, we first need to create the deployment profile, as follows:

  • Right-click on the project file, HelloWorld.jpr.

  • On the box that appears, Select Deployment Profiles; on the left, select EJD JAR File J2EE EJB Module.

  • In the next box, change the filename to HelloWorld. We don't actually care what the deployment profile file is called, but JDeveloper bases the name of the JAR file, EAR file, and the application name on the name of the profile file. Press Save.

  • Accept the defaults presented on this screen.

The deployment profile generates an XML file specific to JDeveloper that, among other things, indicates the name of the EJB JAR file and the EAR file that are to be generated when we deploy the HelloWorld EJB.

The JAR and EAR files aren't generated until we actually deploy the EJB. To do this:

  • Right-click on the deployment profile, HelloWorld.deploy.

  • Select Deploy to=>OC4J (assuming that you've created the OC4J connection, as described in the previous chapter).

The log window will display the progress as it creates the JAR and EAR files, and invokes admin.jar. If successful, there will be an exit status of 0 (indicating no errors), and the message Deployment finished will appear.

To test this EJB, create a sample application.

  • Right-click on the HelloWorld bean embedded OC4J.

  • Select Connect to Remote Server.

  • Verify that the application name is HelloWorld and that the connection to the standalone OC4J instance is selected. Press OK.

This generates a Java file, HelloWorldClient1.java. It differs from the sample application we created above only in that the admin password for the application server and the URL for the EJB are different.

 env.put(Context.SECURITY_PRINCIPAL, "admin"); env.put(Context.SECURITY_CREDENTIALS, "adminpwd"); env.put(Context.PROVIDER_URL, "ormi://noizmaker/HelloWorld"); 

As before, uncomment the call to helloWorldHome.create() and helloWorld.getMessage(), and enclose the latter call with a call to System.out.println().

 // Use one of the create() methods below to create a new instance helloWorld = helloWorldHome.create(  ); // Call any of the Remote methods below to access the EJB System.out.println(helloWorld.getMessage(  )); 

Right-click on HelloWorldClient1.java and select Run HelloWorld-Client1.java. This will run the program and display the "Hello, world" message in the log window.

We can also run the EJB client application outside of JDeveloper. The easiest way is to start with the command line that appeared in the message window when we ran HelloWorldClient1.java. We can't simply copy and paste this command as it is to a command prompt, however. First, we need to substitute java.exe for javaw.exe. The difference between them is that javaw.exe does not use a console window, so if we run our application with javaw.exe from a command prompt, we won't get any output.

We can optionally make a few more changes to simplify the command line. First, we can omit the -ojvm option, which selects Oracle's specialized Java Virtual Machine, OJVM. This is a nonstandard option, and it's not necessary, in any case, because it's the default with Oracle's version of java.exe and javaw.exe. Also, we can eliminate the entries in the CLASSPATH that we don't need for our application, such as mail.jar and jaxp.jar.

The simplified command for starting our application is

 c:\JDeveloper\jdk\bin\java D:\jdev\jdev\mywork\ejbexamples\ HelloWorld\classes;D:\jdev\Jdk\jre\lib\ext\activation.jar;D :\jdev\jdk\jre\lib\ext\jndi.jar;D:\jdev\j2ee\home\ejb.jar;D :\jdev\j2ee\home\jdbc.jar;D:\jdev\j2ee\home\jaas.jar;D:\jde v\j2ee\home\oc4j.jar Samplemypackage2.HelloWorldClient 

Instead of typing this by hand at a command prompt, you may wish to write a batch file to run it, instead. The following example is not only easier to understand and maintain, but it can also be adapted for other client applications.

 @echo off rem runhello.bat - Run EJB HelloWorld client application setlocal rem Java application to run rem ------------------------------------------------------- set JAVA_APP=Samplemypackage2.HelloWorldClient1 rem Set these paths according to your environment rem ------------------------------------------------------- set JRE_LIB=c:\JDeveloper\jdk\jre\lib\ext set OC4J=c:\JDeveloper\j2ee\home rem These jar files shouldn't change much, unless you use rem additional j2ee features such as JAXP rem ------------------------------------------------------- set JRE_CLASSES=%JRE_LIB%\activation.jar;%JRE_LIB%\jndi.jar set OC4J_CLASSES=%OC4J%\ejb.jar;%OC4J%\jdbc.jar;%OC4J%\oc4j.jar rem Set environment variables rem ------------------------------------------------------- set CLASSPATH=.;%JRE_CLASSES%;%OC4J_CLASSES% set PATH=%JDEV%\jdk\bin\java;%PATH% rem Run application: rem ------------------------------------------------------- java %JAVA_APP% endlocal 

Create and save this in the classes directory of your project, for example, as runhello.bat in the following directory:

 c:\JDeveloper\jdev\mywork\ejbexamples\HelloWorld\classes 

At a DOS prompt, change to this directory and enter the command runhello:

 D:\jdev\jdev\mywork\ejbexamples\HelloWorld\classes>runhello Hello, world 


Java Oracle Database Development
Java Oracle Database Development
ISBN: 0130462187
EAN: 2147483647
Year: 2002
Pages: 71

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net