Using OJB with JCS.

This page will explain you in 4 steps how to configure Apache JCS as Object Cache for OJB in Cocoon.

Prerequisites

  • You have already OJBBlock

Facts

  • Object Cache helps to improve the database performance.
  • OJB is able to use different Object Cache Implementations.
  • The default OJB Object Cache Implementation is fast, but has some drawbacks.
  • Since Cocoon 2.1.5 the default cache implementation is based on Apache JCS.
  • Apache JCS is a distributed caching system for server-side java applications. It is far more powerful.

Platform

I think it works in any configuration, but just in case, here isthe tested platform:

  • Linux Fedora Core 2
  • J2SDK 1.4.2_04
  • Apache Tomcat 5.0.24
  • Cocoon 2.1.5

Steps

Let your servlet breathe calm

My first attempt to setup this configuration failed because of a OutOfMemoryError. The problem showed to be that the servlet does not had enough memory to run. ie: add this lines in catalina.sh:

# Set Java Options
JAVA_OPTS='-Xms32m -Xmx128m'
if [ -r "$JAVA_OPTS"/bin/setenv.sh ]; then
  . "$JAVA_OPTS"/bin/setenv.sh
fi

Tell OJB to use JCS

In your OJB.properties file, find the Object Cache section. Comment the line where is ObjectCacheDefaultImpl and uncomment the line where is ObjectCacheJCSPerClassImpl. ie:

#-------------------------------------------------------------------
# Object cache
#-------------------------------------------------------------------
...
#ObjectCacheClass=org.apache.ojb.broker.cache.ObjectCacheDefaultImpl
...
ObjectCacheClass=org.apache.ojb.broker.cache.ObjectCacheJCSPerClassImpl

Tell Cocoon that OJB will use JCS

Cocoon is shipped with a default JCS configuration. Cocoon allow to define new cache regions for JCS via cocoon.xconf. We need to configure the default region used by the JCS ObjectCache implementation for OJB. Search <store> and add this parameters:

<store logger="core.store">
    <parameter name="maxobjects" value="10000"/>
    <parameter name="use-cache-directory" value="true"/>
    <parameter name="jcs.region.ojbDefault" value="DC"/>
    <parameter name="jcs.region.ojbDefault.cacheattributes"
               value="org.apache.jcs.engine.CompositeCacheAttributes"/>
    <parameter name="jcs.region.ojbDefault.cacheattributes.MaxObjects" value="1000"/>
    <parameter name="jcs.region.ojbDefault.cacheattributes.MemoryCacheName"
               value="org.apache.jcs.engine.memory.lru.LRUMemoryCache"/>
</store>

The last 4 parameters (starting with jcs.region.ojbDefault) define the cache region that OJB will use. You can tell JCS how many OJB Object will be able to cache by changing the value of the parameter jcs.region.ojbDefault.cacheattributes.MaxObjects

That is all!

Now restart Tomcat and start to use JCS. You will notice a performance improvement. Good Luck!

More info:

  • Cocoon Store - What are the stores in Cocoon?
  • JCSLogging - How to redirect the JCS messages to a file (very easy too!)

AntonioGallardo

  • No labels