Hotel Reservation System using EJB and Oracle Advanced Queuing

Table Of Contents 

Overview of the application

This sample application implements the Hotel Reservation System(HRS) using two Enterprise Java Bean(EJB) components that communicate through Advanced Queuing (AQ).

It illustrates Oracle9i and Oracle9iAS OC4J features such as :
  - EJBs in the OC4J container.
  - Use of Advanced Queuing between two EJBs for asynchronous communication
  - Java Interface to Advanced Queuing.
  - Using AQ RAW message payloads to parameterize Serializable Java Objects.
  - and a servlet client to the EJBs

The two EJBs used in the sample are:

  HotelSystemBean - The controlling EJB (HotelSystemBean) acts as an agent that dispatches the reservation and 
                                       the enquiry requests in form of messages to the appropriate HotelBean.
                                       Clients only communicate with the HotelSystemBean, and not with the HotelBean directly.

  HotelBean            - Services requests for a specific Hotel, for which it has been instantiated.

This sample illustrates how HotelSystemBean can invoke services on the HotelBean, in a non-blocking manner. These two EJBs use Oracle Advanced Queuing to pass requests and responses between each other. This sample has a web-based client implemented using a servlet. The servlet uses the HotelSystemBean remote interface to implement the system. The non-blocking characteristic of the HotelSystemBean is illustrated using a shopping cart like interface in the servlet client.

When users make reservations or cancellations in any hotel, the servlet adds them to a shopping cart. Once the user is done with his bookings the servlet sends the entire batch of requests to the HotelSystemBean. The HotelSystemBean in turn will execute all the requests in this batch concurrently.

To facilitate the Advanced Queuing based communication between the HotelSystemBean and the HotelBean, two queues will be created:

 REQUEST_QUEUE    : In which the HotelSystemBean writes the requests for the HotelBeans.
 RESPONSE_QUEUE  : From which the HotelSystemBean receives the responses from the HotelBeans.

In the sample, RequestAQHandler class handles the queue interactions for the HotelSystemBean, while ServiceAQHandler class handles the queue interactions for the HotelBean.  To instantiate the HotelBeans, the sample has a class called HotelBeanAgent, in the AQEJBHotelServices package.

In the sections below, we explain the various steps from preparing the environment (which includes setting up of Queues, deploying EJBs, instantiating HotelBean instances), to running the Hotel Reservation System Client.

Required Software

The software required for running the sample are : 

  • Oracle9iAS (9.0.3.0.0) Containers for J2EE or later, downloadable from here
  •  Ant Version 1.5 downloadable from here 
  • Oracle9i Database Release 9.0.2 or later, downloadable from here
  • Oracle9i Travel Schema, downloadable from here

Notations Used

This following notations are used through out this document
 
Notation
Description
<OC4J_HOME>
Folder where OC4J is installed. For example, D:\oc4j
<J2EE_HOME>
Folder where J2EE setup is installed. For example, D:\oc4j\J2EE\HOME
<SAMPLE_HOME>
Folder where AQEJBHotelSample sample will be unzipped. For example, C:\AQEJBHotelSample
<JAVA_HOME>
Folder where JAVA is installed. For example, C:\jdk1.2
<ANT_HOME>
Folder where the ANT is installed. For example, C:\ant

Extracting the source code

Execute the following command to extract the sample application.

> jar xvf AQEJBHotelSample.jar

The above command extracts all the sample files to AQEJBHotelSample folder. Click here to view the directory structure and description of sample files.

Configuring the application

Preparing the Database

1. Ensure that 'Travel Schema' is loaded into the database. For more details about loading the Travel Schema Click here.

2. Grant all the AQ admin privileges to the user 'travel' using the following commands at SQL*plus

SQL> connect system/manager@<TNSName>
SQL> GRANT AQ_ADMINISTRATOR_ROLE TO travel IDENTIFIED BY travel;
SQL> GRANT EXECUTE ON SYS.DBMS_AQADM TO travel;
SQL> GRANT EXECUTE ON SYS.DBMS_AQ TO travel;

3. Grant the "javauserpriv" privilege to the 'travel' user, using the following command from SQL*Plus

SQL> grant javauserpriv to travel;

4. Loading Java AQ API into the database. Java AQ API jar file (aqapi.jar) can be found at <ORACLE_HOME>/rdbms/jlib/aqapi.jar.

     Note: If the aqapi.jar file is not present in a Oracle9i client installation then you need to pick up this file from an 
               Oracle9i server installation. Copy this file to the directory <ORACLE_HOME>/rdbms/jlib in the client.

     Load this jar file into the schema of the 'travel' user in the database using following command

loadjava -user travel/travel@hostname:portnumber:sid -v -f -r -t %ORACLE_HOME%\rdbms\jlib\aqapi.jar

     Here, please substitute
     hostname     : Machine Name where Database is running
     portnumber  : TNS Listener Port Number (Default 1521)
     sid                : Database SID

Deploying the application using ANT

This section describes the steps required in deploying this application to the standalone OC4J using ANT Tool.

Step 1.  SET Environment Variables

  1. Open the Command Prompt and navigate to <SAMPLE_HOME> directory
  2. Ensure that <JAVA_HOME>\bin is set in the system PATH
  3. Ensure that <ANT_HOME>\bin is set in the system PATH
  4. Ensure that <ORACLE_HOME>\bin is set in the system PATH

Step 2. Ensure that you have added the following libraries in the CLASSPATH available at the given locations. Also set the current directory in classpath..

Library/File Name Available at
jta.jar <J2EE_HOME>\lib
aqapi.jar <J2EE_HOME>\lib
translator.jar <OC4J_HOME>\sqlj\lib
runtime12ee.jar <OC4J_HOME>\sqlj\lib
ejb.jar <J2EE_HOME>\lib
jndi.jar <J2EE_HOME>\lib
oc4j.jar <J2EE_HOME>
classes12.jar <OC4J_HOME>\jdbc\lib
ant.jar <ANT_HOME>\lib
servlet.jar <J2EE_HOME>\lib
tools.jar <JAVA_HOME>\lib

Step 3. To create the SQLJ class files, navigate to <SAMPLE_HOME>\AQEJBHotelSample\src\oracle\otnsamples\AQEJBHotelSample\ejb directory. Execute the following command :

> sqlj HotelSystemSQLJ.sqlj HotelSQLJ.sqlj *.java

Step 4. Edit theconfig.properties file available at <SAMPLE_HOME>\AQEJBHotelSample\config directory. Change the JNDI parameters based on your system settings. Save the changes. The default port for ormi in Standalone OC4J is 23791. Copy <SAMPLE_HOME>\logo.gif to <J2EE_HOME>\default-web-app directory. Also, navigate to 
<SAMPLE_HOME>\AQEJBHotelSample\src\oracle\otnsamples\AQEJBHotelSample\AQEJBHotelServices directory. Change the database related information in the file ConnectionParams.java.

Step 5.  To build the .EAR (Enterprise Archive) file, we will use a tool called ant.

The ant tool compiles all the Java files and creates the .jar, .war and finally .ear files. It does so by taking input from a file called build.xml that contains all the relevant information. Build the EAR file, navigate to <SAMPLE_HOME>\AQEJBHotelSample directory, execute ant .  

<SAMPLE_HOME>ant 

    This will create the AQEJBHotelSample.ear file.

Note: Running ant will prompt for OC4J_HOME and JAVA_HOME. Provide these directory values appropriately as the ant tool will use these paths as the relative path for getting the libraries required for compiling and building the application.

Step 6. Ensure that the OC4J server is up. To start the OC4J server, open a new console, navigate to <J2EE_HOME>, use the following command:

java -jar oc4j.jar 

Having created AQEJBHotelSample.ear file, to deploy this application to Standalone OC4J using the following command, from <SAMPLE_HOME>/AQEJBHotelSample/build directory.
 
> java -jar <OC4J_HOME>\j2ee\home\admin.jar ormi://localhost:23791 admin <oc4j password> -deploy -file <SAMPLE_HOME>\AQEJBHotelSample\build\AQEJBHotelSample.ear -deploymentName AQEJBHotelSample
For example,
> java -jar D:\oc4j\j2ee\home\admin.jar ormi://localhost:23791 admin welcome -deploy -file C:\samples\AQEJBHotelSample\build\AQEJBHotelSample.ear -deploymentName AQEJBHotelSample

Step 7.  Now, bind the web application using the following command.
 
> java -jar <OC4J_HOME>\j2ee\home\admin.jar ormi://localhost:23791 admin <oc4j password> -bindWebApp AQEJBHotelSample AQEJBHotelSample-web http-web-site /AQEJBHotelSample
For example,
> java -jar D:\oc4j\j2ee\home\admin.jar ormi://localhost:23791 admin welcome -bindWebApp AQEJBHotelSample AQEJBHotelSample-web http-web-site /AQEJBHotelSample

Now, the application is deployed to the Standalone OC4J. 

Step 8.  Add data source to the application

Execute the following command, substitute database host name, database listening port and database SID appropriately.
 
> java -jar <OC4J_HOME>\j2ee\home\admin.jar ormi://localhost:23791 admin <oc4j password> -application AQEJBHotelSample-installDataSource -url jdbc:oracle:thin:@<DB_HOST>:<DB_PORT>:<DB_SID> -location jdbc/OracleCoreDS -username travel -password <schema password> -connectionDriver oracle.jdbc.driver.OracleDriver -className oracle.jdbc.pool.OracleConnectionPoolDataSource 
For example,
> java -jar D:\oc4j\j2ee\home\admin.jar ormi://localhost:23791 admin welcome -application AQEJBHotelSample -installDataSource -url jdbc:oracle:thin:@demo.oracle.com:1521:orcl -location jdbc/OracleCoreDS -username travel -password travel -connectionDriver oracle.jdbc.driver.OracleDriver -className oracle.jdbc.pool.OracleConnectionPoolDataSource 

 

Running the Servlet Client

Step 1. Before running the client application, we need to make sure that the Hotel Beans are started and are listening for requests from the HotelSystemBean. Ensure to have the following in your classpath. <SAMPLE_HOME>\AQEJBHotelSample\build\Client.jar.

Step 2. Drill down to the directory <SAMPLE_HOME>\AQEJBHotelSample\src\oracle\otnsamples\AQEJBHotelSample\AQEJBHotelServices and run the batch file, setup.bat. This batch file will create the queues and set the environment. On UNIX, run the script setup.sh

Step 3. Now run the batch file, runit.bat. This batch file will instantiate the Hotel Beans and make them listen on the queue. On UNIX, run the script runit.sh

  Note : For stopping the beans from listening on the queue, run the batch file stopit.bat on windows or run the 
             script stopit.sh on UNIX. Also, note that the sequence has to be as mentioned above. First run setup followed by runit. If needed, run stopit.

Step 4. Open your favorite browser and access the sample, using the following URL:

http://<host_name>:<port>/AQEJBHotelSample/AQEJBHotelSample  

where, <host_name> is the machine on which OC4J is running and <port> is Port to which the OC4J server listens to HTTP request . The default HTTP port is 8888. 

Example:

http://demo.oracle.com:8888/AQEJBHotelSample/AQEJBHotelSample

Note: If you get a timeout exception on your OC4J console, increase the value of the timeout attribute of the transaction-config element in the server.xml file located in <OC4J_HOME>\j2ee\home\config directory. For example, <transaction-config timeout="30000000" />

Description of sample files

Directory Filename Description
AQEJBHotelSample build.xml This XML file is used by ANT to create the application ear file
AQEJBHotelSample\src\oracle\otnsamples\AQEJBHotelSample\ejb HotelSystem.java, HotelSystemBean.java, HotelSystemHome.java, HotelSystemSQLJ.sqlj The files implementing the remote, bean and home interface for the HotelSystem Bean
Hotel.java, HotelBean.java, HotelHome.java, HotelSQLJ.sqlj The files implementing the remote,bean and home interface for the Hotel Bean
RequestAQHandler.java The class which handles all the queuing operations for the HotelSystem Bean
ServiceAQHandler.java The class which handles all the queuing operations for the Hotel Bean
AQHotelMessage.java The format of request which is sent from the HotelSystem bean to the Hotel Bean
HotelSummary.java, HotelDetails.java, RoomType.java, ReservationDetails.java Support files needed by this application
HotelSummary.java This Java file contains the member variables which represents the top level description of the hotel.
HotelDetails.java This Java file contains the member variables which represents all the attributes for the HotelDetails of any hotel.
AQEJBHotelSample\src\oracle\otnsamples\AQEJBHotelSample\web AQEJBHotelServlet.java Servlet implementation class
AQEJBHotelServletHTML.java Servlet HTML generation class
HotelSession.java For maintaining hotel reservation sessions
AQSetup.java For setting up the queues.
AQEJBHotelSample\src\oracle\otnsamples\AQEJBHotelSample\AQEJBHotelServices
ConnectionParams.java Database connection parameters.
runit.bat, setup.bat, stopit.bat, runit.sh, setup.sh, stopit.sh Scripts for setting up queues, starting them and stopping the queues.
HotelBeanAgent.java For running the hotel beans and for cleanin up.
AQEJBHotelSample\docs blaf.css Cascading stylesheet applied to Readme.html
Readme.html This file. It contains description about the sample and steps to deploy and run the sample.
AQEJBHotelSample\src\web logo.gif Image file displayed in HRS application
AQEJBHotelSample\src\web\WEB-INF web.xml This file is used to define the Web application deployment parameters and is included in the WAR file.
AQEJBHotelSample\config config.properties Contains JNDI configuration parameters.
ejb-jar.xml This file is used to define the EJB deployment
descriptors and is included in the EJB JAR file.
application.xml This file is used to define the J2EE EAR file, which contains the location of EJB JAR file, Web application WAR file and Application Client JAR file and is included in the EAR file.


Please enter your comments about this sample in the OTN Sample Code Discussion Forum.

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy