Place the cursor on this icon to display all the screenshots
in the lesson. You can also place the cursor on each individual icon in the
following steps to see only the screenshot that is associated with that step.
Web services enable programmatic access to remote content
and application functionality on the Web using industry-standard mechanisms,
without dependence on the platform, location, and implementation. The interfaces
and binding of Web services are capable of being described by standard XML.
Web services are services deployed on the middle-tier application servers. Oracle9iAS
Web services focus on exposing the J2EE components as Web services. However,
there is an increasing need to access stored procedures as well as data and
metadata through the Web services interface. Database Web services focus on
exposing functionality in the database as Web services and consuming external
Web services from the database.
The application that you deploy in this lesson illustrates
calling a Web service from within an Oracle database. It shows calling out Web
services from a Java stored procedure. The front end of this purchasing-model
application is designed with JSP pages, which display the product catalog and
enable you to browse for products. To purchase products, you provide log-in
information, with authentication performed by the Web service. This application
uses a Credit Agency Web service that maintains a repository of customers and
their credit card information. The Web service exposes two methods:
To authorize the customer
To bill the purchase amount to the customer's
credit card account
How does the application work?
1.
The products catalog is displayed, and you can browse
through the catalog.
2.
To purchase any product, you must enter your customer
ID.
3.
The Credit Agency Web service maintains a repository
of customers and their credit card information. Based on the login information
supplied in step 2, the Web service authenticates you for buying products.
a.
If you are not a valid customer, the Web
service returns INVALID_CUSTOMER.
b.
If you are a valid customer, the Web service returns
VALID_CUSTOMER. The information
about the products that you have chosen is stored in the database. The
status of the order is stored as PENDING
and processed later.
4.
A batch job runs every six minutes to process the PENDING
orders.
5.
This job invokes the Credit Agency Web service to bill
your account. The Web service is invoked with the customer information
and the amount of purchase.
a.
If the amount in your account is less than
the bill amount, the Web service returns the string INSUFFICIENT_FUNDS
and your account is restored.
b.
If the amount in your account is sufficient,
the account is charged and the message SHIPPED
is returned.
c.
If the Web service does not recognize you, it returns
INVALID_CUSTOMER.
6.
Depending on the response from the Web service, the
batch job updates the order status with the string that is returned by
the Web service.
Files in the Application
WSClientSample
build.xml
Project build file for generating deployment
file using ANT. You use this file only if you are deploying the application
to standalone OC4J.
WSClientSample.jws
Oracle9i JDeveloper workspace file
WSClientSample.jpr
Oracle9i JDeveloper project file
WSClientSample/config
application.xml
Configuration file for the Application Server
WSCreate.sql
SQL script file used to create a user and
the tables required by the application
WSProcedure.sql
SQL script used to publish the Java stored
procedure to the database and create a batch job for invoking the Java stored
procedure
Connection.properties
Properties file that has details of the
database connection
WSClientSample/src/oracle/otnsamples/wsservices
CreditAgencyService.java
Class that implements a stateless Java Web
service
ICreditAgencyService.java
Web service interface that lists the methods
exposed as Web service
CreditAgencyService.wsdl
WSDL file for the Web service
ConnectionManager.java
A Java bean used for managing database connection
WSClientSample/src/oracle/otnsamples/wsclient
CreditAgencyServiceClient.java
Stored procedure that invokes the Web service
for validation
CreditAgencyServiceStub.java
Client-side stub file for the Web service;
generated by Oracle9i JDeveloper
StoresBean.java
Java bean used by the JSP to handle database
operations
OrdersInfo.java
Value object used to pass order information
between the JSP and the Java bean
ProductsInfo.java
Value object used to pass product information
between the JSP and the Java bean
WSClientSample/webroot
Products.jsp
Displays the product catalog and accepts
orders
ViewOrders.jsp
Retrieves order status for a specified order
ID
ShipOrders.jsp
Retrieves order information from the database
for the shipping service
From your terminal window, execute the following commands:
cd /home/oracle/wkdir/WSClientSample/config
sqlplus /nolog
connect / as sysdba
@WSCreate
exit
This script prompts you to enter a password for the
user system and the tns
name. Enter oracle
and orcl, respectively.
3.
The Web service client is a Java stored procedure. This
client application requires some .jar files to be present in the database
to invoke the external Web service. Edit the load.sh
file found in /home/oracle/wkdir
and modify the path of the .jar files to reflect the correct path on your
machine.
From your terminal window, run the
following commands:
cd /home/oracle/wkdir
chmod 777 load.sh
./load.sh
4.
Check the Web service end point in the client. Open
the following file:/home/oracle/wkdir/WSClientSample/src/oracle
/otnsamples/wsclient/CreditAgencyServiceStub.java
Verify that the string variable endpoint
points to your Web service end point as follows:
public String endpoint = "http://localhost:8988/wsclient/CreditAgencyService";
5.
Load the Web service client and stub file to the database.
Switch back to your terminal window and issue the following commands:
cd /home/oracle/wkdir/WSClientSample/src/oracle/otnsamples/wsclient
loadjava -thin -user ws/ws@localhost:1521:orcl
CreditAgencyServiceStub.java CreditAgencyServiceClient.java
6.
Publish the Web service client and create a database
job that can invoke the Web service. From your terminal window, execute
the following commands:
cd /home/oracle/wkdir/WSClientSample/config
sqlplus /nolog
connect / as sysdba;
@WSProcedure
exit
When prompted for the tnsname
value, type orcl.
This SQL script publishes the stored procedure created in the previous step
to the database and creates a job that invokes CreditAgencyServiceClient.java
once every six minutes.
7.
Open the following file: /home/oracle/wkdir/WSClientSample/config/
Connection.properties
The USERS table
in the ws schema is
populated with two usernames: otn-user
and oracle-user. The credit limit
for otn-user is $2,000, and the
credit limit for oracle-user
is $3,000. You now run the application and place an order by otn-user
for under $2,000 and another order that exceeds the credit limit of $2,000.
Perform the following steps:
1.
Right-click CreditAgencyServiceWebservice and select
Regenerate Web Service.
2.
Click Yes to close the warning window.
3.
Right-click the project WSClientSample.jpr and
select Run.
4.
When you run the application, you see this product selection
screen in the default browser. Enter otn-user
as the customer ID, and then choose some of the products. The default
quantity is 1 if you do not specify a quantity. Then click Place Order.
Click Continue when the security warning window appears
5.
Observe that the order is placed successfully with order
ID 1. Click View Order Status.
6.
Enter 1
as the order ID and click Get Order Details. Click Continue
if you see the security warning.
7.
You have successfully placed the order. As the vendor,
you now go to the shipment module to check the pending orders. In the
URL, change ViewOrders.jsp
to ShipOrders.jsp.
The URL is now the following:
http://host:8988/wsclient/ShipOrders.jsp
8.
Select your order for shipping and then click Ship
Order.
9.
Observe that the status of the order changes from PENDING
to IN_SHIPPING. The shipping
process is automated and configured to run every six minutes. The process
scans the database for orders that are ready to be shipped. Click Back
to Orders Page.
10.
You see that you have no more orders pending. Based
on the amount of purchase and the credit balance of the customer, the
status of the order is updated when the batch job is run next time. Remember
that the batch job is designed to run every six minutes.
11.
Open a SQL*Plus session and execute the following
command:
connect ws/ws
Determine the next time that the job runs by issuing
the following query:
SELECT last_date,
last_sec, next_date, next_sec
FROM user_jobs
WHERE what like 'ws_client%';
12.
Switch back to your browser and check the order status.
In the location bar, replace ShipOrders.jsp
with ViewOrders.jsp.
Enter 1 as the order
ID, and then click Get Order Details.
13.
Observe that the status changes from IN_SHIPPING
to SHIPPED. If it does
not change, the batch job has not yet run. Try again in a few minutes
until the status changes.
14.
You now place another order so that the total amount
of the order exceeds the credit limit. Change the URL to http://localhost:8988/wsclient/Products.jsp.
Enter otn-user as
the customer ID, and then choose three of the products with a quantity
of 10 for each.
Click Place Order.
15.
Your order is placed. Change the URL to http://localhost:8988/wsclient/ShipOrders.jsp.
16.
Select your order, and then click Ship Order.
17.
Your order is shipped. You must wait for the batch job
to execute again.
18.
Change the URL to http://localhost:8988/wsclient/ViewOrders.jsp.
Enter 2 as the order
ID, and then click Get Order Details.
19.
You see that there are insufficient funds because the
credit limit is now exceeded.
Place the cursor on this icon to hide all screenshots.