oracle as java object cache

22
OracleAS Java Object Cache Darko Spasovski SIOUG 2008, Portorož

Upload: darko-spasovski

Post on 10-Apr-2015

882 views

Category:

Documents


0 download

DESCRIPTION

Java Object Cache (JOC) is an implementation of JSR 107 available in Oracle AS 10g. Its primary goal is to provide a service that will significantly improve server performance by managing local copies of objects that are expensive to retrieve or create. This presentation was given by me as part of the SIOUG (Slovenian Oracle User Group) conference in Portoroz, Slovenia in 2008. It describes the main features of JOC, as well as illustrates a typical use case scenario in a sample application.

TRANSCRIPT

Page 1: Oracle AS Java Object Cache

OracleAS Java Object Cache

Darko Spasovski

SIOUG 2008, Portorož

Page 2: Oracle AS Java Object Cache

What is it?

• Java Object Cache is a OC4J service that manages local copies of objects that are expensive to retrieve or create

• Known as Oracle Object Caching Service for Java (OCS4J) in Oracle AS 9i

• It is the basis for Java Specification Request 107

• Available as part of Oracle AS 10g and as a stand-alone JAR

Portorož, 23.9.2008 2

Page 3: Oracle AS Java Object Cache

Cache Organization

• Cache Environment. Includes cache regions, subregions, groups, and attributes.

• Cache regions, subregions, and groups associate objects and collections of objects with similar characteristics

• Attributes are associated with cache regions, subregions, groups, and individual objects. They affect how Java Object Cache manages objects.

• Cache Object Types. The cache object types include memory objects, disk objects, pooled objects, and StreamAccess objects.

Portorož, 23.9.2008 3

Page 4: Oracle AS Java Object Cache

Features of Java Object Cache

• Objects can be updated or invalidated

• Objects can be invalidated either explicitly, or with an attribute that specifies its expiration or idle time.

• Automatic object loading and creation

• Cache event notification provides for event handling

• Cache management attributes can be specified for each object or applied to cache regions/groups

• Can work locally or in a distributed mode

Portorož, 23.9.2008 4

Page 5: Oracle AS Java Object Cache

Distributed Object Cache

• By default the cache operates in local mode

• It can be configured to operate in distributed, multiple-cache environment

• Enable it in javacache.xml config file

<communication><isDistributed>true</isDistributed>

<discoverer ip=“10.1.1.22" discovery-port="7000“ />

<discoverer ip=“10.1.1.23" discovery-port="7000“ />

</communication>

• All caches cooperating in the same cache system must specify the same set of host name and port addresses

• Set the DISTRIBUTE attribute for all regions, sub-regions and groups that you want distributed across the cache nodes

Portorož, 23.9.2008 5

Page 6: Oracle AS Java Object Cache

Distributed Object Cache (continued)

• Invalidations, destroys, and changes are propagated through the cache messaging system

• Changes can be propagated to other caches with a request for confirmation

• This is done by setting the REPLY attribute for the region, sub-region or group of cached objects

•CacheAccess.waitForResponse() should be used to block until all instances confirm the change

• A proprietary protocol is used for inter-cache communication; this can be easily changed to HTTP/SSL using the JOC API

Portorož, 23.9.2008 6

Page 7: Oracle AS Java Object Cache

Basic Architecture of the JOC API

Portorož, 23.9.2008 7

User User User

CacheCache

Data Source Data Source

CacheLoader APICacheLoader API

CacheAccess APICacheAccess API

Data Source

Page 8: Oracle AS Java Object Cache

How it works

• Objects can be loaded in the cache manually or by using a user-defined CacheLoader object

• Loading the object from cache is done as follows:

Portorož, 23.9.2008 8

Get objectName

Object is in

cache?

Return object

Cache loader

defined?

Load object with CacheLoader

Is cache distributed?

Do a distributed cache search

ObjectNotFoundException

Object found?

Yes

Yes Yes

Yes

No No No

No

Page 9: Oracle AS Java Object Cache

Configuration (OC4J)

• Java Object Cache is not automatically initialized by Oracle AS

• It can be activated by defining oracle.ias.jcache in opmn.xml

• OC4J initializes JOC using configuration in javacache.xml

• The path to javacache.xml is specified via the javacache-config element of OC4J’s server.xml file:

<javacache-config path="../../../javacache/admin/javacache.xml"/>

Portorož, 23.9.2008 9

Page 10: Oracle AS Java Object Cache

Configuration (stand-alone)

1. Include cache.jar in application’s classpath

2. Initialize it using oracle.ias.cache.Cache class (JOC API)

try

{

Cache.open (“/path-to-config-file/javacache.xml”);

...

}

catch (Exception ex) { }

Portorož, 23.9.2008 10

Page 11: Oracle AS Java Object Cache

Configuration (javacache.xml)

• javacache.xml is used to specify different attributes important for the cache operation:

• clean-interval• ping-interval• max-size• max-objects• region-name-separator• preload-file• communication• logging• persistence

Portorož, 23.9.2008 11

javacache.xml example:

<?xml version="1.0" encoding="UTF-8"?><cache-configuration> <logging>

<location>javacache.log</location><level>ERROR</level>

</logging> <communication>

<isDistributed>true</isDistributed><discoverer discovery-port="7000"/>

</communication> <persistence>

<location>diskcache</location><disksize>32</disksize>

</persistence> <max-objects>1000</max-objects> <max-size>48</max-size> <clean-interval>30</clean-interval></cache-configuration>

javacache.xml example:

<?xml version="1.0" encoding="UTF-8"?><cache-configuration> <logging>

<location>javacache.log</location><level>ERROR</level>

</logging> <communication>

<isDistributed>true</isDistributed><discoverer discovery-port="7000"/>

</communication> <persistence>

<location>diskcache</location><disksize>32</disksize>

</persistence> <max-objects>1000</max-objects> <max-size>48</max-size> <clean-interval>30</clean-interval></cache-configuration>

Page 12: Oracle AS Java Object Cache

Practical example: codebooks

• Codebooks

• Used in many types of applications (finances, government, etc)

• Frequently accessed, but rarely modified

• Usually expensive to create

• Ideal case for caching

Portorož, 23.9.2008 12

Page 13: Oracle AS Java Object Cache

Concrete codebook implementation

• We have implemented a Data Access Object (DAO) for each codebook

• The DAO fetches the codebook records from the database

• Individual codebook records are kept in Value Objects (VO)

• Each VO has to implement a getDAO() method which returns an instance of its DAO implementation

• We will be caching a collection of codebooks records instead of single records

Portorož, 23.9.2008 13

Page 14: Oracle AS Java Object Cache

Implementation overview

Portorož, 23.9.2008 14

Page 15: Oracle AS Java Object Cache

Codebooks.java (initialization)

private Codebooks()

{

initDataSource();

initCache();

}

public static Codebooks getInstance()

{

if (instance == null) instance = new Codebooks();

return instance;

}

private void initCache() {

try

{

Cache.open(CACHE_CONFIG_FILE);

Attributes attr = new Attributes();

attr.setLoader(new CodebookCacheLoader());

CacheAccess.defineRegion(DEFAULT_REGION, attr);

}

catch (CacheException e)

{

logger.error("Error initializing cache", e);

}

}

Portorož, 23.9.2008 15

Page 16: Oracle AS Java Object Cache

Codebooks.java (object lookup)

public List<NvisVO> getList (Class clazz) throws Exception

{

ArrayList params = new ArrayList();

params.add(0, clazz);

params.add(1, dataSource);

CacheAccess cacheAccess = null;

List<NvisVO> result = new ArrayList<NvisVO>();

try

{

cacheAccess = CacheAccess.getAccess(DEFAULT_REGION);

Object cachedObject = cacheAccess.get(clazz.getCanonicalName(), params);

result.addAll((Collection<? extends NvisVO>) cachedObject);

}

catch (Exception e)

{

logger.error("Error while fetching object from cache: ", e);

}

finally {

if (cacheAccess == null) cacheAccess.close();

}

return result;

}

Portorož, 23.9.2008 16

Page 17: Oracle AS Java Object Cache

Codebooks.java (cache invalidation)

public boolean invalidate(Class clazz)

{

boolean isInvalidated = false;

CacheAccess cacheAccess = null;

try

{

cacheAccess = CacheAccess.getAccess(DEFAULT_REGION);

cacheAccess.invalidate(clazz.getCanonicalName(), false);

isInvalidated = true;

}

catch (Exception e)

{

logger.error("Error invalidating object " + clazz.getCanonicalName(), e);

}

finally

{

if (cacheAccess == null) cacheAccess.close();

}

return isInvalidated;

}

Portorož, 23.9.2008 17

Page 18: Oracle AS Java Object Cache

CodebookCacheLoader.java

public class CodebookCacheLoader extends CacheLoader

{

@Override

public Object load(Object handle, Object args) throws CacheException

{

try

{

Collection codebook = (Collection) netSearch(handle, 1000);

return codebook;

}

catch (Exception e) { }

try

{

Class codebookClass = (Class) ((ArrayList)args).get(0);

DataSource ds = (DataSource) ((ArrayList)args).get(1);

NvisVO vo = (NvisVO) codebookClass.newInstance ();

NvisDAO dao = vo.getDao (ds);

return new ArrayList (dao.getRecords ());

}

catch (Exception e)

{

throw new CacheException("Unexpected error in cache loader", e);

}

}

}

Portorož, 23.9.2008 18

Page 19: Oracle AS Java Object Cache

Programming tips (part I)

• Do not share the CacheAccess object between threads!

• If multiple cached objects are being accessed concurrently, use multiple CacheAccess objects

• CacheAccess objects are pooled, so always close a CacheAccess object when it is no longer being used

• If a cached object must survive process termination, both the object and the cache must be DISTRIBUTE

• The cache configuration (environment) is local to a cache – it is not propagated to other caches

Portorož, 23.9.2008 19

Page 20: Oracle AS Java Object Cache

Programming tips (part II)

• Be careful: invalidating a distributed region will also invalidate all local i.e. non-distributed objects that are part of that region!

• If CacheAccess.waitForResponse() times out, you must call it again until it returns successfully. If waitForResponse() does not succeed, you must call CacheAccess.cancelResponse() to free resources

Portorož, 23.9.2008 20

Page 21: Oracle AS Java Object Cache

Useful references

• “Oracle Application Server Containers for J2EE Services Guide 10g Release 2 (10.1.2) for Windows or UNIX”, Oracle, July 2005 (Part No. B14012-02)

Chapter 9: Java Object Cachehttp://download-west.oracle.com/docs/cd/B14099_19/web.1012/b14012/objcache.htm

• Oracle Java Object Cache Tutorialhttp://www.oracle.com/technology/products/ias/joc/10.1.2/tutorial/index.html

• Oracle Java Object Cache API documentationhttp://www.oracle.com/technology/products/ias/joc/10.1.2/javadoc/allclasses-noframe.html

• JSR 107: JCACHE - Java Temporary Caching API http://www.jcp.org/en/jsr/detail?id=107

Portorož, 23.9.2008 21

Page 22: Oracle AS Java Object Cache

Portorož, 23.9.2008 22

Thank you

Questions?

[email protected]