Using Coherence as a Cache from a J2EE Web Application

Although Coherence is often used to manage Servlet session state for J2EE applications – without changing the application code – it can also be used by J2EE Web applications to access data in a simple cache. This article discusses how to do this and how you should package your Web application so that it is a completely self contained Coherence client. Although the principles should work for all J2EE applications servers you will not be surprised to learn that I have only tested this on WebLogic Server (10.3).

The Web application outlined below is a Coherence*Extend Client, that means that it connects over a TCP/IP connection to the Coherence cluster (which uses UDP for inter-node communications) holding the cache data. A Proxy service run either on one or more of the cache nodes (or in a separate JVM) is used to pass requests and response between the client and the Coherence cluster – in a similar fashion to the way an Oracle Database listener works.

Note, the client could also be a Coherence Data Client, which means that the Web client would actually be part of the Coherence cluster. In this case the client would need to be ‘storage disabled’ through a system property or through the tangosol-coherence-override.xml configuration file. This means that although it will be part of the cluster it will not actually hold any data, so when the client is deployed or un-deployed there will be no cluster overhead re-organising cache data.

Accessing the configuration files

You really have a choice here. put them in the packaged Web application or reference them externally through the CLASSPATH or via a system property. Here I am going to package them with the Web application to make is completely self-contained. The code to do this is shown below:

 56    /**
 57     * <p>Reference to ConfigurableCacheFactory that can be used to
 58     * create a cache.</p>
 59     */
 60    private ConfigurableCacheFactory factory = null;
 62    /**
 63     * @inheritDoc
 64     */
 65    public void init(ServletConfig config)
 66      throws ServletException
 67    {
 68      super.init(config);
 69      // Create DefaultConfigurableCacheFactory using cache configuration file
 70      // in classes dir
 71      factory =
 72          new DefaultConfigurableCacheFactory("/config/cache-config.xml",
 73                                              getClass().getClassLoader());
 74    }

Here a DefaultConfigurableCacheFactory is used to read the configuration file used to setup the Coherence client parameters. All the configuration files that the Coherence client needs will be stored in the /WEB-INF/classes/config directory of the Web application. These are:

  • cache-config.xml indicating where the Coherence cluster can be contacted
  • pof-config.xml indicating the mapping between client and server objects. As both the client and server are Java only one object needs to be defined.
  • tangosol-coherence-override.xml encapsulated many of the Coherence settings the are often specified as system properties.

The cache is accessed in the doGet(..) method of Servlet  as shown below:

 76    /**
 77     * @inheritDoc
 78     */
 79    public void doGet(HttpServletRequest request,
 80                      HttpServletResponse response)
 81      throws ServletException, IOException
 82    {
 83      // Create response
 84      response.setContentType(CONTENT_TYPE);
 85      PrintWriter out = response.getWriter();
 86      out.println("<html>");
 87      out.println("<head><title>CoherenceServlet</title></head>");
 88      out.println("<body>");
 89      // Get Coherence cache date
 90      NamedCache cache =
 91        factory.ensureCache("stock-cache", getClass().getClassLoader());
 92      // Get Stock object from cache
 93      Stock s = (Stock) cache.get("ORCL");
 94      double price = 0;
 95      if (s != null)
 96      {
 97        price = s.getPrice();
 98      }
 99      // Output stock price in response
100      out.println("<p>The latest Oracle Stock Price is: <b>" + price +
101                  "</b></p>");
102      out.println("</body></html>");
103      out.close();
104    }

The other source file that needs to be deployed with the Servlet is the Stock object that is being cached – a separate Java application is used in this scenario to add a Stock object to the “stock-cache” and then continually update it. This means that if the content displayed by the Servlet is refreshed in a browser a new stock price will appear.

Packaging it all up

To package it all up you naturally wrap the Servlet and Stock classes – along with the Coherence configuration files and supporting web configuration files – in a WAR file. This WAR file then needs to be wrapped in an EAR file with the coherence.jar file and a reference to this JAR in the MANIFEST.MF file as follows:

Class-Path: lib/coherence.jar

Note in the Eclipse project the coherence.jar file is in the base dir of the EAR file. The complete structure of the EAR file is shown below:




When the application is deployed and run you should see something like this which if you keep refreshing should change.




That’s it. You should now be able to create a cache, put some data into it and then access it from the above Servlet. The full source code for this example can be found here. There is a JDeveloper 11g project and an Eclipse Eurpoa project. There are some batch files to run-up a cache server and a separate client to insert and update Stock data, but unfortunately they are only in JDeveloper project – at the moment. See the readme.txt file for details of how to set things up.


Post a Comment:
  • HTML Syntax: NOT allowed

Views and ideas about Oracle Coherence and other software


« July 2016