Spring Data for Tanzu GemFire 2.0

Annotation-based Configuration Quick Start

Last Updated February 19, 2025

This topic provides an overview of the Spring Data for VMware Tanzu GemFire annotations to get started quickly.

All annotations provide additional configuration attributes along with associated properties to conveniently customize the configuration and behavior of GemFire at runtime. However, in general, none of the attributes or associated properties are required to use a particular GemFire feature. Declare the annotation to enable the feature and you are done. Refer to the individual Javadoc of each annotation for more details.

Configure a ClientCache Application

To configure and bootstrap a GemFire ClientCache application, use the following:

@SpringBootApplication
@ClientCacheApplication
public class ClientApplication {
  
  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }
}

See @ClientCacheApplication Javadoc.

For more information, see Configuring GemFire Applications with Spring in Configuring a GemFire Client with the Spring Framework Using Annotations.

Configure Logging

To configure or adjust GemFire logging, annotate your Spring, GemFire client application class with @EnableLogging, as follows:

@SpringBootApplication
@ClientCacheApplication
@EnableLogging(logLevel="trace")
public class ClientApplication {

  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }
}

Default log-level is config. This annotation does not adjust log levels in your application and is only used for GemFire.

See @EnableLogging Javadoc.

For more information, see Configuring Logging Configuring a GemFire Client with the Spring Framework Using Annotations.

Configure Statistics

To gather GemFire statistics at runtime, annotate your Spring, GemFire client application class with @EnableStatistics, as follows:

@SpringBootApplication
@ClientCacheApplication
@EnableStatistics
public class ClientApplication {

  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }
}

See @EnableStatistics Javadoc.

For more information, see Configuring Statistics Configuring a GemFire Client with the Spring Framework Using Annotations.

Configure PDX

To enable GemFire PDX serialization, annotate your Spring, GemFire client application class with @EnablePdx, as follows:

@SpringBootApplication
@ClientCacheApplication
@EnablePdx
public class ClientApplication {

  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }
}

GemFire PDX Serialization is an alternative to Java Serialization with many added benefits. For one, it makes short work of making all of your application domain model types serializable without having to implement java.io.Serializable.

By default, Spring Data for Tanzu GemFire configures the MappingPdxSerializer to serialize your application domain model types, which does not require any special configuration out-of-the-box to properly identify application domain objects that need to be serialized and then perform the serialization since the logic in MappingPdxSerializer is based on Spring Data’s mapping infrastructure. For more details, see MappingPdxSerializer in POJO Mapping.

See @EnablePdx Javadoc.

For more information, see Configuring PDX in Configuring a GemFire Client with the Spring Framework Using Annotations.

Configure SSL

To enable GemFire SSL, annotate your Spring, GemFire client application class with @EnableSsl, as follows:

@SpringBootApplication
@ClientCacheApplication
@EnableSsl(components = SERVER)
public class ClientApplication {

  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }
}

Minimally, GemFire requires you to specify a keystore and truststore using the appropriate configuration attributes or properties. Both keystore and truststore configuration attributes or properties may refer to the same KeyStore file. Additionally, you must specify a username and password to access the KeyStore file if the file has been secured.

GemFire SSL allows you to configure the specific components of the system that require TLS, such as client/server, Locators, Gateways, etc. Optionally, you can specify that all components of GemFire use SSL with ALL.

See @EnableSsl Javadoc.

For more information, see Configuring SSL in Configuring a GemFire Client with the Spring Framework Using Annotations.

Configure Security

To enable GemFire security, annotate your Spring, GemFire client application class with @EnableSecurity, as follows:

@SpringBootApplication
@ClientCacheApplication
@EnableSecurity
public class ClientApplication {

  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }
}

You must configure a username and password. For more details, see Configuring Client Security in Configuring a GemFire Client with the Spring Framework Using Annotations.

See @EnableSecurity Javadoc.

For more information, see Configuring Security in Configuring a GemFire Client with the Spring Framework Using Annotations.

Configure GemFire Properties

To configure other low-level GemFire properties not covered by the feature-oriented, Spring Data for Tanzu GemFire configuration annotations, annotate your Spring, GemFire client application class with @GemFireProperties, as follows:

@SpringBootApplication
@ClientCacheAppliction
@EnableGemFireProperties(
    cacheXmlFile = "/path/to/cache.xml",
    locators = "lunchbox[11235],mailbox[10101],skullbox[12480]"
)
public class ClientApplication {

  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }
}

For the appropriate use of each property, see gemfire.properties vars.and gfsecurity.properties: GemFire Properties in the GemFire product documentation.

See @EnableGemFireProperties Javadoc.

For more information, see Configuring GemFire Properties in Configuring a GemFire Client with the Spring Framework Using Annotations.

Configure Caching

To use GemFire as a caching provider in Spring’s Cache Abstraction and have Spring Data for Tanzu GemFire automatically create GemFire Regions for the caches required by your application service components, annotate your Spring, GemFire client, application class with @EnableGemfireCaching and @EnableCachingDefinedRegions as follows:

@SpringBootApplication
@ClientCacheApplication
@EnableCachingDefinedRegions
@EnableGemfireCaching
public class ClientApplication {

  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }
}

Then define the application services that require caching as follows:

@Service
public class BookService {
  
    @Cacheable("Books")
    public Book findBy(ISBN isbn) {
        ...
    }
}

@EnableCachingDefinedRegions is optional. You can manually define your Regions instead.

See @EnableCachingDefinedRegions Javadoc.

See @EnableGemfireCaching Javadoc.

For more information, see Configuring Spring’s Cache Abstraction in Configuring a GemFire Client with the Spring Framework Using Annotations.

Configure Regions, Repositories, and Entities for Persistent Applications

To quickly create Spring, GemFire persistent client, applications, annotate your application class wit @EnableEntityDefinedRegions and @EnableGemfireRepositories, as follows:

@SpringBootApplication
@ClientCacheApplication
@EnableEntityDefinedRegions(basePackageClasses = Book.class)
@EnableGemfireRepositories(basePackageClasses = BookRepository.class)
public class ClientApplication {
  
  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }
}

Next, define your entity class and use the @Region mapping annotation to specify the Region in which your entity will be stored, as follows:

package example.app.model;

import ...;

@Region("Books")
public class Book {

  @Id
  private ISBN isbn;
  
  private Author author;
  
  private LocalDate published;
  
  private String title;

}

Note: The @Region("Books") entity class annotation is used by the @EnableEntityDefinedRegions to determine the Regions required by the application.

Finally, define your CRUD Repository with simple queries to persist and access Books, as follows:

package example.app.repo;

import ...;

public interface BookRepository extends CrudRepository {

  List<Book> findByAuthorOrderByPublishedDesc(Author author);

}

For more information, see Spring Data for Tanzu GemFire Repositories.

Configure Client Regions from Cluster-defined Regions

Alternatively, you can define client PROXY Regions from Regions already defined in the cluster using @EnableClusterDefinedRegions, as follows:

@SpringBootApplication
@ClientCacheApplication
@EnableClusterDefinedRegions
@EnableGemfireRepositories
public class ClientApplication {

  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }

  ...
}

For more information, see Configured Cluster-Defined Regions in Configuring a GemFire Client with the Spring Framework Using Annotations.

Configure Functions

GemFire Functions are useful in distributed compute scenarios where a potentially expensive computation requiring data can be performed in parallel across the nodes in the cluster. In this case, it is more efficient to bring the logic to where the data is located (stored) rather than requesting and fetching the data to be processed by the computation.

Use the @EnableGemfireFunctionExecutions along with one of the Function calling annotations: @OnMember, @OnMembers, @OnRegion, @OnServer and @OnServers.

@ClientCacheApplication
@EnableGemfireFunctionExecutions(basePackageClasses = CustomerRewardsFunction.class)
class ClientApplication {

  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }
}

@OnRegion("Customers")
interface CustomerRewardsFunctions {

  Integer computeLoyaltyPoints(Customer customer);

}

Configure Continuous Query

Real-time, event stream processing is becoming an increasingly important task for data-intensive applications, primarily to respond to user requests in a timely manner. GemFire Continuous Query (CQ) will help you achieve this rather complex task quite easily.

Enable CQ by annotating your application class with @EnableContinuousQueries and define your CQs along with the associated event handlers, as follows:

@ClientCacheApplication
@EnableContinuousQueries
class ClientApplication {

  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }
}

Then, define your CQs by annotating the associated handler method with @ContinousQuery, as follows:

@Service
class CustomerService {

  @ContinuousQuery(name = "CustomerQuery", query = "SELECT * FROM /Customers c WHERE ...")
  public void process(CqEvent event) {
    ...
  }
}

Anytime an event occurs changing the Customer data to match the predicate in your continuous OQL query (CQ), the process method will be called.

A new feature in GemFire 10.1 is the ability to exclude certain kinds of events from triggering your CQs. You can use this feature by setting the excludedEvents attribute on the @ContinuousQuery annotation.

@ContinuousQuery(name = "CustomerQuery", query = "...", excludedEvents = {CQEvent.INVALIDATE, CQEvent.DESTROY})

The above example excludes DESTROY and INVALIDATE events, so the CQ will only trigger on CREATE or UPDATE events.