<commit_before>---
title:  Creating Regions Dynamically
---

You can dynamically create regions in your application code and automatically instantiate them on members of a distributed system.

If your application does not require partitioned regions, you can use the <span class="keyword apiname">com.gemstone.gemfire.cache.DynamicRegionFactory</span> class to dynamically create regions, or you can create them using the `<dynamic-region-factory>` element in the cache.xml file that defines the region. See [&lt;dynamic-region-factory&gt;](../../reference/topics/cache_xml.html#dynamic-region-factory).

Due to the number of options involved, most developers use functions to create regions dynamically in their applications, as described in this topic. Dynamic regions can also be created from the `gfsh` command line.

For a complete discussion of using Geode functions, see [Function Execution](../function_exec/chapter_overview.html). Functions use the <span class="keyword apiname">com.gemstone.gemfire.cache.execute.FunctionService</span> class.

For example, the following Java classes define and use a function for dynamic region creation:

The <span class="keyword apiname">CreateRegionFunction</span> class defines a function invoked on a server by a client using the <span class="keyword apiname">onServer()</span> method of the <span class="keyword apiname">FunctionService</span> class. This function call initiates region creation by putting an entry into the region attributes metadata region. The entry key is the region name and the value is the set of region attributes used to create the region.

``` pre
#CreateRegionFunction.java

import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.Declarable;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.RegionAttributes;
import com.gemstone.gemfire.cache.RegionFactory;
import com.gemstone.gemfire.cache.Scope;

import com.gemstone.gemfire.cache.execute.Function;
import com.gemstone.gemfire.cache.execute.FunctionContext;

import java.util.Properties;

public class CreateRegionFunction implements Function, Declarable {

  private final Cache cache;
  
  private final Region<String,RegionAttributes> regionAttributesMetadataRegion;

  private static final String REGION_ATTRIBUTES_METADATA_REGION = 
                                     "__regionAttributesMetadata";
  
  public enum Status {SUCCESSFUL, UNSUCCESSFUL, ALREADY_EXISTS};<commit_msg>Remove underscore from Creating Regions Dynamically example [#128972847]<commit_after>---
title:  Creating Regions Dynamically
---

You can dynamically create regions in your application code and automatically instantiate them on members of a distributed system.

If your application does not require partitioned regions, you can use the <span class="keyword apiname">com.gemstone.gemfire.cache.DynamicRegionFactory</span> class to dynamically create regions, or you can create them using the `<dynamic-region-factory>` element in the cache.xml file that defines the region. See [&lt;dynamic-region-factory&gt;](../../reference/topics/cache_xml.html#dynamic-region-factory).

Due to the number of options involved, most developers use functions to create regions dynamically in their applications, as described in this topic. Dynamic regions can also be created from the `gfsh` command line.

For a complete discussion of using Geode functions, see [Function Execution](../function_exec/chapter_overview.html). Functions use the <span class="keyword apiname">com.gemstone.gemfire.cache.execute.FunctionService</span> class.

For example, the following Java classes define and use a function for dynamic region creation:

The <span class="keyword apiname">CreateRegionFunction</span> class defines a function invoked on a server by a client using the <span class="keyword apiname">onServer()</span> method of the <span class="keyword apiname">FunctionService</span> class. This function call initiates region creation by putting an entry into the region attributes metadata region. The entry key is the region name and the value is the set of region attributes used to create the region.

``` pre
#CreateRegionFunction.java

import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.Declarable;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.RegionAttributes;
import com.gemstone.gemfire.cache.RegionFactory;
import com.gemstone.gemfire.cache.Scope;

import com.gemstone.gemfire.cache.execute.Function;
import com.gemstone.gemfire.cache.execute.FunctionContext;

import java.util.Properties;

public class CreateRegionFunction implements Function, Declarable {

  private final Cache cache;
  
  private final Region<String,RegionAttributes> regionAttributesMetadataRegion;

  private static final String REGION_ATTRIBUTES_METADATA_REGION = 
                                     "_regionAttributesMetadata";
  
  public enum Status {SUCCESSFUL, UNSUCCESSFUL, ALREADY_EXISTS};
{'fraction_non_alphanumeric': 0.07562862669245649, 'fraction_numerical': 0.0017408123791102514, 'mean_word_length': 5.260290556900727, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2890636', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1297, 'n_words': 494}