Chapter 4 Creating Enterprise JavaBeans Components and Clients
To create and run the sample application:
Starting EAServer
Starting EAServer Manager
In EAServer, a package is a unit of deployment for a group of components that perform related tasks. Before a component can be instantiated by clients, it must be installed in a package, and that package must be installed in the server. The steps below create the package and component within the predefined "Jaguar" server to satisfy these requirements.
All components created in the EAServer tutorials are installed in the Tutorial package.
Creating the Tutorial package if it does not exist
Tutorial
.
We will define the entity bean by first creating the bean interface and implementation classes in Java, then importing the classes into EAServer Manager. Importing the classes creates the EJB component and defines the IDL interfaces required for it to run in Jaguar and be invoked by clients.
Creating the entity bean classes
Sample/Intro/Glossary
cd $JAGUAR/java/classes/Sample/Intro/Glossary $JAGUAR/bin/jc Glossary*.java
cd %JAGUAR\java\classes\Sample\Intro\Glossary %JAGUAR%\bin\jc Glossary*.java
Importing the entity bean classes into EAServer Manager
| Field | Value |
|---|---|
| Component Name | Glossary
|
| Component Type | JaguarEJB::EntityBean |
| Bean Class | Sample.Intro.Glossary.GlossaryBean
|
| Primary Key Class | java.lang.String
|
| Specify Remote Interface | (Checked) |
| Home Interface Class | Sample.Intro.Glossary.GlossaryHome
|
| Remote Interface Class | Sample.Intro.Glossary.Glossary
|
| Specify Local Interfaces | (Not checked) |
| Field | Value |
|---|---|
| Description | Tutorial EJB entity
bean
|
| Component Type | EJB - Entity Bean |
| EJB Version | 2.0 |
| JNDI Name | Tutorial/Glossary
|
As done to create the entity bean, we will define the session bean by first creating the bean interface and implementation classes in Java, then importing the classes into EAServer Manager.
Creating the session bean classes
cd $JAGUAR/java/classes/Sample/Intro/Glossary $JAGUAR/bin/jc Query*.java
cd %JAGUAR\java\classes\Sample\Intro\Glossary %JAGUAR%\bin\jc Query*.java
Importing the classes into EAServer Manager
| Field | Value |
|---|---|
| Component Name | Query
|
| Component Type | JaguarEJB::StatelessSessionBean |
| Bean Class | Sample.Intro.Glossary.QueryBean
|
| Specify Remote Interface | (Checked) |
| Home Interface Class | Sample.Intro.Glossary.QueryHome
|
| Remote Interface Class | Sample.Intro.Glossary.Query
|
| Specify Local Interfaces | (Not checked) |
| Field | Value |
|---|---|
| Description | Tutorial EJB session
bean
|
| Component Type | EJB - Stateless SessionBean |
| EJB Version | 2.0 |
| JNDI Name | Tutorial/Query
|
Use EAServer Manager to generate stubs and skeletons for the new components. The skeleton contains generated code to manage the interaction between EAServer and the implementation. The stubs are required by clients to execute the component.
Generating stubs and skeletons
Query
component.
Explicit stub generation is not required
When generating skeletons, EAServer Manager generates stubs
under the skeleton code base, java/classes.
We will use these stubs to run our client.
The glossary data is stored in an Adaptive Server Anywhere database. To make the data available to the components, we must start a database server, define a connection cache, and associate the connection cache with each component.
Copy the file gloss.db from the html/docs/tutorial/ejb/database directory to the sample directory of your EAServer installation. Create a batch or script file to run the database server as described below.
Creating and running the Windows batch file
SETLOCAL call %JAGUAR%\bin\setenv.bat cd %JAGUAR%\sample start %JAGUAR_ASA80%\win32\dbsrv8 -x tcpip(ServerPort=2640) -n localhost gloss.db ENDLOCAL
Creating and running the UNIX script file
#!/bin/sh # # If JAGUAR is not set where you run this, uncomment this
# line and edit the path to match your install location: #JAGUAR=/path/to/your/install export JAGUAR . $JAGUAR/bin/setenv.sh cd $JAGUAR/sample $SQLANY/bin/dbsrv7 -x "tcpip(ServerPort=2640)" -n localhost gloss.db
chmod 777 run_gloss
A connection cache maintains a pool of connections to a database server, increasing performance by allowing connection sharing and reuse. EJB connections obtain database connections using JNDI. In EAServer, we must associate a connection cache with the JNDI name alias that the component uses to look up connections.
Creating the connection cache
| Tab/ Setting |
Value |
|---|---|
| Name | Glossary
|
| Description | Glossary database
|
| Database Type | Sybase_ASA
|
| Server name | NetworkProtocol=Tds:Server=localhost:Port=2640
|
| User name | dba
|
| Password | sql
|
| Database Driver Type |
JDBC |
| DLL or Class Name | com.sybase.jdbc2.jdbc.SybConnectionPoolDataSource
|
Associating the cache with the EJB components
jdbc/glossary
.
Copy the file TestClient.java from the html/docs/tutorial/ejb directory to the java/classes/Sample/Intro/Glossary directory of your EAServer installation.
Compile this file with a JDK 1.3 or later compiler, for example, on UNIX:
cd $JAGUAR/java/classes/Sample/Intro/Glossary $JAGUAR/bin/jc TestClient.java
Or on Windows, in a Command window:
cd %JAGUAR\java\classes\Sample\Intro\Glossary %JAGUAR%\bin\jc TestClient.java
If you have not refreshed or restarted your server since last modifying the Query or Glossary components, refresh the server now before running the client. Otherwise, verify that the server is running.
Run the client from using a batch file or UNIX shell script. The batch file or shell script configures the CLASSPATH environment variable, then runs the application using the JDK 1.3 java program included with your EAServer installation.
Creating the Windows batch file
call %JAGUAR%\bin\setenv.bat set CLASSPATH=%JAGUAR%\java\lib\easj2ee.jar; set CLASSPATH=%CLASSPATH%;%JAGUAR%\java\lib\easclient.jar set CLASSPATH=%CLASSPATH%;%JAGUAR%\java\classes set JAVA_HOME=%JAGUAR_JDK13% %JAVA_HOME%\jre\bin\java Sample.Intro.Glossary.TestClient %*
Creating the UNIX shell script
#!/bin/sh . $JAGUAR/bin/setenv.sh CLASSPATH=$JAGUAR/java/lib/easj2ee.jar CLASSPATH=$CLASSPATH:$JAGUAR/java/lib/easclient.jar CLASSPATH=$CLASSPATH:$JAGUAR/java/classes export CLASSPATH JAVA_HOME=$JAGUAR_JDK13 $JAVA_HOME/jre/bin/java Sample.Intro.Glossary.TestClient $*
chmod 777 runtest
Running the client application
runtest iiop://host:iiop-port
runtest iiop://myhost:9000
The client application:
If errors occur, check the server log file for information on how to correct the problem.
| Copyright (C) 2004. Sybase Inc. All rights reserved. |
| |