Managing Region Attributes

Use region attributes to fine-tune the region configuration provided by the region shortcut settings.

All region attributes have default settings, so you only need to use region attributes to set the ones you want to override. See Region Attributes List.

Define Region Attributes

Create region attributes using any of these methods:
  • Declarations inside the cache.xml <region> element:
    <cache>
       <region name="exampleRegion" refid="REPLICATE">
          <region-attributes statistics-enabled="true">
            <entry-idle-time>
              <expiration-attributes timeout="10" action="destroy"/>
            </entry-idle-time>
            <cache-listener>
              <class-name>quickstart.SimpleCacheListener</class-name>
            </cache-listener>
          </region-attributes>
        </region>
    </cache>

    When the cache.xml is loaded at startup, declared region attributes are automatically created and applied to the region.

  • RegionFactory API set* method calls:
    // Creating a partitioned region using the RegionFactory
    RegionFactory rf = cache.createRegionFactory(RegionShortcut.PARTITION);
    rf.addCacheListener(new LoggingCacheListener());
    custRegion = rf.create("customer");
    // Creating a partitioned region using the RegionFactory, with attribute modifications
    PartitionAttributesFactory paf = 
      new PartitionAttributesFactory<CustomerId, String>();
    paf.setPartitionResolver(new CustomerOrderResolver());
      
    RegionFactory rf = 
      cache.createRegionFactory(RegionShortcut.PARTITION);
    rf.setPartitionAttributes(paf.create());
    rf.addCacheListener(new LoggingCacheListener());
    custRegion = rf.create("customer");
    // Creating a client with a Pool Specification Using ClientRegionFactory
    ClientRegionFactory<String,String> cRegionFactory = 
    	cache.createClientRegionFactory(PROXY);
    Region<String, String> region = 
    	cRegionFactory.setPoolName("Pool3").create("DATA");
  • AttributesFactory API:
    1. Create an AttributesFactory instance
    2. Use the instance to set non-default attributes
    3. Run the factory create method to create a RegionAttributes instance
    4. Pass the RegionAttributes instance to the region create method:
    Cache cache = CacheFactory.create(system);
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.setDataPolicy(DataPolicy.REPLICATE);
    Region region = cache.createRegion("SampleRegion", factory.create());
    // Create a new region using another region's attributes
    AttributesFactory fac =
        new AttributesFactory(this.currRegion.getAttributes());
    Region nr = cache.createRegion(name, fac.create());

Modify Region Attributes

You can modify a region’s event handlers and expiration and eviction attributes after the region is created.
Note: Do not modify attributes for existing regions unless you have to. Creating the attributes you need at region creation is more efficient.
Modify attributes in one of these ways:
  • By loading a cache.xml with modified region attribute specifications:
    <!-- Change the listener for exampleRegion
    ...
        <region name="exampleRegion">
          <region-attributes statistics-enabled="true">
            <cache-listener>
              <class-name>quickstart.ComplicatedCacheListener</class-name>
            </cache-listener>
          </region-attributes>
        </region>
    ... 
  • Using the AttributesMutator API:
    1. Retrieve the AttributesMutator from the region
    2. Call the mutator set methods to modify attributes:
    currRegion = cache.getRegion("root");
    AttributesMutator mutator = this.currRegion.getAttributesMutator();
    mutator.addCacheListener(new LoggingCacheListener());