Sometimes a particular data source is either temporarily unavailable or very slow to send a response back to a consuming application. In such cases, you need to be able to run an alternative data source. ALDSP enables you create an alternative data source that will be called if the primary data source does not respond within a specified time frame.
After completing this lesson, you will be able to:
Enabling an alternative data source is implemented by calling the fn-bea:timeout() function. The syntax for the function is as follows:
fn-bea:timeout($seq as item()*, $millis as xs:int, $alt as item()*) as item()*
To implement this functionality, the return types of both the primary and alternative expression should be available when the project is compiled. This ensures that the function's return type is correctly inferred. In other words, the source metadata must be available at compile time, because the alternative source function provides only runtime failover capability.
You will import a slow Web service into your application, thereby enabling the demonstration of configuring alternatives for unavailable data sources.
http://localhost:7001/CreditWS/NewCreditReport.jws?WSDL=
Note: | Do not select NewLookCredit as a side-effect procedure. |
Because the CreditWS Web service is slow, you need to configure an alternative source to obtain the credit rating information in a timely manner.
declare namespace ws3="ld:DataServices/WebServices/NewLookupCreditResponse";
declare namespace ws4 = "http://www.openuri.org/";
{
for $rating in ws1:getCreditRating(
<ws2:getCreditRating>
<ws2:customer_id>{data($CUSTOMER/CUSTOMER_ID)}</ws2:customer_id>
</ws2:getCreditRating> )
return
<creditrating>
<rating>{data($rating/ws2:getCreditRatingResult/ws2:Rating)}</rating>
<customer_id>{data($rating/ws2:getCreditRatingResult/ws2:Customer_id)}</customer_id>
</creditrating>
}
{
<creditrating>
<rating>
{
fn-bea:timeout(
data(
ws3:NewLookupCredit(
<ws4:NewLookupCredit>
<ws4:cid>{data($CUSTOMER/CUSTOMER_ID)}</ws4:cid>
</ws4:NewLookupCredit>
)/ws4:NewLookupCreditResult/ws4:CreditCode
)
, 2000,
data(
ws1:getCreditRating(
<ws2:getCreditRating>
<ws2:customer_id>{data($CUSTOMER/CUSTOMER_ID)}</ws2:customer_id>
</ws2:getCreditRating> )/ws2:getCreditRatingResult/ws2:Rating)
)
}
</rating>
<customer_id>{data($CUSTOMER/CUSTOMER_ID)}</customer_id>
</creditrating>
}
Testing getAllCustomers() function will let you confirm that the query is retrieving data from the alternative source, rather than the CreditWS.
Open the Output window, scroll to the bottom, and then confirm that the CreditWS Web service times out and then the CreditRating Web service is called. The output should display as shown in Figure 28-3.
The invocation of the first Web service NewLookupCreditResponse fails because the thread times out. Because this Web service has failed it will not be invoked again. Instead, the alternate Web service is invoked.
In this lesson, you learned how to: