ADF "Function Result Cache" – how to speed up calls to deterministic WebServices

0

The 11g release of the Oracle Database gave us the PL/SQL Function Result Cache. This feature can help speed up performance in calling PL/SQL functions by caching function When you invoke a function has been set up to use a result cache, the PL/SQL engine will check whether the cache already contains a result for the set of input parameter values for this current call. If so, the result is retrieved from the cache and returned to the invoker. If the result is not yet in the cache, it will be put there as the function completes execution. This of course means that for frequently called functions that take some time to execute, substantial performance gains can be achieved.

I was thinking about this feature while working with ADF and WebServices. I have been using with this nice temperature conversion service – Fahrenheit to Celsius for example – and it did the job very well. However, the call to web service took anywhere between 2 and 6 seconds – way too long for an interesting user experience. And given the fact that this web service is a deterministic one – same input will always return the same response, if I would have something like a client side result cache that I could first inspect for a non-expired result for this current input, I may be able to at least prevent some slow service calls and replace them with very fast cache accesses.

This article describes how to set up WebService calls from ADF with a crude attempt at ResultCache implementation.....
I will first create an ADF Data Control for a WebService (based on the WSDL). Using items bound to this data control, I will create a JSF page that allows users to convert temperatures. Unfortunately , we will discover that while calling the service works fine, it is way too slow. And given the fact that 80 degrees Fahrenheit today will convert to the same temperature expressed in degrees Celsius as it will be tomorrow. So calling the (slow) WebService over and over again with input parameters we already know the outcome for, we will attempt to find a smarter way of only calling the service if we really have to.

We will first implement this cache in the View layer using a managed bean between the page and the operation binding for the WebService call. Then I will try to add this Result Cache functionality at the Model level.

1. Create a WebService Data Control

Go to the new gallery and in the Web Services catagory,pick Web Service Data Control. 

 

 

 Fill in the WSDL Url in the wizard and tab out of the field. The wizard will download the WSDL and extract the Service Name.

Select the ConvertTemperature Operation on the second wizard step:

 

 

and press next until the last step is reached. An overview of the selected options is shown:

 

 Now press finish. The Data Control is created and published on the Data Control palet.

 

Now create a new JSF page. Drag and drop the Operation from the Data Control Palette, as an ADF Parameter Form:

 

The Operation Binding is created in the Page Definition file and a Form with Input Fields for the Parameters as well as a submit button to invoke the service is added to the page:

Now again drag and drop, this time the Result attribute, drop it as an output text:

The Output Text item that will display the result of calling this ConvertTemperature service is added to the page, bound to the appropriate attribute in the Page Definition file:

Now it is time to run the page and see whether we can successfully invoke the web service:

and press OK:

It takes a while, but we get the result we were after. (it may not seem that warm, but add the humidity…)

Now the page is doing what we wanted it to do, but the conversion takes quite long. And doing it the second time round for 80 degrees Fahrenheit takes just as long as the first attempt. No smart result cache around at this point.

2/ View Level implementation of Operation Result Cache

The quick and dirty way of implement a result cache could be at the view-level, as we will see next.

In this implementation, I will insert a managed bean between between the Page and the OperationBinding. The bean will only invoke the OperationBinding that then leverages the WebService Data Control to make the actual call to the Web Service if it cannot retrieve the value for the given input from the application scope result cache (another managed bean). The input fields are value bound to the managed bean, as is the button and the output text field. The managed bean will provide the input parameters when invoking the OperationBinding.

a. Create Class TemperatureConverterProxyBean

package temperatureconverter;<br /><br />import java.util.Map;<br /><br />import javax.faces.context.FacesContext;<br /><br />import javax.faces.el.ValueBinding;<br /><br />import oracle.adf.model.binding.DCBindingContainer;<br /><br />import oracle.adfinternal.view.faces.model.binding.FacesCtrlActionBinding;<br /><br />public class TemperatureConverterProxyBean {<br /><br />    String fromUnit =&quot;degreeFahrenheit&quot;;<br />    String toUnit   = &quot;degreeCelsius&quot;;<br />    Double temperature;<br />    Double convertedTemperature;<br /><br />    public TemperatureConverterProxyBean() {<br />    }<br /><br />    public void setFromUnit(String fromUnit) {<br />        this.fromUnit = fromUnit;<br />    }<br /><br />    public String getFromUnit() {<br />        return fromUnit;<br />    }<br /><br />    public void setToUnit(String toUnit) {<br />        this.toUnit = toUnit;<br />    }<br /><br />    public String getToUnit() {<br />        return toUnit;<br />    }<br /><br />    public void setTemperature(Double temperature) {<br />        this.temperature = temperature;<br />    }<br /><br />    public Double getTemperature() {<br />        return temperature;<br />    }<br />    <br />    private String getCacheKey() {<br />        StringBuffer cacheKey = new StringBuffer(&quot;convertTemperatue&quot;);<br />        cacheKey.append(&quot;;*;&quot;).append(this.fromUnit);<br />        cacheKey.append(&quot;;*;&quot;).append(this.toUnit);<br />        cacheKey.append(&quot;;*;&quot;).append(this.temperature.toString());        <br />        return cacheKey.toString();<br />    }<br />    <br />    private Object findInCache() {<br />        Map functionResultCache = (Map)getExpressionValue(&quot;#{FunctionR
esultCache}&quot;);<br />        Object cachedValue = functio
nResultCache.get(getCacheKey());<br />        return cachedValue;<br />    }<br />    <br />    public String convertTemperature() {<br />        // check if result exists in cache<br />        //note: this cache implementation is extremely simplistic!<br />        Object cachedValue = findInCache();<br />        if (cachedValue!=null) {<br />            this.convertedTemperature = (Double)cachedValue;<br />        <br />        } else {<br />        DCBindingContainer bindings = (DCBindingContainer)getExpressionValue(&quot;#{bindings}&quot;);<br />        FacesCtrlActionBinding convertTemperatureOperation = (FacesCtrlActionBinding)bindings.findCtrlBinding(&quot;ConvertTemp&quot;);<br />        convertTemperatureOperation.getParamsMap().put(&quot;Temperature&quot;, this.temperature);<br />        convertTemperatureOperation.getParamsMap().put(&quot;FromUnit&quot;, this.fromUnit);<br />        convertTemperatureOperation.getParamsMap().put(&quot;ToUnit&quot;, this.toUnit);<br />        convertTemperatureOperation.invoke();<br />        Double result = (Double)convertTemperatureOperation.getResult();<br />        storeInCache(result);<br />        // store result in the cache<br />        this.convertedTemperature = result;<br />        }<br />        return null;<br />    }<br />    <br />    public Object getExpressionValue( String el) {<br />        ValueBinding vb = FacesContext.getCurrentInstance().getApplication().createValueBinding(el);<br />        return vb.getValue(FacesContext.getCurrentInstance());<br />    }<br /><br />    public void setConvertedTemperature(Double convertedTemperature) {<br />        this.convertedTemperature = convertedTemperature;<br />    }<br /><br />    public Double getConvertedTemperature() {<br />        return convertedTemperature;<br />    }<br /><br />    private void storeInCache(Object result) {<br />        Map functionResultCache = (Map)getExpressionValue(&quot;#{FunctionResultCache}&quot;);<br />        functionResultCache.put(getCacheKey(), result);<br />            <br />    }<br />}<br />&nbsp;

The interesting bit is the convertTemperature method. This method will be invoked when the Convert button is pressed in the web page. It will then first try to find a result in the FunctionResultCache. If it finds the value, the value is returned. If it does not, it will retrieve the OperationBinding ConvertTemp from the BindingContainer, set the parameters based on the bean properties and invoke the OperationBinding. Finally it will store the result in the FunctionResultCache (call to storeInCache) and put the result on the bean property convertedTemperature.

b. Configure Managed Bean temperatureConverterProxyBean

  &lt;managed-bean&gt;<br />    &lt;managed-bean-name&gt;temperatureConverterProxyBean&lt;/managed-bean-name&gt;<br />    &lt;managed-bean-class&gt;temperatureconverter.TemperatureConverterProxyBean&lt;/managed-bean-class&gt;<br />    &lt;managed-bean-scope&gt;session&lt;/managed-bean-scope&gt;<br />  &lt;/managed-bean&gt;<br />&nbsp;

c. Bind form elements to managed bean

All page items that before were directly bound to elements in the Page Definition, are now associated with the temperatureConverterProxyBean. For example:

            &lt;af:inputText value=&quot;#{temperatureConverterProxyBean.temperature}&quot;  ...<br />            &lt;af:inputText value=&quot;#{temperatureConverterProxyBean.fromUnit}&quot; ...<br />            &lt;af:inputText value=&quot;#{temperatureConverterProxyBean.toUnit}&quot; ...<br />            &lt;af:commandButton action=&quot;#{temperatureConverterProxyBean.convertTemperature}&quot;<br />                              text=&quot;ConvertTemp&quot;<br />                              disabled=&quot;#{!bindings.ConvertTemp.enabled}&quot;/&gt;<br />            &lt;af:inputText value=&quot;#{temperatureConverterProxyBean.convertedTemperature}&quot; ...<br /><br />

d. Configure the Application Scope Result Cache

  &lt;managed-bean&gt;<br />    &lt;managed-bean-name&gt;FunctionResultCache&lt;/managed-bean-name&gt;<br />    &lt;managed-bean-class&gt;java.util.HashMap&lt;/managed-bean-class&gt;<br />    &lt;managed-bean-scope&gt;application&lt;/managed-bean-scope&gt;<br />  &lt;/managed-bean&gt;<br />&nbsp;

When we now run the page, it is simple to see what happens: the first conversion of a specific Fahrenheit temperature value is just as slow as it was before. However, when we now perform the conversion for a temperature we have converted before, it appears blazingly fast in the page, because in that case it can be taken from the cache.

 

So here we have demonstrated a simple implementation of a Service Result Cache. Simple because for example it does not have an expiration time set on cached values – now they will remain valid for as long as the application instance is running – which for the Temperature converter may be appropriate. Also simple because it is implemented very explicitly in the View – in consumer of the Service based Data Control. That is not an ideal place – we do not want to burden the View with such implementation details, especially because now we would have to implement cache support everywhere in any View project that calls the service.

3. Model based Service Result Cache

My first attempt at model based implementation was to implement an extension of the SOAPProvider class that is configured as provider in the Data Control that was created for the WebService. This sub class would have to be configured in the DataControls.jspx file, something like this:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;<br />&lt;DataControlConfigs xmlns=&quot;http://xmlns.oracle.com/adfm/configuration&quot;<br />                    version=&quot;10.1.3.41.57&quot; Package=&quot;temperatureconverter&quot;<br />                    id=&quot;DataControls&quot;&gt;<br />  &lt;AdapterDataControl id=&quot;TemperatueConversionService&quot;<br />                      FactoryClass=&quot;oracle.adf.model.adapter.DataControlFactoryImpl&quot;<br />                      ImplDef=&quot;oracle.adfinternal.model.adapter.webservice.WSDefinition&quot;<br />                      SupportsTransactions=&quot;false&quot;<br />                      SupportsSortCollection=&quot;false&quot; SupportsResetState=&quot;false&quot;<br />                      SupportsRangesize=&quot;false&quot; SupportsFindMode=&quot;false&quot;<br />                      SupportsUpdates=&quot;false&quot;<br />                      Definition=&quot;TemperatueConversionService&quot;<br />                      BeanClass=&quot;TemperatueConversionService&quot;<br />                      xmlns=&quot;http://xmlns.oracle.com/adfm/datacontrol&quot;&gt;<br />    &lt;Source&gt;<br />      &lt;!-- replaced oracle.adfinternal.model.adapter.webservice.provider.soap.SOAPProvider with temperatureconverter.CachedResultsSOAPProvider --&gt;<br />      &lt;definition xmlns=&quot;http://xmlns.oracle.com/adfm/adapter/webservice&quot;<br />                  name=&quot;TemperatueConversionService&quot; version=&quot;1.0&quot;<br />                  provider=&quot;temperatureconverter.CachedResultsSOAPProvider&quot;<br />                  wsdl=&quot;http://www.webservicex.net/ConvertTemperature.asmx?wsdl&quot;&gt;<br />        &lt;service name=&quot;ConvertTemperature&quot;<br />                 namespace=&quot;http://www.webserviceX.NET/&quot;<br />                 connection=&quot;TemperatueConversionService&quot;&gt;<br />          &lt;port name=&quot;ConvertTemperatureSoap&quot;&gt;<br />            &lt;operation name=&quot;ConvertTemp&quot;/&gt;<br />          &lt;/port&gt;<br />        &lt;/service&gt;<br />      &lt;/definition&gt;<br />    &lt;/Source&gt;<br />  &lt;/AdapterDataControl&gt;<br />&lt;/DataControlConfigs&gt; &nbsp;<br />

It was my intention to override the execute() method in this class, making it first check the cache before calling the super.execute() – and in the latter case storing the result in the cache before returning it to the  invoker. However, with

this change and a bare bones implementation of the CachedResultsSOAPProvider:

public class CachedResultsSOAPProvider extends SOAPProvider{<br />    public CachedResultsSOAPProvider() {<br />    }<br /><br />    public Object execute(String operationName, <br />                          Map params) throws AdapterException {<br />        return super.execute(operationName, params);<br />    }<br />}<br />&nbsp;

I ran into an exception that seems to be caused by a bug. The classloader is unable to load the class: SEVERE: Failed to load the provider: temperatureconverter.CachedResultsSOAPProvider.

It may be that the class should be in a specific jar-file (as is suggested by some comments in the Java sources). Indeed, it turns out that when I add the CachedResultsSOAPProvider class to the jar dc-adapters.jar in JDEV_HOME\BC4J\jlib, it does get picked up.

I now further flesh out this class:

public class CachedResultsSOAPProvider extends SOAPProvider {<br />    public CachedResultsSOAPProvider() {<br />    }<br /><br /><br />    private String getCacheKey(String operation, Map parameters) {<br />        StringBuffer cacheKey = new StringBuffer(operation);<br />        for (Object o:parameters.entrySet()) {<br />        cacheKey.append(&quot;;*;&quot;).append(o.toString());<br />        }<br />        return cacheKey.toString();<br />    }<br /><br />    public Object execute(String operationName, <br />                          Map params) throws AdapterException {<br />        Map cache = <br />            (Map)ADFContext.getCurrent().getExpressionEvaluator().evaluate(&quot;#{FunctionResultCache}&quot;);<br />            <br />        Object result = cache.get(getCacheKey(operationName, params));<br />        if (result==null) { // not found in cache, we need to make the web service call after all<br />          result = super.execute(operationName, params);<br />          cache.put(getCacheKey(operationName, params), result);<br />        }<br />        return result;<br /><br />    }<br />}<br /><br />

When we now construct a page based on this Data Control, it is completely transparent to the page that the WebService is actually provided with a Service Result Cache that will prevent unnecessary, time consuming service calls being made.
 

Conclusion

It turns out to be fairly simple with ADF to implement the concept of a Service Result Cache for Data Controls based on deterministic WebServices. By adding a special subclass of SOAPProvider to the dc-adapters.jar file and changing the provider in the DataControl definition to CachedResultsSOAPProvider, we turn a WebService Data Control into a Service Result Cache enabled Data Control. Using this Data Control is not any different from using a not-ResultCache-enabled Data Control. It can be a useful concept in managing expensive service calls, also for example when the call is not necessarily deterministic, but we can live with data that is from within a certain time window ("data less than 2 hours old is fine").

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

Comments are closed.