![]() ![]() ![]() ![]() ![]() ![]() |
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.
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.
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
.
weblogic
for both the username and password and click Log In.tutorial_deployment
enterprise application.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 . |
PhysicianSessionEJB
under the EJBs heading.PhysicianSessionEJB
.PhysicianSessionEJB
:prompt> cd C:\medrec_tutorial\src\physicianEar\physSessionEjbs\com\bea\medrec\controller
PhysicianSessionEJB.ejb
file: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:
@Session
—Specifies, at the class-level, that the EJB is of type stateless session. Use attributes to specify characteristics of the EJB, as shown in the following example:@Session(maxBeansInFreePool = "1000",
initialBeansInFreePool = "0",
transTimeoutSeconds = "0",
type = Session.SessionType.STATELESS,
defaultTransaction = Constants.TransactionAttribute.REQUIRED,
enableCallByReference = Constants.Bool.TRUE,
ejbName = "PhysicianSessionEJB")
@JndiName
—Specifies, at the class-level, the remote or local JNDI name of the EJB. For example:@JndiName(remote = "PhysicianSessionEJB.PhysicianSessionHome")
@RemoteMethod
—Specifies, at the method-level, which methods are exposed in the generated Remote interface.See EJBGen Reference for additional information about EJBGen and the full list of annotations.
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.
PhysicianSessionEJB
:prompt> cd c:\medrec_tutorial\build\physicianEar\physSessionEjbs\META-INF
ejb-jar.xml
—The standard J2EE deployment descriptor that specifies the PhysicianSessionEJB bean, its interfaces, and its session and transaction types.weblogic-ejb-jar.xml
—The WebLogic-specific deployment descriptor that specifies PhysicianSessionEJB
's pool and time-out deployment settings..For details about the elements used in these files, see the ejb-jar_2_1.xsd and weblogic-ejb-jar.xsd XML Schema files.
wlcompile
Ant task also copies the Java source of the EJB files to this directory:prompt> cd c:\medrec_tutorial\build\physicianEar\physSessionEjbs\com\bea\medrec\controller
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:PhysicianSession.java
—The remote interface of the EJB that describes the signature of its public methods, such as addRecord
, getRecordSummary
, and so on. This interface extends the J2EE javax.ejb.EJBObject
interface.PhysicianSessionHome.java
—The home interface of the EJB that extends the J2EE javax.ejb.EJBHome
interface.
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.
prompt> c:\bea\user_projects\domains\medrecdomain\bin\setDomainEnv.cmd
physicianEar
subdirectory:prompt> cd c:\medrec_tutorial\src\physicianEar
ejbgen.xml
: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. |
ejbgen.xml
file by defining a project named ejbgen_tutorial
with a default target run-ejbgen
:<project name="ejbgen_tutorial" default="run-ejbgen">
taskdef
task to define the full classname of the EJBGen task:<taskdef name="ejbgen"
classname="com.bea.wls.ejbgen.ant.EJBGenAntTask" />
PhysicianSessionEJB.ejb
Java file: <target name="run-ejbgen">
<ejbgen
source="1.5"
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" />
</ejbgen>
</target>
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
.
ejbgen.xml
file by closing the project element:</project>
<project name="ejbgen_tutorial" default="run-ejbgen">
<taskdef name="ejbgen"
classname="com.bea.wls.ejbgen.ant.EJBGenAntTask" />
<target name="run-ejbgen">
<ejbgen
source="1.5"
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" />
</ejbgen>
</target>
</project>
Save the file and exit your text editor.
prompt> ant -f ejbgen.xml
You should receive the following output from the ejbgen
task:
Buildfile: ejbgen.xml
run-ejbgen:
[ejbgen] EJBGen WebLogic Server 9.2 SP0 Fri Jun 23 20:47:26 EDT 2006 783464
[ejbgen]
[ejbgen] Creating C:\medrec_tutorial\build\physicianEar\physSessionEjbs\com\bea\medrec\controller\PhysicianSessionHome.java
[ejbgen] Creating C:\medrec_tutorial\build\physicianEar\physSessionEjbs\com\bea\medrec\controller\PhysicianSession.java
[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
BUILD SUCCESSFUL
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
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.
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 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 {
...
The RecordEJB
entity bean is hard-coded to use the Pointbase database. This means that if you want the Medrec application to use a database other than Pointbase, such as Oracle or MySQL, you must change the source code of the RecordEJB
entity bean accordingly, in addition to changing the JDBC data sources (both global and application-scoped) to point to the new database.
The source code for the RecordEJB
entity bean is located in the following EJBGen file:
C:\medrec_tutorial\src\medrecEar\entityEjbs\com\bea\medrec\entities\RecordEJB.ejb
Use of the Pointbase database is hard-coded with the databaseType
attribute of the @Entity
metadata annotation, as shown by the following code excerpt (relevant section in bold):
@Entity(maxBeansInCache = "1000",
dataSourceName = "jdbc/MedRecTxDataSource",
transTimeoutSeconds = "0",
ejbName = "RecordEJB",
reentrant = Constants.Bool.FALSE,
concurrencyStrategy = Constants.ConcurrencyStrategy.DATABASE,
delayDatabaseInsertUntil = Entity.DelayDatabaseInsertUntil.EJB_POST_CREATE,
tableName = "record",
readTimeoutSeconds = "600",
primKeyClass = "java.lang.Integer",
defaultTransaction = Constants.TransactionAttribute.MANDATORY,
abstractSchemaName = "RecordEJB",
databaseType = Entity.DatabaseType.POINTBASE
)
To specify an Oracle database, for example, change the value of the databaseType
attribute to Entity.DatabaseType.ORACLE
.
See
weblogic.ejbgen.Entity for all valid values for the databaseType
attribute.
![]() ![]() ![]() |