Getting Cache Manager Using Default Configuration

Access to Cacheonix API begins with obtaining an instance of Cacheonix. When Cacheonix is requested for the first time, Cacheonix initializes internal data structures and starts up network services if one or more clustered caches are configured:

final Cacheonix cacheonix = Cacheonix.getInstance();

The following example demonstrates how to get an instance of Cacheonix using the default Cacheonix configuration. Cacheonix receives the default configuration from a file named cacheonix-config.xml found in the classpath or using a file that name is defined by system parameter cacheonix.config.xml:

/**
 * Tester for CacheManager.
 */
public final class CacheonixTest extends TestCase {


   private Cacheonix cacheonix;


   /**
    * Tests getting an instance of CacheManager using a default Cacheonix configuration.
    */
   public void testGetInstance() {

      assertNotNull("Cacheonix created in setUp() method should not be null", cacheonix);
   }


   /**
    * Sets up the fixture. This method is called before a test is executed.
    * <p/>
    * Cacheonix receives the default configuration from a <code>cacheonix-config.xml</code> found in a class path or
    * using a file that name is defined by system parameter <code>cacheonix.config.xml<code>.
    */
   protected void setUp() throws Exception {

      super.setUp();

      // Get Cacheonix using a default Cacheonix configuration. The configuration
      // is stored in the conf/cacheonix-config.xml
      cacheonix = Cacheonix.getInstance();
   }


   /**
    * Tears down the fixture. This method is called after a test is executed.
    */
   protected void tearDown() throws Exception {

      // Cache manager has be be shutdown upon application exit.
      // Note that call to shutdown() here uses unregisterSingleton
      // set to true. This is necessary to support clean restart on setUp()
      cacheonix.shutdown(ShutdownMode.GRACEFUL_SHUTDOWN, true);
      cacheonix = null;

      super.tearDown();
   }
}

Once you've obtained the Cacheonix instance, you can access configured caches.

See Also

Labels

 
(None)