Chapter 4 Creating Enterprise JavaBeans Components and Clients


Creating the application

To create and run the sample application:

  1. Start EAServer and EAServer Manager.
  2. Create the package and components.
  3. Create the glossary database and connection cache.
  4. Create the client application.
  5. Run the client application.

Start EAServer and EAServer Manager

Steps Starting EAServer

  1. If EAServer is not already running, follow the instructions under "Starting the server" to start the server.

Steps Starting EAServer Manager

  1. If EAServer Manager is not already running, start it as described in "Using EAServer Manager".

Create the package and components

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.

Define a new package

All components created in the EAServer tutorials are installed in the Tutorial package.

Steps Creating the Tutorial package if it does not exist

  1. In EAServer Manager, expand the servers folder, then expand the Jaguar server icon.
  2. Expand the Installed Packages folder. If the Tutorial package is displayed, skip to "Define the Glossary entity bean".
  3. 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 .
  4. Click Create New Package.

    You see the Package Properties window.
  5. Click OK.

Define the Glossary entity bean

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.

Steps Creating the entity bean classes

  1. Under the EAServer java/classes directory, create the following subdirectory structure:
    Sample/Intro/Glossary
  2. Copy the following files from the html/docs/tutorial/ejb directory of your installation to the java/classes/Sample/Intro/Glossary directory:

  3. 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

Steps Importing the entity bean classes into EAServer Manager

  1. In EAServer Manager, click on the Tutorial package.
  2. Select File | New Component.
  3. In the Component wizard, select Import From EJB Class File. Click Next.
  4. In the Component wizard - CLASSPATH screen, click Next. No changes are required to the default CLASSPATH to import our classes.
  5. In the Component wizard - Import EJB Class Files screen, enter the values below:
    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)
  6. Click Finish. You see a dialog saying "All methods imported," then the Component Properties dialog box displays.
  7. Apply or confirm the following settings to the General tab fields in the Component Properties dialog box:
    Field Value
    Description Tutorial EJB entity bean
    Component Type EJB - Entity Bean
    EJB Version 2.0
    JNDI Name Tutorial/Glossary


    Leave other fields as-is, and click OK.

Define the Query stateless session bean

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.

Steps Creating the session bean classes

  1. Copy the following files from the html/docs/tutorial/ejb directory of your installation to the java/classes/Sample/Intro/Glossary directory:

  2. 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

Steps Importing the classes into EAServer Manager

  1. In EAServer Manager, click on the Tutorial package.
  2. Select File | New Component.
  3. In the Component wizard, select Import From EJB Class File.
  4. In the Component wizard - CLASSPATH screen, click Next. No changes are required to the default CLASSPATH to import our classes.
  5. In the Component wizard - Import EJB Class Files screen, enter the values below:
    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)
  6. Click Finish. You see a dialog saying "All methods imported," then the Component Properties dialog box displays.
  7. Apply or confirm the following settings to the General tab fields in the Component Properties dialog box:
    Field Value
    Description Tutorial EJB session bean
    Component Type EJB - Stateless SessionBean
    EJB Version 2.0
    JNDI Name Tutorial/Query


    Leave other fields as-is, and click OK.

Generate stubs and skeletons

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.

Steps Generating stubs and skeletons

  1. Click on the Tutorial package and select the Glossary component.
  2. Select File | Generate Stub/Skeleton.
  3. Deselect Generate Stubs.
  4. Click Next to display the skeleton generation options and configure them as follows:
    1. Select Generate Skeletons.
    2. Select Generate Skeletons on Server.
    3. Select Compile Java Skeletons.

  5. Click Next to display the advanced options and configure them as follows:
    1. For Java Version, select JDK 1.2 and Above.
    2. For Generation Strategy, choose Full.

  6. Click Finish.
  7. Repeat these steps to generate stubs and skeletons for the Query component.

Note   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.


Create the glossary database and connection cache

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.

Start the glossary database server

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.

Steps Creating and running the Windows batch file

  1. 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
  2. Start the database by running run_gloss.bat. For example, double-click this file in Windows Explorer.

Steps Creating and running the UNIX script file

  1. 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.
  2. 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
  3. Change the file permissions to allow the script to be executed. For example:
    chmod 777 run_gloss
  4. Run the script in a terminal window.

Create the connection cache and associate it with the components

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.

Steps Creating the connection cache

  1. Click on the Connection Caches folder in EAServer Manager, and select File | New Connection Cache.
  2. In the Connection Cache wizard, configure these settings and leave others at their default values:
    Tab/
    Setting
    Value
    Name Glossary
    Description Glossary database
    Database Type Sybase_ASA
    Server name NetworkProtocol=Tds:Server=localhost:Port=2640

    Note   Port numbers must match Make sure the port number used in the connection cache properties matches the port number you specify in the start script or batch file for the database server.
    User name dba
    Password sql
    Database Driver
    Type
    JDBC
    DLL or Class Name com.sybase.jdbc2.jdbc.SybConnectionPoolDataSource
  3. Verify the connection cache properties as follows:
    1. Highlight the Glossary cache and choose File | Refresh.
    2. 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.


Steps Associating the cache with the EJB components

  1. Click on the Tutorial package and select the Glossary component.
  2. Choose File | Component Properties.
  3. In the Component Properties dialog box, display the Resource Refs tab.
  4. Click Add to create a new reference in the list. Configure the values as follows:
    1. Set the Name field to jdbc/glossary .
    2. For Type, choose java.sql.Datasource.
    3. For Authentication, choose Container.

  5. Click in the Resource Link field at the bottom of the dialog, and choose Glossary from the drop-down list.
  6. Click OK to save the changes.
  7. Repeat these steps for the Query component.

Create the client application

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

Run the client application

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.

Steps Creating the Windows batch file

  1. 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 %* 
    

Steps Creating the UNIX shell script

  1. 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 $*
    
  2. Change the file permissions to allow the script to be executed. For example:
    chmod 777 runtest

Steps Running the client application

  1. 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:

  1. Creates a proxy for the Glossary entity bean's home interface, then calls the create method to populate the database with some glossary entries.
  2. 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 (C) 2004. Sybase Inc. All rights reserved.