Skip navigation.

Avitek Medical Records Development Tutorials

  Previous Next vertical dots separating previous/next from contents/index/pdf Contents View as PDF   Get Adobe Reader


Developing the MedRec Applications


Tutorial 10: Using EJBGen to Generate EJB Deployment Descriptors

This tutorial demonstrates how to use the WebLogic Server EJBGen utility to generate deployment descriptor files (ejb-jar.xml and weblogic-ejb-jar.xml) and EJB source files, such as the Home interface file, from the main programmer-written EJB bean source file that contains your business logic. In particular, this tutorial uses the PhysicianSession EJB from the physicianEar application.

EJBGen uses JDK 5.0 metadata annotations in the bean file to generate the deployment descriptor files and the supporting EJB Java source files, such as the Home and Remote interfaces. This means that you only need to program one file that contains your business logic, and use EJBGen-specific annotations inside this file to specify the shape and behavior of the EJB. The EJBGen utility then takes care of generating all supporting files.

The wlcompile Ant task automatically invokes the EJBGen utility if it encounters a Java file with a *.ejb extension, instead of the normal *.java extension. This means that you can mix EJBGen-annotated files with standard Java files in an EAR structure and use a single wlcompile Ant task to compile all the code.

All EJB files in the MedRec application are already annotated with EJBGen metadata annotations.

The tutorial includes the following sections:



Before starting this tutorial, complete Tutorial 9: Deploying MedRec from the Development Environment so that you have already compiled the physicianEar application, started the MedRec server, and deployed the application, including its EJBs.

If you completed up to Tutorial 5 but skipped one or more of the subsequent tutorials so that you have not yet deployed the physicianEar application, you can catch up by moving to the c:\medrec_tutorial\src\physicianEar subdirectory, setting the environment, and using Ant to run the default build.xml file to build the entire application and deploy it to WebLogic Server:

prompt> c:\bea\user_projects\domains\MedRecDomain\bin\setDomainEnv.cmd
prompt> cd c:\medrec_tutorial\src\physicianEar
prompt> ant -f build.xml build deploy.physician.ear



In the following steps you use the Administration Console to view administrative information about a deployed EJB; view the EJB Java source file that contains the EJBGen metadata annotations; view some of the files that EJBGen generates, and finally use EJBGen to regenerate those files.

Step 1: Use the Administration Console to view information about a deployed EJB.

The procedure is not necessary for using EJBGen; it is provided only as additional information about using the Administration Console to view a deployed EJB.

  1. Open the Administration Console by navigating in a browser to:
  2. http://host:7101/console

    where host refers to the computer on which MedRecServer is running. If your browser is on the same computer as MedRecServer, then you can use the URL http://localhost:7101/console.

  3. Specify weblogic for both the username and password and click Log In.
  4. In the left Domain Structure window, click MedRecDomain—>Deployments.
  5. In the right pane, in the Deployments table, expand the tutorial_deployment enterprise application.
  6. A list of all the modules, EJBs, and Web Services contained in the application appears below the application name.

    Note: If you followed the directions in Prerequisites to catch up, then the application is listed with the name PhysicianEar.

  7. Click PhysicianSessionEJB under the EJBs heading.
  8. Select the tabs to view information about the EJB. For example, the Configuration tab displays session and transaction information for the PhysicianSessionEJB.

Step 2: View the EJB bean class file that contains the EJBGen metadata annotations.

  1. Open a command window and move to the directory that contains the Java source file for the PhysicianSessionEJB:
  2. prompt> cd C:\medrec_tutorial\src\physicianEar\physSessionEjbs\com\bea\medrec\controller
  3. Use an IDE or text editor to view the PhysicianSessionEJB.ejb file:
  4. prompt> notepad PhysicianSessionEJB.ejb

    Because this Java source file has an .ejb extension, the wlcompile Ant command automatically uses the EJBGen utility to compile it into a deployable EJB. This is a handy convenience when programming EJBs that use EJBGen annotations because you let the wlcompile Ant determine how to process the file.

    PhysicianSessionEJB.ejb file is the main stateless session EJB bean implementation class file that programmers code and contains the business logic that defines how the EJB behaves. The file implements the WebLogic-specific abstract class weblogic.ejb.GenericSessionBean, which is a convenience class similar to the standard J2EE javax.ejb.SessionBean interface. The file uses EJBGen metadata annotations that further describe the shape and behavior of the EJB. Some typical annotations include:

See EJBGen Reference for additional information about EJBGen and the full list of annotations.

Step 3: View the already-generated deployment descriptor and Java files.

In Tutorial 7: Compiling Split Development Directory Applications with Ant Tasks, you executed the wlcompile Ant task, which in turn executed the EJBGen utility to compile the EJBs of the physicianEar application. This procedure describes how to view the deployment descriptor files and supporting Java interfaces that were generated by EJBGen, located in the \build directory.

  1. Open a command window and move to the directory that contains the deployment descriptor files that were generated by the EJBGen utility for PhysicianSessionEJB:
  2. prompt> cd c:\medrec_tutorial\build\physicianEar\physSessionEjbs\META-INF
  3. Use a text editor to view the two EJB-related deployment descriptor files:
  4. For details about the elements used in these files, see the ejb-jar_2_1.xsd and weblogic-ejb-jar.xsd XML Schema files.

  5. Move to the directory that contains the compiled EJB classes; the wlcompile Ant task also copies the Java source of the EJB files to this directory:
  6. prompt> cd c:\medrec_tutorial\build\physicianEar\physSessionEjbs\com\bea\medrec\controller
  7. Use an IDE or text editor to view the Java source files that make up the PhysicianSessionEJB. These files include the programmer-created bean class that contains the EJBGen annotations (described in Step 2: View the EJB bean class file that contains the EJBGen metadata annotations.), as well as the following Java files generated by the EJBGen utility:

Step 4: Use the EJBGen utility to regenerate the deployment descriptor and EJB files.

When you program EJBs using annotations within a split development directory structure, you typically only need to execute the wlcompile Ant task over the entire source directory to compile the EJB and generate its supporting files into the build directory. As long as the EJB files that use annotations have the *.ejb extension, the wlcompile Ant task automatically knows to execute the EJBGen Ant task on the file.

You can, however, also use the EJBGen utility on its own if you want to generate the EJB deployment descriptors and supporting home and remote interfaces individually, outside of the wlcompile Ant task. You can execute the EJBGen utility using either of the following methods:

The following procedure shows how to use the Ant task.

  1. Open a command window and set your environment:
  2. prompt> c:\bea\user_projects\domains\medrecdomain\bin\setDomainEnv.cmd
  3. Move to the physicianEar subdirectory:
  4. prompt> cd c:\medrec_tutorial\src\physicianEar
  5. Use a text editor to create a new file, ejbgen.xml:
  6. prompt> notepad ejbgen.xml

    Note: If you do not want to create the ejbgen.xml file manually in this tutorial, copy the file named ejbgen_tutorial.xml to a new file named ejbgen.xml and follow along.

  7. Start the ejbgen.xml file by defining a project named ejbgen_tutorial with a default target run-ejbgen:
  8. <project name="ejbgen_tutorial" default="run-ejbgen">
  9. Use the taskdef task to define the full classname of the EJBGen task:
  10.   <taskdef name="ejbgen"
    classname="com.bea.wls.ejbgen.ant.EJBGenAntTask" />
  11. Define the main target for executing the EJBGen task on the PhysicianSessionEJB.ejb Java file:
  12.   <target name="run-ejbgen">
    outputDir = "C:\medrec_tutorial\build\physicianEar\physSessionEjbs"
    descriptorDir = "C:\medrec_tutorial\build\physicianEar\physSessionEjbs\META-INF"
    forceGeneration = "true"
    <fileset dir="physSessionEjbs/com/bea/medrec/controller"
    includes="PhysicianSessionEJB.ejb" />

    The source attribute specifies that the source EJB bean file uses JDK 1.5 metadata annotations (rather than Javadoc tags used in previous releases of WebLogic Server.) The outputDir and descriptorDir attributes specify the directories into which EJBGen should generate the home and remote interfaces and the deployment descriptors, respectively. The forceGeneration attribute specifies that files should always be generated, even if there have been no changes to the source file. Finally, the Ant-standard fileset task specifies the directory which contains the source EJB bean files and the name of the file that EJBGen should process, in this case PhysicianSessionEJB.ejb.

  13. Complete the ejbgen.xml file by closing the project element:
  14. </project>
  15. Your file contents should now resemble the following:
  16. <project name="ejbgen_tutorial" default="run-ejbgen">
      <taskdef name="ejbgen"
    classname="com.bea.wls.ejbgen.ant.EJBGenAntTask" />
      <target name="run-ejbgen">
    outputDir = "C:\medrec_tutorial\build\physicianEar\physSessionEjbs"
    descriptorDir = "C:\medrec_tutorial\build\physicianEar\physSessionEjbs\META-INF"
    forceGeneration = "true"
    <fileset dir="physSessionEjbs/com/bea/medrec/controller"
    includes="PhysicianSessionEJB.ejb" />

    Save the file and exit your text editor.

  17. In the same command shell, enter the commands to execute the build script:
  18. prompt> ant -f ejbgen.xml

    You should receive the following output from the ejbgen task:

    Buildfile: ejbgen.xml
    [ejbgen] EJBGen 9.0
    [ejbgen] Creating C:\medrec_tutorial\build\physicianEar\physSessionEjbs\com\bea\medrec\controller\
    [ejbgen] Creating C:\medrec_tutorial\build\physicianEar\physSessionEjbs\com\bea\medrec\controller\
    [ejbgen] Creating C:\medrec_tutorial\build\physicianEar\physSessionEjbs\META-INF\ejb-jar.xml
    Creating C:\medrec_tutorial\build\physicianEar\physSessionEjbs\META-INF\weblogic-ejb-jar.xml
    Total time: 3 seconds

    If you did not receive the above output, or ran into a problem, you can use the Ant build file provided for this tutorial instead:

    prompt> ant -f ejbgen_tutorial.xml
  19. Verify that the deployment descriptors and home/remote interfaces were indeed regenerated by checking their file creation date and time:
  20. prompt> dir c:\medrec_tutorial\build\physicianEar\physSessionEjbs\META-INF
    prompt> dir c:\medrec_tutorial\build\physicianEar\physSessionEjbs\com\bea\medrec\controller\*.java

    The actual contents of the files is the same as that of the files that were previously generated with the wlcompile Ant task.


Best Practices

Use EJBGen to develop the EJB component of your application. You can simplify your EJB development and code maintenance by writing just the bean implementation files and annotating them with EJBGen metadata annotations, and then generating all the remaining files—the home interface, the local interface, the deployment descriptor files—using EJBGen.


The Big Picture

The scripts that compile and deploy MedRec use EJBGen to generate most of the EJB files in the application. The EJBGen task is called implicitly by the wlcompile task any time wlcompile encounters a file with the *.ejb extension.

The PhysicianSessionEJB.ejb Java file contains all of the information necessary for EJBGen to generate the EJB descriptor files and the home interface. You can view the EJBGen annotations by opening C:\medrec_tutorial\src\physicianEar\physSessionEjbs\com\bea\medrec\controller\PhysicianSessionEJB.ejb in an IDE or text editor.

For example, the following class-level @Session annotation specifies that the class implements a stateless session EJB, and then defines the pool and timeout settings that you see in the generated weblogic-ejb-jar.xml:

@Session(maxBeansInFreePool = "1000",
initialBeansInFreePool = "0",
transTimeoutSeconds = "0",
type = Session.SessionType.STATELESS,
defaultTransaction = Constants.TransactionAttribute.REQUIRED,
enableCallByReference = Constants.Bool.TRUE,
ejbName = "PhysicianSessionEJB")
public class PhysicianSessionEJB extends GenericSessionBean {


Related Reading


Skip navigation bar  Back to Top Previous Next