1. Overview

Apache Geode provides data management solutions through a distributed cloud architecture. It would be ideal to utilize the Spring Data APIs for the data access through the Apache Geode server.

In this tutorial, we’ll explore Spring Data Geode for the configuration and development of an Apache Geode Java client application.

2. Spring Data Geode

The Spring Data Geode library empowers a Java application to configure an Apache Geode server through XML and annotations. At the same time, the library is also handy for creating an Apache Geode cache client-server application.

The Spring Data Geode library is similar to Spring Data Gemfire. Apart from subtle differences, the latter provides integration with Pivotal Gemfire, which is a commercial version of Apache Geode.

Along the way, we’ll explore a few Spring Data Geode annotations to configure a Java application into an Apache Geode’s cache client.

3. Maven Dependency

Let’s add the latest spring-geode-starter dependency to our pom.xml:

<dependency>
    <groupId>org.springframework.geode</groupId>
    <artifactId>spring-geode-starter</artifactId>
    <version>1.1.1.RELEASE</version>
</dependency>

4. Apache Geode’s @ClientCacheApplication with Spring Boot

First, let’s create a Spring Boot ClientCacheApp by using @SpringBootApplication:

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

Then, to transform the ClientCacheApp class into the Apache Geode cache client, we’ll add the Spring Data Geode provided @ClientCacheApplication:

@ClientCacheApplication
// existing annotations
public class ClientCacheApp {
    // ...
}

That’s it! The cache client app is ready to run.

However, before starting our app, we’ll need to start the Apache Geode server.

5. Start an Apache Geode Server

Assuming that Apache Geode and gfsh command-line interface are already set up, we can start a locator named basicLocator and then a server named basicServer.

To do so, let’s run the following commands in the gfsh CLI:

gfsh>start locator --name="basicLocator"
gfsh>start server --name="basicServer"

Once the server starts running, we can list all the members:

gfsh>list members

The gfsh CLI output should list the locator and the server:

    Name     | Id
------------ | ------------------------------------------------------------------
basicLocator | 10.25.3.192(basicLocator:25461:locator)<ec><v0>:1024 [Coordinator]
basicServer  | 10.25.3.192(basicServer:25546)<v1>:1025

Voila! We’re all set to run our cache client app using the Maven command:

mvn spring-boot:run

6. Configuration

Let’s configure our cache client app to access data through the Apache Geode server.

6.1. Region

First, we’ll create an entity named Author and then define it as an Apache Geode Region. A Region is similar to a table in the RDBMS:

@Region("Authors")
public class Author {
    @Id
    private Long id;
    
    private String firstName;
    private String lastName;
    private int age;
}

Let’s review the Spring Data Geode annotations declared in the Author entity.

To begin with, @Region will create the Authors region in the Apache Geode server to persist the Author object.

Then, @Id will mark the property as a primary key.

6.2. Entity

We can enable the Author entity by adding @EnableEntityDefinedRegions.

Also, we’ll add @EnableClusterConfiguration to let the application create the regions in the Apache Geode server:

@EnableEntityDefinedRegions(basePackageClasses = Author.class)
@EnableClusterConfiguration
// existing annotations
public class ClientCacheApp {
    // ...
}

Therefore, restarting the app will create the regions automatically:

gfsh>list regions

List of regions
---------------
Authors

6.3. Repository

Next, we’ll add CRUD operations on the Author entity.

To do so, let’s create a repository named AuthorRepository, which extends Spring Data’s CrudRepository:

public interface AuthorRepository extends CrudRepository<Author, Long> {
}

Then, we’ll enable the AuthorRepository by adding @EnableGemfireRepositories:

@EnableGemfireRepositories(basePackageClasses = AuthorRepository.class)
// existing annotations
public class ClientCacheApp {
    // ...
}

Now, we’re all set to perform CRUD operations on the Author entity using methods like save and findById provided by CrudRepository.

6.4. Indexes

Spring Data Geode provides an easy way to create and enable the indexes in the Apache Geode server.

First, we’ll add @EnableIndexing to the ClientCacheApp class:

@EnableIndexing
// existing annotations
public class ClientCacheApp {
    // ...
}

Then, let’s add @Indexed to a property in the Author class:

public class Author {
    @Id
    private Long id;

    @Indexed
    private int age;

    // existing data members
}

Here, Spring Data Geode will automatically implement the indexes based on the annotations defined in the Author entity.

Hence, @Id will implement the primary key index for the id. Similarly, @Indexed will implement the hash index for the age.

Now, let’s restart the application and confirm the indexes created in the Apache Geode server:

gfsh> list indexes

Member Name | Region Path |       Name        | Type  | Indexed Expression | From Clause | Valid Index
----------- | ----------- | ----------------- | ----- | ------------------ | ----------- | -----------
basicServer | /Authors    | AuthorsAgeKeyIdx  | RANGE | age                | /Authors    | true
basicServer | /Authors    | AuthorsIdHashIdx  | RANGE | id                 | /Authors    | true

Likewise, we can use @LuceneIndexed to create an Apache Geode Lucene index for the String typed properties.

6.5. Continuous Query

The continuous query enables the application to receive automatic notifications when data gets changed in the server. It matches the query and relies on the subscription model.

To add the capability, we’ll create the AuthorService and add @ContinuousQuery with the matching query:

@Service
public class AuthorService {
    @ContinuousQuery(query = "SELECT * FROM /Authors a WHERE a.id = 1")
    public void process(CqEvent event) {
        System.out.println("Author #" + event.getKey() + " updated to " + event.getNewValue());
    }
}

To use the continuous queries, we’ll enable server-to-client subscriptions:

@ClientCacheApplication(subscriptionEnabled = true)
// existing annotations
public class ClientCacheApp {
    // ...
}

Hence, our app will receive automatic notification at the process method, whenever we modify an Author object with an id equal to 1.

7. Additional Annotations

Let’s explore a few handy annotations additionally available in the Spring Data Geode library.

7.1. @PeerCacheApplication

So far, we’ve examined a Spring Boot application as an Apache Geode cache client. At times, we may require our application to be an Apache Geode peer cache application.

Then, we should annotate the main class with @PeerCacheApplication in place of @CacheClientApplication.

Also, @PeerCacheApplication will automatically create an embedded peer cache instance to connect with.

7.2. @CacheServerApplication

Similarly, to have our Spring Boot application as both a peer member and a server, we can annotate the main class with @CacheServerApplication.

7.3. @EnableHttpService

We can enable Apache Geode’s embedded HTTP server for both of @PeerCacheApplication and @CacheServerApplication.

To do so, we need to annotate the main class with @EnableHttpService. By default, the HTTP service starts on port 7070.

7.4. @EnableLogging

We can enable the logging by simply adding @EnableLogging to the main class. At the same time, we can use the logLevel and logFile attributes to set the corresponding properties.

7.5. @EnablePdx

Also, we can enable Apache Geode’s PDX serialization technique for all our domains, by merely adding @EnablePdx to the main class.

7.6. @EnableSsl and @EnableSecurity

We can use @EnableSsl to switch on Apache Geode’s TCP/IP Socket SSL. Similarly, @EnableSecurity can be used to enable Apache Geode’s security for authentication and authorization.

8. Conclusion

In this tutorial, we’ve explored Spring Data for Apache Geode.

To begin with, we’ve created a Spring Boot application to serve as the Apache Geode cache client application.

At the same time, we’ve examined a few handy annotations provided by Spring Data Geode to configure and enable Apache Geode features.

Last, we’ve explored a few additional annotations like @PeerCacheApplication and *@*CacheServerApplication to change the application to a peer or server in the cluster configuration.

As usual, all the code implementations are available over on GitHub.