Search This Blog

Wednesday 17 November 2010

How to configure your own cache factory to access multiple caches?

I needed to be able to load multiple cache configurations files within the same JVM for different applications. I found that to do this I simply create my own Cache Factory and supply my own cache config file. Of course you are in the same cluster as that member but this allows you to load different configuration files as shown below.

1. Create two cache config files as follows.

Cache Config 1 - cache-config1.xml
<?xml version="1.0"?>
<!DOCTYPE cache-config SYSTEM "cache-config.dtd">

<cache-config>

  <caching-scheme-mapping>
    <cache-mapping>
      <cache-name>config1</cache-name>
      <scheme-name>distributed</scheme-name>
    </cache-mapping>
  </caching-scheme-mapping>

  <caching-schemes>
    <distributed-scheme>
      <scheme-name>distributed</scheme-name>
      <service-name>DistributedCacheConfig1</service-name>
      <backing-map-scheme>
        <local-scheme>
          <high-units>10m</high-units>
        </local-scheme>
      </backing-map-scheme>
      <autostart>true</autostart>
    </distributed-scheme>
  </caching-schemes>

</cache-config> 

Cache Config 2 - cache-config2.xml
<?xml version="1.0"?>
<!DOCTYPE cache-config SYSTEM "cache-config.dtd">

<cache-config>

  <caching-scheme-mapping>
    <cache-mapping>
      <cache-name>config2</cache-name>
      <scheme-name>distributed</scheme-name>
    </cache-mapping>
  </caching-scheme-mapping>

  <caching-schemes>
    <distributed-scheme>
      <scheme-name>distributed</scheme-name>
      <service-name>DistributedCacheConfig2</service-name>
      <backing-map-scheme>
        <local-scheme>
          <high-units>10m</high-units>
        </local-scheme>
      </backing-map-scheme>
      <autostart>true</autostart>
    </distributed-scheme>
  </caching-schemes>

</cache-config>

2. Create a test class as follows.
package pas.au.coheremce.demo;

import com.tangosol.net.ConfigurableCacheFactory;
import com.tangosol.net.DefaultConfigurableCacheFactory;
import com.tangosol.net.NamedCache;
import com.tangosol.net.DefaultConfigurableCacheFactory.CacheInfo;

public class CacheFactoryDemo
{
  private ClassLoader loader = null;
  
  public CacheFactoryDemo()
  {
    loader  = getClass().getClassLoader();
  }

  public void run ()
  {
    // access cache config cache-config1.xml
    accessCacheConfig("cache-config1.xml", "config1");
    
    // access cache config cache-config2.xml
    accessCacheConfig("cache-config2.xml", "config2");
  }
  
  private void accessCacheConfig(String configName, String cacheName)
  {
    ConfigurableCacheFactory factory = 
      new DefaultConfigurableCacheFactory(configName);
    
    NamedCache namedCache = factory.ensureCache(cacheName, loader);  
    
    // display scheme mapping to verify we are using the correct cache config file
    DefaultConfigurableCacheFactory dccf = (DefaultConfigurableCacheFactory) factory;
    CacheInfo info = dccf.findSchemeMapping(cacheName);
    System.out.println(String.valueOf(dccf.resolveScheme(info)));
  }
  
  public static void main(String[] args)
  {
    CacheFactoryDemo test = new CacheFactoryDemo();
    test.run();
    System.out.println("all done..");
  }
}

3. When run the output below shows how this works.In the code we display the scheme mapping to verify we have set this up correctly.

Access cache-config1.xml and the output is as follows
Oracle Coherence Version 3.6.0.0 Build 17229
 Grid Edition: Development mode
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

2010-11-17 12:57:53.461/0.473 Oracle Coherence GE 3.6.0.0 <Info> (thread=main, member=n/a): Loaded cache configuration from "file:/C:/jdev/jdevprod/11113/jdeveloper/jdev/mywork/CoherenceLoadCustomCacheConfig/Demo/classes/cache-config1.xml"
2010-11-17 12:57:53.821/0.833 Oracle Coherence GE 3.6.0.0 <D4> (thread=main, member=n/a): TCMP bound to /10.187.114.243:8088 using SystemSocketProvider
2010-11-17 12:57:57.215/4.227 Oracle Coherence GE 3.6.0.0 <Info> (thread=Cluster, member=n/a): Created a new cluster "cluster:0xC4DB" with Member(Id=1, Timestamp=2010-11-17 12:57:53.846, Address=10.187.114.243:8088, MachineId=50163, Location=machine:paslap-au,process:6608, Role=PasAuCoheremceCacheFactoryDemo, Edition=Grid Edition, Mode=Development, CpuCount=4, SocketCount=4) UID=0x0ABB72F30000012C578D6836C3F31F98
2010-11-17 12:57:57.224/4.236 Oracle Coherence GE 3.6.0.0 <Info> (thread=main, member=n/a): Started cluster Name=cluster:0xC4DB

...

2010-11-17 12:57:57.440/4.452 Oracle Coherence GE 3.6.0.0 <D5> (thread=DistributedCache:DistributedCacheConfig1, member=1): Service DistributedCacheConfig1 joined the cluster with senior service member 1
<distributed-scheme>
  <scheme-name>distributed</scheme-name>
  <service-name>DistributedCacheConfig1</service-name>
  <backing-map-scheme>
    <local-scheme>
      <high-units>10m</high-units>
    </local-scheme>
  </backing-map-scheme>
  <autostart>true</autostart>
</distributed-scheme>

Access cache-config2.xml and the output from that is as follows
2010-11-17 12:57:57.504/4.516 Oracle Coherence GE 3.6.0.0 <Info> (thread=main, member=1): Loaded cache configuration from "file:/C:/jdev/jdevprod/11113/jdeveloper/jdev/mywork/CoherenceLoadCustomCacheConfig/Demo/classes/cache-config2.xml"
2010-11-17 12:57:57.508/4.520 Oracle Coherence GE 3.6.0.0 <D5> (thread=DistributedCache:DistributedCacheConfig2, member=1): Service DistributedCacheConfig2 joined the cluster with senior service member 1
<distributed-scheme>
  <scheme-name>distributed</scheme-name>
  <service-name>DistributedCacheConfig2</service-name>
  <backing-map-scheme>
    <local-scheme>
      <high-units>10m</high-units>
    </local-scheme>
  </backing-map-scheme>
  <autostart>true</autostart>
</distributed-scheme>  

More information on  this can be found in the javadoc as follows:

http://download.oracle.com/otn_hosted_doc/coherence/330/com/tangosol/net/DefaultConfigurableCacheFactory.html

2 comments:

Anonymous said...

Very nice demo Pas! The only other thing I would add is that if you go this route, you'll want to hold the reference to DCCF in case you want shutdown the cache factory (which will disconnect you from the cluster or from the proxy if running from Extend.)

-Patrick

Anonymous said...

Appreaciate for the work you have done into this article, this helps clear away some questions I had.