In this section, you walk through the Workshop VDE performing a simple task of creating an asynchronous Web service that checks an applicant 's credit history (used throughout discussions in this chapter) by using a Database control and an EJB control. The completed source files for this task are shown in Listing 32.6. Listing 32.6 Source Code for the creditCheck Web Service Investigate.jws package creditCheck; import Weblogic.jws.control.JwsContext; public class Investigate { /** * @jws:control */ private ValidateCreditControl validateCreditEJB; /** * @jws:control */ private BankruptciesDatabaseControl bankruptciesDB; public Callback callback; public interface Callback { /** * @jws:conversation phase="finish" * @jws:message-buffer enable="true" */ public void onCreditReportDone(Applicant applicant, String responseMsg); } /** @jws:context */ JwsContext context; public static class Applicant implements java.io.Serializable { public String taxID; public String firstName; public String lastName; public boolean currentlyBankrupt; public int availableCCCredit; public int creditScore; public String approvalLevel; public Applicant(String taxID) { this.taxID = taxID; } public Applicant() {} } Applicant m_currentApplicant = new Applicant(); /** * @jws:operation * @jws:conversation phase="start" * @jws:message-buffer enable="true" * @jws:parameter-xml xml-map:: * <requestCreditReportAsync xmlns="http://www.openuri.org/"> * <taxID>{taxID}</taxID> * <name>*</ name > * <address>*</address> * </requestCreditReportAsync> * * :: */ public void requestCreditReportAsync(String taxID) throws java.rmi.RemoteException, java.sql.SQLException { m_currentApplicant.taxID = taxID; String summary = null; Applicant dbApplicant = bankruptciesDB.checkForBankruptcies(taxID); if (dbApplicant != null) { m_currentApplicant = dbApplicant; if (m_currentApplicant.currentlyBankrupt == true) summary = "Currently Bankrupt."; else summary = "Not currently bankrupt."; } else { m_currentApplicant.approvalLevel = validateCreditEJB.validate(2000); summary = "No bankruptcy information found."; } callback.onCreditReportDone(m_currentApplicant, summary); } } Listing 32.7 shows the source code for the creditCheck Web service's controls. Listing 32.7 Source Code for the Web Service's Controls [View full width] BankruptciesDatabaseControl.ctrl package creditCheck; import weblogic.jws.*; import weblogic.jws.control.*; import java.sql.SQLException; /** * Defines a new database control. * * The @jws:connection tag indicates which WebLogic datasource will be used by * this database control. Please change this to suit your needs. You can see a * list of available data sources by going to the WebLogic console in a browser * (typically http://localhost:7001/console) and clicking Services, JDBC, * Data Sources. * * @jws:connection data-source-jndi-name="cgSampleDataSource" */ public interface BankruptciesDatabaseControl extends DatabaseControl { // Sample database function. Uncomment to use // static public class Customer // { // public int id; // public String name; // } // // /** // * @jws:sql statement="SELECT ID, NAME FROM CUSTOMERS WHERE ID = {id}" // */ // Customer findCustomer(int id); // Add "throws SQLException" to request that SQLExeptions be thrown on errors. /** * @jws:sql statement:: * SELECT TAXID, FIRSTNAME, LASTNAME, CURRENTLYBANKRUPT FROM BANKRUPTCIES WHERE TAXID={taxID} * :: */ public Investigate.Applicant checkForBankruptcies(String taxID); } ValidateCreditControl.ctrl package creditCheck; import weblogic.jws.*; import weblogic.jws.control.*; /** * @jws:ejb home-jndi-name="financial.ValidateCredit" * @editor- info :ejb home="ValidateCreditBean.jar" * bean="ValidateCreditBean.jar" */ public interface ValidateCreditControl extends financial.ValidateCreditHome, // home interface financial.ValidateCredit, // bean interface weblogic.jws.control.SessionEJBControl // control interface { } Of course, because you're using the Workshop VDE to create this Web service, you are not going to be typing all this code in by hand. Workshop generates all the infrastructure and plumbing code, so you should concern yourself only with business logic. Here are the steps and specifications:
PreliminariesTo start WebLogic Workshop, click Start, Programs, WebLogic Platform 7.0, WebLogic Workshop. Workshop ships with a project called Sample, which you'll use to build the tutorial Web service. Choose File, Open Project to open the Open Project dialog box. Select the Samples item in this dialog box. To do this tutorial, you need to start WebLogic Server by choosing Tools, Start WebLogic Server. It takes some time for the server to come up, but when it does, you should see the green light icon on the bottom status bar of Workshop, which indicates that the server is running. Create the creditCheck Web ServiceTo create the creditCheck Web service, follow these steps:
If you have not done so already, save your work by clicking the Save icon in the toolbar or choosing File, Save from the menu bar. Create the Bankruptcies Database ControlTo create the Bankruptcies Database control, follow these steps:
Create the Validate Credit EJB ControlTo create the Validate Credit control, follow these steps:
Make Investigate.jws Call These ControlsThe Database control is finished, but how does Investigate.jws know when or where to invoke it? You need to tell it by adding business logic. Back in the Design View pane, click the requestCreditReportAsync method (to the left of the Investigate.jws icon), which takes you to the Source View pane for that method. Type in the following code: public void requestCreditReportAsync(String taxID) throws java.rmi.RemoteException, java.sql.SQLException { m_currentApplicant.taxID = taxID; String summary = null; Applicant dbApplicant = bankruptciesDB.checkForBankruptcies(taxID); if (dbApplicant != null) { m_currentApplicant = dbApplicant; if (m_currentApplicant.currentlyBankrupt == true) summary = "Currently Bankrupt."; else summary = "Not currently bankrupt."; } else { m_currentApplicant.approvalLevel = validateCreditEJB.validate(2000); summary = "No bankruptcy information found."; } callback.onCreditReportDone(m_currentApplicant, summary); } This code basically checks the BANKRUPTCIES database for history on the applicant. It invokes the EJB control to validate the applicant's credit score (hard-coded at 2000), and then calls back the client. Remember that the client had execution control before either control was invoked. The callback with conversation implements a true asynchronous communication model. If you look at the Source View pane for the Investigate.jws , BankruptciesDatabaseControl.ctrl , and ValidateCreditControl.ctrl files, they should all match the source files shown in Listing 32.6 and Listing 32.7. Test the ServiceWorkshop enables you to write and run your code using the Test Harness pane, a browser-based tool through which you can call your Web service's methods. To start it, click the Start icon on the toolbar. Click the Stop button to halt execution of your Web service. Clicking the Start icon causes Workshop to build your project, checking for errors along the way. It then launches your Web browser to display a page you can use to test the service method with possible values. Figure 32.36 shows the Test Harness pane for the Investigate Web service. Figure 32.36. The Test Harness pane for Investigate.jws .
Because an incoming XML Map is in place, the only way to specify input is through XML. Otherwise, the Test Harness pane can provide input fields for each parameter of the method being invoked. Try entering some meaningful values. The shipped BANKRUPTCIES database has values for known users, such as 111111111, 222222222, and so forth. In Figure 32.37, a taxID of 222222222 has been substituted for the placeholder string Value_taxID . Figure 32.37. Results of invoking the requestCreditReportAsync method.
Clicking the requestCreditReportAsync button sends a SOAP message to the Investigate Web service, and the results window in Figure 32.37 is displayed, where you actually see the SOAP request and response messages. Because this method starts a conversation, control immediately returns to the caller without any real return value, while the request is still being processed (notice the "Processing Request" label at the bottom). When the callback has finished, as shown in the upper-left Message Log section in Figure 32.37, you can click on the callback (in this case, onCreditReportDone ) and get the real results that came back in the callback (see Figure 32.38). Figure 32.38. Results returned in the client callback.
It all works. |