I’m always trying to think of different ways Coherence can benefit when developing applications. One of the more unusual and useful ways my colleague and I came up with was to use Coherence, rather than a database, to store reference data for ADF based applications. Doing this means you can get extremely fast, in memory access, to the data you need to display in your ADF apps, therefore increasing performance and scalability.

Oracle ADF is an Application Development Framework from Oracle, which allows you to develop web and mobile-based applications quickly and easily using a rich set of components. Part of ADF is the concept of a data control, which can be attached to components to display or process data. Data controls can be based upon sources such as web services, EJB’s, RESTful web services, JBO/BC4J, etc.

To see how this would help, consider the example of an ADF application querying country codes from the database. If you had these in the DB, you could use in-built controls to access that data via a BC4J, which is fine, but every ADF page that accesses this data will incur overhead of DB access, O/R mapping, etc. Image it all being in memory and accessible very quickly providing a more scalable platform for heavy reference data access. Also, what if you wanted a fast Google-like autosuggest? Getting data for this from the database as you type would not be a good idea!

I’m going to assume a bit of ADF and Java knowledge here for brevity. For my example I’m using JDeveloper 11.1.1.6.

Creating the ADF Project
First create a new Application and choose Fusion Web Applications (ADF). Take the defaults and finish.

Now we’re going to create a JavaBean class to hold our country details. On the empty Model project, right-click and select New. Choose Java->Java Class. Name this Country and click on OK.

Add the following attributes:

//
private String shortCode;
private String description;

We then use JDeveloper to automatically generate the required code for the JavaBean. Under the Source menu, choose Generate Accessors, Generate equals() and hashCode(), and Generate Constructors from Fields. Choose both fields. Also make sure this object implements java.io.Serializable for Coherence to store. For sorting, implement the following inner class:

We will also need a class to start our cache server – StartCacheServer.java. In this case I’ve just referenced our cache through Cachefactory.getCache() (which will start a cache server by default) and loaded the data in directly in one operation. There are better ways to do this, but just taking the easier path today!. Create this in the Model project.

Now we will create a Data Control. Select the CountryLister.java file and right-click and choose Create Data Control. Once this is completed it should look something like this.

Use the Data Control to create a Country List

Next we are going to create a page that will use this data control. In Application Navigator, select the View Controller project, right-click and choose New -> Web Tier -> JSF -> JSF Page. Name it as CountryList and ensure you check Create as XML Document.

Drag a Panel Stretch Layout from the Layout panel from the component palette. In the Data Controls panel, expand CountryLister and then expand findAllCountries(). Drag Country to the center facet and choose Table->ADF Read Only Table. Check Enable Sorting. Save All.

Now before we run, we need to startup our cache server. In your model project choose StartCacheServer.java, right-click and Run. Once that starts, select CountryList.jspx, right-click and Run.

What you should see is something like the following. I know, not amazing looking (thats for you to make nicer!) but every time you access this data control data is coming from Coherence so its fast! (see the timings, without indexes in the JDeveloper Log)
Have a think how you could apply this data control to other components such as drop down boxes, etc.

If you get a message like “No storage-enabled nodes exist” when you try to run, it is likely that your machine doesn’t support multicast. You can un-comment the following in your StartCacheServer.java and static code in CountryLister.java to use Well Known Addresses (WKA).

That’s it! Now when we run this, as we type we can see suggestions of countries that match. I’m using a LikeFilter in Coherence and putting % around the values I type, but you could as easily only look for values that start with what you type.

In the example above, we have used storage-disabled clients, which allow us to separate the data access from the cache servers. It enables us to scale out the cache servers to provide more data capacity and processing capacity. The nice thing about Coherence is that when you are querying the data, it will do this in parallel across all available cache servers!

There are many extra things to consider before putting this into production including (but not limited to):

Adding indexes to improve performance (from the 20ms to get countries as I typed!),

Implementing near caching getting even faster in memory access speed;

Using a proper cache server and cache config; and

Using EvolvablePortableObjects for fast serialization and allowing schema evolution of objects.

Thanks to Andrew Rosson for his original thoughts around this and Chris Muir for some ADF help.

3 Responses to Using Coherence for Reference Data in ADF

‘No storage-enabled nodes exist” should not be related to unicast or multicast. Coherence works fine on both, multicast is recommended as Coherence relies heavily on network protocols, especially for large clusters.
You will get this error because you set the localstorage to false for the cluster node. For this kind of implementation, where you want the cluster node to act as cache nodes, you would want the localstorage to be set to true and for nodes that you want to use as TCP extend nodes, which don’t store data, you want to set this to false.

Thanks for your comment. You are right it what you say, but i’ve seen on many machines, mainly laptop’s in workshops, that multicast is disabled by default. (Particularly on some corporate machines with standard environment installed – SOE).
If you try to create a cluster using multicast in these situations, the processes don’t see each other, from the cluster perspective, and you can get ‘no storage-enabled nodes exist’.
Tim

If I remember it right from my experience with Coherence last year, there is a multicast tester script that Coherence provides out of box. This can be used to test if multicast is enabled on the network.
Also, isn’t it that multicast/unicast come into play when there are cluster nodes distributed across multiple physical machines across the network ? On a single machine, it would not matter much (or at all), just that the cluster will not be machine-safe.
On this post, there is another thing to note – it is good that you are using a POJO class, wouldn’t it be better to use a dynamically created ViewObject with a DB query, accompanied with read-through caching.
Thanks
Tapash

Blogroll

Copyright (c) 2013 Tim Middleton and other contributors. All Rights Reserved. The views expressed in this blog are our own and do not necessarily reflect the views of Oracle Corporation. All content is provided on an 'as is' basis, without warranties or conditions of any kind, either express or implied, including, without limitation, any warranties or conditions of title, non-infringement, merchantability, or fitness for a particular purpose. You are solely responsible for determining the appropriateness of using or redistributing and assume any risks.