L.3 Sample programs of Enterprise Bean (Annotations)
Following are the three types of annotation compliant Enterprise Bean sample programs:
-
Annotation compliant Enterprise Bean (without database connection)
Storage location is Cosminexus-installation-directory\CC\examples\ejb\annotation\stateless.
-
Annotation compliant Enterprise Bean (with database connection)
Storage location is Cosminexus-installation-directory\CC\examples\ejb\annotation\stateful.
-
Annotation compliant Enterprise Bean connected from Enterprise Bean (EJB2.0)
Storage location is
Cosminexus-installation-directory\CC\examples\ejb\annotation\home.
The overview, and the execution procedure of sample programs are as follows:
- Organization of this subsection
-
-
(1) Overview of annotation compliant Enterprise Bean (without database connection)
-
(2) Overview of annotation compliant Enterprise Bean (with database connection)
-
(3) Overview of annotation compliant Enterprise Bean connected from Enterprise Bean (EJB2.0)
-
(4) Execution procedure of Enterprise Bean (Annotations) sample programs
-
(1) Overview of annotation compliant Enterprise Bean (without database connection)
Following figure shows the configuration of annotation compliant Enterprise Bean sample program "annotation_stateless" without a database connection:
In this sample program, the program accesses the servlet through Web browser, and displays execution results on the browser. The program calls Enterprise Bean from the servlet by using the business interface.
Use the @EJB annotation on the servlet side to acquire the reference of annotation compliant Enterprise Bean.
@EJB(beanName="MyConverter") Converter converter;
Following figure shows the execution example of the sample program:
(2) Overview of annotation compliant Enterprise Bean (with database connection)
Following figure shows the configuration of annotation compliant Enterprise Bean sample program "annotation_stateful" with the database connection:
In this sample program, the program uses annotation to connect to the database. The program acquires the resource by @Resource annotation in annotation compliant Enterprise Bean.
@Resource(mappedName="DB_Connector_for_Cosminexus_Driver") private DataSource ds;
Use JNDI to look up Stateful Session Bean with the business interface from the servlet side.
Context initial = new InitialContext(); duke = (Bank)initial.lookup("java:comp/env/ejb/Bank");
The following figure shows the execution example of the sample program:
Enter user ID and amount, and then execute the process. You can specify User ID as 001. The initial balance is 10,000 yen.
(3) Overview of annotation compliant Enterprise Bean connected from Enterprise Bean (EJB2.0)
You can add the definition of the home interface to annotation compliant Enterprise Bean; to call annotation compliant Enterprise Bean from EJB2.0. Following figure shows the configuration of the sample program "annotation_home" that executes this process:
In this sample program, the program calls annotation compliant Enterprise Bean with the local home interface from Enterprise Bean of EJB2.0. The program looks up the local home interface in Enterprise Bean code of EJB2.0.
... Context ctx = new InitialContext(); Converter2LocalHome home = (Converter2LocalHome)ctx.lookup("java:comp/env/ejb/Converter2"); ...
Specify @LocalHome annotation in Enterprise Bean for the local home interface of annotation compliant Enterprise Bean.
@Stateless(name="MyConverter2") @LocalHome(value=Converter2LocalHome.class) public class Converter2EJB { ... }
(4) Execution procedure of Enterprise Bean (Annotations) sample programs
Following is the procedure to execute sample programs:
(a) Compiling sample programs
Compile sample programs by using batch files provided in the sample.
Sample program of Annotation compliant Enterprise Bean (with/without database connection) executes "compile.bat".
The sample connected to annotation compliant Enterprise Bean from Enterprise Bean of EJB2.0 executes "compileBean.bat" and "compileClient.bat".
(b) Executing SQL (Annotation compliant Enterprise Bean (with database connection))
When you use the sample program for connecting to the database, execute SQL in advance. SQL statements are available in the sample (createTable_Oracle.sql, createTable_HiRDB.sql).
(c) Setting the J2EE server
(d) Customizing the J2EE server
(e) Starting the J2EE server
(f) Setting the resource adapter (Annotation compliant Enterprise Bean (with database connection))
You must set the resource adapter when using sample programs for connecting to the database. See Appendix L.1(4) Setting the resource adapter.
(g) Deploying the application
Use the batch file (deployApp.bat) provided in the sample to deploy the application. This batch file imports, starts, displays the list of applications, and acquires stub and interface of Enterprise Bean.
Following is the process executed by this batch file:
-
Importing, starting and listing applications
For execution contents, see Appendix L.1(5) Importing, setting and starting the application.
-
Acquiring Enterprise Bean stub and interface file
Acquires the Enterprise Bean stub and interface file with the cjgetsubsjar command.
(h) Executing the application
The following procedure describes the details on executing each application:
-
Annotation compliant Enterprise Bean (without database connection)
Start the browser and specify the following string in URL:
http://Machin-name : Port-number/ContextRoot/Target file
-
Annotation compliant Enterprise Bean (with database connection)
Start the browser and specify the following string in the URL:
http://Machine-name: Port-number/ContextRoot/Target-file
-
Annotation compliant Enterprise Bean connected from Enterprise Bean (EJB2.0)
Use the batch file (testClient.bat) provided in the sample to execute the EJB client application. Following is the execution example of the application.
C:\Program Files\Hitachi\Cosminexus\CC\examples\ejb\annotation\home>testClient KDJE40053-I The cjclstartap command will now start. (directory for the user definition file = C:\Program Files\Hitachi\Cosminexus\CC\examples\ ejb\annotation\home, PID = 3684) 93.632 KDJE40054-I The cjclstartap command was stopped. (PID = 3684, exit status = 0)