To create and run the sample application:
Starting EAServer
If EAServer is not already running, follow the instructions under “Starting the server” to start the server.
Starting EAServer Manager
If EAServer Manager is not already running, start it as described in “Using 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
In EAServer Manager, expand the servers folder, then expand the Jaguar server icon.
Expand the Installed Packages folder. If the Tutorial package is displayed, skip to “Define the Glossary entity bean”.
Highlight the Installed Packages folder, and select File | Install Package.
In the Package wizard, select Create and Install a New Package.
For the package name, enter Tutorial
.
Click Create New Package.
You see the Package Properties window.
Click OK.
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
Under the EAServer java/classes directory, create the following subdirectory structure:
Sample/Intro/Glossary
Copy the following files from the html/docs/tutorial/ejb directory of your installation to the java/classes/Sample/Intro/Glossary directory:
Glossary.java defines the remote interface.
GlossaryHome.java defines the home interface.
GlossaryBean.java contains the source for the implementation.
Compile these classes using a JDK 1.3 or later compiler, for example, on UNIX:
cd $JAGUAR/java/classes/Sample/Intro/Glossary $JAGUAR/bin/jc Glossary*.java
Or on Windows, in a Command window:
cd %JAGUAR\java\classes\Sample\Intro\Glossary %JAGUAR%\bin\jc Glossary*.java
Importing the entity bean classes into EAServer Manager
In EAServer Manager, click on the Tutorial package.
Select File | New Component.
In the Component wizard, select Import From EJB Class File. Click Next.
In the Component wizard - CLASSPATH screen, click Next. No changes are required to the default CLASSPATH to import our classes.
In the Component wizard - Import EJB Class Files screen, enter the values below:
Field |
Value |
---|---|
Component Name |
|
Component Type |
JaguarEJB::EntityBean |
Bean Class |
|
Primary Key Class |
|
Specify Remote Interface |
(Checked) |
Home Interface Class |
|
Remote Interface Class |
|
Specify Local Interfaces |
(Not checked) |
Click Finish. You see a dialog saying “All methods imported,” then the Component Properties dialog box displays.
Apply or confirm the following settings to the General tab fields in the Component Properties dialog box:
Field |
Value |
---|---|
Description |
|
Component Type |
EJB - Entity Bean |
EJB Version |
2.0 |
JNDI Name |
|
Leave other fields as-is, and click OK.
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
Copy the following files from the html/docs/tutorial/ejb directory of your installation to the java/classes/Sample/Intro/Glossary directory:
Query.java defines the remote interface.
QueryHome.java defines the home interface.
QueryBean.java contains the source for the implementation.
Compile these classes using a JDK 1.3 or later compiler, for example, on UNIX:
cd $JAGUAR/java/classes/Sample/Intro/Glossary $JAGUAR/bin/jc Query*.java
Or on Windows, in a Command window:
cd %JAGUAR\java\classes\Sample\Intro\Glossary %JAGUAR%\bin\jc Query*.java
Importing the classes into EAServer Manager
In EAServer Manager, click on the Tutorial package.
Select File | New Component.
In the Component wizard, select Import From EJB Class File.
In the Component wizard - CLASSPATH screen, click Next. No changes are required to the default CLASSPATH to import our classes.
In the Component wizard - Import EJB Class Files screen, enter the values below:
Field |
Value |
---|---|
Component Name |
|
Component Type |
JaguarEJB::StatelessSessionBean |
Bean Class |
|
Specify Remote Interface |
(Checked) |
Home Interface Class |
|
Remote Interface Class |
|
Specify Local Interfaces |
(Not checked) |
Click Finish. You see a dialog saying “All methods imported,” then the Component Properties dialog box displays.
Apply or confirm the following settings to the General tab fields in the Component Properties dialog box:
Field |
Value |
---|---|
Description |
|
Component Type |
EJB - Stateless SessionBean |
EJB Version |
2.0 |
JNDI Name |
|
Leave other fields as-is, and click OK.
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
Click on the Tutorial package and select the Glossary component.
Select File | Generate Stub/Skeleton.
Deselect Generate Stubs.
Click Next to display the skeleton generation options and configure them as follows:
Select Generate Skeletons.
Select Generate Skeletons on Server.
Select Compile Java Skeletons.
Click Next to display the advanced options and configure them as follows:
For Java Version, select JDK 1.2 and Above.
For Generation Strategy, choose Full.
Click Finish.
Repeat these steps to generate stubs and skeletons for
the 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
In the sample directory of your EAServer installation, create a batch file named run_gloss.bat, containing the commands below. These commands start the database server on port 2640. If that port is in use on your machine, edit the port number to an unused value:
SETLOCAL call %JAGUAR%\bin\setenv.bat cd %JAGUAR%\sample start %JAGUAR_ASA80%\win32\dbsrv8 -x tcpip(ServerPort=2640) -n localhost gloss.db ENDLOCAL
Start the database by running run_gloss.bat. For example, double-click this file in Windows Explorer.
Creating and running the UNIX script file
Edit the bin/setenv.sh script in your EAServer installation, and verify that the SQLANY setting matches the location where you have installed Adaptive Server Anywhere.
Create a text file run_gloss, containing the commands below. These commands start the database server on port 2640. If that port is in use on your machine, edit the port number to an unused value:
#!/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
Change the file permissions to allow the script to be executed. For example:
chmod 777 run_gloss
Run the script in a terminal window.
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
Click on the Connection Caches folder in EAServer Manager, and select File | New Connection Cache.
In the Connection Cache wizard, configure these settings and leave others at their default values:
Tab/ Setting |
Value |
---|---|
Name |
|
Description |
|
Database Type |
|
Server name |
|
User name |
|
Password |
|
Database Driver Type |
JDBC |
DLL or Class Name |
|
Verify the connection cache properties as follows:
Highlight the Glossary cache and choose File | Refresh.
Highlight the Glossary cache and choose File | Ping. If the Ping operation fails, confirm that you have applied the settings correctly and that the database is running.
Associating the cache with the EJB components
Click on the Tutorial package and select the Glossary component.
Choose File | Component Properties.
In the Component Properties dialog box, display the Resource Refs tab.
Click Add to create a new reference in the list. Configure the values as follows:
Set the Name field to jdbc/glossary
.
For Type, choose java.sql.Datasource.
For Authentication, choose Container.
Click in the Resource Link field at the bottom of the dialog, and choose Glossary from the drop-down list.
Click OK to save the changes.
Repeat these steps for the Query component.
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
Create a file named runtest.bat containing the commands below:
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
Create a file named runtest containing the commands below:
#!/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 $*
Change the file permissions to allow the script to be executed. For example:
chmod 777 runtest
Running the client application
Run the batch or script file, specifying the server host name and IIOP port number on the command line as follows:
runtest iiop://host:iiop-port
For example:
runtest iiop://myhost:9000
The client application:
Creates a proxy for the Glossary entity bean’s home interface, then calls the create method to populate the database with some glossary entries.
Creates a proxy for Query session bean’s home interface, then calls the runQuery method to get a result set containing all the entries, then prints them.
If errors occur, check the server log file for information on how to correct the problem.
Copyright © 2005. Sybase Inc. All rights reserved. |
![]() |