Caching in a JEE : don’t write it yourself, use LoadingCache from Google Guava libraries.

Caching data is something you use in almost every JEE project. Most of the time it’s pretty simple : put your data in a .properties file and use a PropertyManager to fetch the data.

But that’s not very flexible and manageable. Updating the values means, updating your property file, repackaging the ear file, and redeploying, and only developers can update the data.

Putting the data in JNDI entries, and using JNDI lookups may solve the problem of redeploying, but if you got a few 100 properties, it’s still not very manageable.

Most of the times, JNDI entries are entered via some application server console which, in a production environment, is not accessible for your users who need to manage this data.

So lets put the data that needs to be cached in a database, or make it accessible via a web service. That would be ideal. You can write your own application on it, and have the data managed by your users.

But that means that you have to write your own, thread safe, caching algorithms.

No big deal if the data only changes once every 10 years, but refreshing it on a time or size basis, makes the whole thing a bit more complicated. And that’s where the great LoadingCache class from the Google Guava library comes in.

What are the Guave libraries ? Well, here’s how they describe it : ‘The Guava project contains several of Google’s core libraries that we rely on in our Java-based projects: collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, and so forth.’

Now for caching, the Guava LoadingCache class caches data in a key-object map, and lets you define a cache refreshing mechanism, all done in a thread safe manner.

So lets show a small  example and explain how it works. Suppose your cache contains a list of products that are on sale for 1 day. Depending on the no. of sold products, the price will increase during that day. This means that the cache should be updated every few seconds, to update the price, and after 1 day, the whole cache should be refreshed with new products. Suppose that price setting and product selections are in the database, updated by some back-end application, and we need the new data in our frontend application and we want to cache it.

All this can be done with this simple class :

import java.util.concurrent.TimeUnit;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import be.iadvise.dao.DatabaseDAO;
import be.iadvise.entities.Product;
import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.MoreExecutors;

@Singleton
public class ProductCache {

@EJB
 DatabaseDAO databaseDAO;
 private static final Integer REFRESH_PRODUCT_AFTER_5_SECONDS = 5;
 private static final Integer EXPIRE_PRODUCT_AFTER_1_DAY = 1;
 private final LoadingCache<String, Optional<Product>> cache;

 public ProductCache() {
      cache = CacheBuilder.newBuilder()
           .expireAfterWrite(EXPIRE_PRODUCT_AFTER_1_DAY, TimeUnit.DAYS)
           .refreshAfterWrite(REFRESH_PRODUCT_AFTER_5_SECONDS, TimeUnit.SECONDS)
           .build( new CacheLoader<String, Optional<Product>>() {
                 @Override
                 public Optional<Product> load( String productId ) throws Exception {
                     return loadCache(productId);
                 }
           }
     );
 }

 public Optional<Product> getEntry( String productId ) {
      return cache.getUnchecked( productId );
 }

 private Optional<Product> loadCache(String productId) {
      Product product = databaseDAO.getProduct(productId);
      return Optional.fromNullable(product);
 }
}

Explanation

  1. In the constructor, we build the cache using the CacheLoader, defining the refresh mechanism. In our example we define 2 rules :
    - expireAfterWrite : after this period, the object will be evicted from the cache, and replaced the next time it is requested.
    - refreshAfterWrite : after this period, the object will be refreshed using the loadCache method. (with our new price)
  2. getEntry(String productId) method : will return the object with given key. So in this example, the cache is not loaded all at once, but only when the object is needed.
  3. loadCache(String productId) : will load the product and add it to the cache, or replace it if it’s already there and needs to be refreshed.

That’s all there is to it !

A few other remarks on the code

  1. There are other mechanism like expire/refresh AfterRead, which will time only from the last read, or let the cache hold only a certain no. of objects,…
  2. This code is implemented as a session bean. To make a singleton, I’m using EJB 3 annotation @Singleton. Because I only want 1 cache in my application
  3. My DAO is also injected using the @EJB annotation
  4. The LoadingCache does not want any null objects in the map (returns an error), so I’m using the Guava ‘Optional’ class here. This is basically a wrapper for my object and used to check if there is a value for my product id or not. So if someone uses a wrong productId, my cache will indicate that there is no product for this id, and I don’t have to go to the database every time it is requested.

To conclude:

Programming a caching mechanism in a JEE environment is not as trivial as it may seem. Testing it in a multithreaded environment is even more difficult. The caching classes of Guave gives you ready-to-use solution. It’s programmed, tested and used by Google, so I think we can say in all honesty : this is proven technology.

A remark on deploying on Weblogic 12c:

Weblogic also uses the Guava libraries, but an older version. This causes following error on deployment :

java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor()

Lcom/google/common/util/concurrent/ListeningExecutorService;

Adding the following to your weblogic-application.xml will solve the problem (force weblogic to use your deployed Guava libraries :

<wls:prefer-application-packages>
<wls:package-name>
com.google.common.*
</wls:package-name>
<wls:prefer-application-packages>

Guava libraries run under Apache license, more info/download can be found on :

https://code.google.com/p/guava-libraries/

Have fun !

Oracle Service Bus : Agility in Action

Yesterday I followed a session regarding OSB given by Jeff Davis, it was an interesting session with a lot of demo’s regarding using business services and proxy services.

Topics that we’re interesting to me during the session and I would like to share with the community:

  • Coherence ships with OSB/Weblogic which gives you the possiblity to cache service results to speed up performance. As Jeff mentioned a case could be that your users need to get the latest financial results each day which is a long running business process. This process can be launched asynchronously when the user is logged on in the background. The result of the process is then put in the service cache so when the user needs the information it’s shown to him without delay. The Service Result Caching functionality is integrated within OSB 11G, Release 1.
  • OSB 11g, Release 1 is also fully integrated with Enterprise Repository which gives you the possiblity to index all your existing services using your metadata such as XML, XSD, WSDL, … Using Enterprise Repository the impact of changes made to individual services is clear which makes your release management process a lot easier! Make sure to check out the features and functionality delivered by the Enterprise Repository to be able to govern your services throughout the enterprise.
  • OSB12C (C stands for Cloud) is the next upcoming release of OSB which holds a lot of new interesting functionality such as templating/prototyping services, development is integrated within Jdeveloper, … The fact that the development will be integrated within Jdeveloper gives your development team the possibility to use one and only one IDE to have E2E development from database to business components, to services such as bpel, osb, external services, business rules untill the end-user interface

During the demo Jeff showed how you can expose functionality of an EJB 3.0/2.1 module as a service using business services and proxy services. You can transform from an EJB 3.0 business service to an EJB 2.1 Proxy Service using XQuery and the JEJB protocol.

Forms 11g javascript integration: Call others

Forms 11g holds a lot of interesting new features focused on event-driven architecture, one of these is javascript integration. There are two ways of using javascript with Forms 11g: “call others” and “let others call you”.

Javascript can call code in Forms(“Let others call you”) using the new forms trigger “when-custom-javacript-event”.

This post is going to show you the first one: “call others”, in other words call javascript from your Oracle Forms application.

During the Forms Modernization Seminar I showed a google map that could be manipulated from an Oracle Form. It’s an easy implementation with only a few lines of code(most of the javascript is taken from the api examples on the google code site: http://code.google.com/apis/maps/).

  • Build a little form with one (control) block, one text field(to enter an address) and one button(to call the javascript code).
  • Next step is to create an HTML-page to display the form.

This code puts the form(in an iframe) and the map side by side:
(Click to enlarge)

And it will look like this:

  • The javascript that will be called is put in another file google.js:

  • The only thing to do is creating a “when-button-pressed” trigger in forms to call the javascript function showAddress.
    This is done by a new built-in procedure web.javascript_eval_expr:
  • Copy the HTML and javascript file to the following directory:
    <middleware_home>\user_projects\domains\<domain>\servers\WLS_FORMS\tmp\_WL_user\formsapp_11.1.1\e18uoi\war\
  • Create a new configuration using Enterprise Manager:

  • Make sure the parameter EnableJavascriptEvent is set to “true’ in your configuration!

And the working demo…

OFM 11g Launch – The fusion ages kicked in

Oracle Fusion Middleware 11g has finally arrived and it’ll give as well developers as customers the possibility to implement bleeding edge technology throughout the Oracle Stack.

Just looking at the different features and functionalities that have been build into the stack is amazing and most of all, you can’t get round fusion anymore.

So let’s have a look at what fusion has to offer us (it’ll swipe you of your feet!):

  1. The BEA Integration Milestone:
    1. Weblogic Server will be the underlying backbone of all fusion applications, going from classic environments such as Forms, Reports to the web 2.0 environments such as Webcenter and ADF. What will this milestone offer us:
      1. Flexibility and agility by declarity of design
      2. Take decisions in real time by integrating analytics within the transactional context
      3. Power for the end-user so business and it can interact and collaborate whilst enabling end-users with a flexible, personalized and easy-to-use business oriented environment
      4. Ease-of-upgrade using automated tools to upgrade existing OC4J applications towards the new Web Logic Server
        1. Automated upgrade for soa components, webcenter applications, forms and reports applications
        2. Smart Upgrade for OC4J JEE Applications
          1. Descriptor analysis to report on recommended findings
          2. Upgrade all the pieces of configuration necessary (pick and choose)
      5. Consolidation and virtualization of the data-center to enable companies to maximize return on investment using the data grid
  • Budget can go to development instead of administration and management
  • Services-based delivery that will enable companies to re-use services throughout the enterprise using a standards based approach giving customers and partners the ability to work within a shared-service infrastructure
  • Unified management and monitoring capabilities within one environment and a rich user interface
  1. Enterprise Manager will be the window on the data-center!
  2. Fusion Middleware Control gives you the ability to manage multi-domains, policies, alerts, SLA and much more in a single environment.
  1. The Grid: A new model for efficient resource utilization:
    1. The Coherence in-memory data grid gives you the ability to paralyze computation across the grid
    2. Inherently use the grid for as well cpu as memory
    3. State-aware continuous availability for service infrastructure
    4. Software-only solution, no hardware costs necessary
    5. Off-heap storage = Significantly reduced coherence nodes
  2. Unified Security Model
    1. Using the Oracle Platform Security Services you can define a generic and integrated security model throughout the Oracle Stack = 1 Common Security Layer

So now we have the backbone of our infrastructure laid out, now it’s time to have a look at the different components that will offer these functionalities.

  1. SCA – Service Component Architecture:
    1. Composites will be key throughout this stack which will give you the possibility to deliver and reuse any service that has been built Enterprise-wide. When you’re talking about data services, business services, web services or even mediator services, it’ll all be managed within one environment. In other words ease of development, deployment, management using a unified approach for as well business and IT.
      1. These composite services can be re-used in different ways throughout the business and it lifecycle going from business dashboards, to side-by-side versioning on composite level, to global-policy management, to unified management and monitoring of each aspect and transactions within and E2E-context and much more.
    2. Integration of BEA products :
      1. OESB will become the mediator component within the Service Component Architecture used for basic routing between components
      2. OSB will become the new service bus that’ll offer routing, transformation, chaining of services and much more within SCA
    3. B2B, BAM, BPEL, Human Workflow, OSB can be hooked up into the SCA cloud using Adapter technology to enable business and it to focus on different aspects of the organization whilst holding the needed agility and flexibility throughout the entire development lifecycle.
    4. A unified BPM Platform which is fully BPMN 2.0 compliant and leverages BPM capabilities using a single environment for as well business as IT, namely Jdeveloper
      1. Integration of BPM Studio within Jdeveloper
      2. Share processes and services using the BPM Process Composer and business Catalog
      3. Easy-to-upgrade using the migration path provided when going from 10g to 11g
  2. Jdeveloper – The development environment for as well business and IT, a unique proposition
  • Integration of BPM Studio
  • Composite based development using Adapter-technology
  • ADF which will put a face on each application that needs to be delivered to end-users
    • ADF render kits for Ajax and Flash
    • ADF render kits to .png (talking bandwidth and performance into account)
    • ADF render kits for usability purposes (e.g. Screen reader-functionality)
  • Active Data Services Push
    • Data changes are pushed into the UI instead of needing to pull for data changes, using Coherence Cache Listeners and asynchronous calls
  • Application Lifecycle Management
    • Pluggable interface to quickly access documentation, bugs, reports etc. used throughout the stack
  • ADF Desktop Integration
    • Build transactional spreadsheets using the existing binding layer of ADF

In other words SCA Suite, Webcenter Suite, Forms, Reports and Fusion Apps will use the same infrastructure which will enable the usage of consistent admin and management tools whilst leveraging capabilities of the infrastructure and database throughout the entire stack.

My 2 cents … Fusion has become a reality with unlimited functionality and features!
Working within a forms, JEE, DB or Apps environment, everyone will be using the same technologies throughout the stack … The fusion ages kicked in!

Oracle Siebel and Webcenter Integration

One of the sessions I’ve attended at Open World in regards of a customer case we’re working on, was regarding Siebel and Webcenter integration.

The presentation was to high-level which wasn’t that interesting after all, but I learned more about the packaging strategies for Webcenter.

Of course I got a bit more information regarding the release date of Webcenter … and to make sure you read this post fully, I will put it at the very end ;o))


What have I noted regarding this presentation:

  • Webcenter, what does it offer? Centralize Applications and embed collaboration within a context specific manner. In other words: you’re showing documents which were published within UCM and the user-account is shown, you can check if the users’ presence by adding the presense services in the portlet, …
  • Webcenter 11g comprises of: Webcenter Composer / Webcenter Services / Webcenter Framework / Enterprise Extensions
  • Webcenter Composer = holds all components regarding collaboration (spaces, anywhere, interaction, …)
  • Webcenter Services = holds all needed services to spice up your portal environment (mashups, analytics, social networking, shared services, …)
  • Webcenter Framework= the actual building blocks of a portal environment
  • Enterprise Extensions

The most important news given during this session:

A Migration Path will be foreseen when moving from OC4J into Weblogic Server

Well let’s see what happens early next year.

OOW – Oracle BPM Studio

Following an hands on lab regarding BPM Studio was really interesting for me, because I’m a firm believer of Oracle BPA Suite and I’m using it in day-to-day projects in Europe today.

So my big question was … what’s BPM Studio all about, and what will it offer that I don’t already have when using Oracle BPA Suite. Well I have to say, I still don’t know exactly how these 2 products will work together. So let’s have a quick overview of the different parts of BPM that got my attention on performing the hands-on lab.


The big difference in BPM Studio is the different contexts in which users will work depending on their skill-set and their responsibilities within the team.

You have 3 kinds of views within the ‘Eclipse’-environment – BPM Studio uses Eclipse as it’s IDE – you can be a ‘Business Analyst’, a ‘Business Architect’ or a ‘Business Developer’.
Depending on your responsibilities and your view you will get a different context within the IDE so only those components, views and perspectives are shown that are of importance for your skill-set.

The ability to add swimlanes, add activies, add components is more straight-forward than in BPA, but you’re not really using the BPMN Specs and best practices your used to working with as a BPMN Engineer.

When you’ve defined your process flow you can already test it, simulate it, within the environment and follow the process lifecycle using dashboards, charts, …

So the big value is that within one environment you have all the different artifacts you need to define a process, to deploy a process, to simulate a process (using KPI’s) and to monitor the process’ execution.

I will need to take a look at it to be able to have a clear comparison of strengths and weaknesses of both environments to be able to see the value in both of these products.