gRPC Client for Space Operations
Implementing a gRPC client provides a lightweight, multi-language interface designed for modern cloud-native architectures. It enables low-overhead integration with serverless environments like AWS Lambda, allows polyglot applications to interact directly with the IMDG
In-Memory Data Grid.
A simple to deploy, highly distributed, and cost-effective solution for accelerating and scaling services and applications. It is a high throughput and low latency data fabric that minimizes access to high-latency, hard-disk-drive-based or solid-state-drive-based data storage. The application and the data co-locate in the same memory space, reducing data movement over the network and providing both data and application scalability. Space
Where GigaSpaces data is stored. It is the logical cache that holds data objects in memory and might also hold them in layered in tiering. Data is hosted from multiple SoRs, consolidated as a unified data model., and offers Java services a minimal dependency footprint to eliminate classpath and library conflicts.
Overview
The GigaSpaces XAP
GigaSpaces eXtreme Application Platform.
Provides a powerful solution for data processing, launching, and running digital services REST
REpresentational State Transfer. Application Programming Interface
An API, or application programming interface, is a set of rules that define how applications or devices can connect to and communicate with each other. A REST API is an API that conforms to the design principles of the REST, or representational state transfer architectural style. v3 API provides a gRPC interface for Space operations through a service called SpaceService. This guide explains how to build a gRPC client to perform simple Space operations like Write, Read, Take, Clear, Count, and Query.
Key features:
-
Type-safe Protocol Buffer definitions
-
Efficient binary serialization
-
Support for single and batch operations
-
Query expression support
-
Security and audit logging
-
OpenTelemetry tracing integration
-
Full error handling with proper status codes
Prerequisites
Software Requirements
-
gRPC Framework (version 1.50+)
-
Protocol Buffer Compiler (
protoc) 3.x or higher -
Java 11+ (Java 8 is supported with additional ALPN configuration)
-
Maven or Gradle (for build management)
-
Running GigaSpaces XAP instance with REST v3 server enabled
Knowledge Requirements
-
Basic understanding of gRPC concepts
-
Familiarity with Protocol Buffers
-
Understanding of XAP Space operations (Write, Read, Take, etc.)
-
Basic knowledge of the data types you plan to store
Getting the Protocol Buffer Definitions
The server provides a convenient endpoint to download all required .proto files.
Download Proto Files
URL: http://<server-host>:<server-port>/grpc/proto.zip
Example:
curl -o space-grpc-proto.zip http://localhost:8090/grpc/proto.zip
This downloads a ZIP containing:
| File | Contents |
|---|---|
space_service.proto
|
Service definition with all RPC methods |
space_crud.proto
|
Create, Read, Update, Delete operations |
space_admin.proto
|
Admin operations like RegisterType, Clear |
space_tasks.proto
|
Task execution operations |
common.proto
|
Common message types and enumerations |
Extract the Files
unzip space-grpc-proto.zip
This creates a space/ directory with all .proto files.
Proto File Dependencies
The .proto files are organized with clear dependencies:
space_service.proto
├── imports space_crud.proto
├── imports space_admin.proto
└── imports space_tasks.proto
All of the above import common.proto.
Project Setup for Java Thin Client
For Maven Projects
Add the following dependencies to your pom.xml:
<dependencies>
<!-- gRPC -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.53.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.53.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.53.0</version>
</dependency>
<!-- Protocol Buffers -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.21.12</version>
</dependency>
</dependencies>
Add the protobuf-maven-plugin to your build section:
<build>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.21.12:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.53.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Project Directory Structure
my-space-client/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/example/spaceclient/
│ │ │ ├── SpaceClient.java
│ │ │ ├── operations/
│ │ │ │ ├── WriteOperation.java
│ │ │ │ ├── ReadOperation.java
│ │ │ │ └── QueryOperation.java
│ │ │ └── config/
│ │ │ └── ClientConfig.java
│ │ └── proto/
│ │ └── space/
│ │ ├── space_service.proto
│ │ ├── space_crud.proto
│ │ ├── space_admin.proto
│ │ ├── space_tasks.proto
│ │ └── common.proto
│ └── test/
│ └── java/
│ └── com/example/spaceclient/
│ └── SpaceClientTest.java
├── pom.xml (or build.gradle)
└── README.md
Java Client Code Example
The client code for Java 8 is identical to Java 11+, with one exception when using TLS
Transport Layer Security, or TLS, is a widely adopted security protocol designed to facilitate privacy and data security for communications over the Internet. A primary use case of TLS is encrypting the communication between web applications and servers.. If your server uses TLS/SSL, you should explicitly enable transport security for better ALPN negotiation:
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
public class SpaceClientJava8 {
private final ManagedChannel channel;
private final SpaceServiceGrpc.SpaceServiceBlockingStub blockingStub;
public SpaceClientJava8(String host, int port, boolean useTls) {
// For Java 8 with TLS: explicitly enable transport security
// to ensure ALPN negotiation works properly
if (useTls) {
this.channel = ManagedChannelBuilder.forAddress(host, port)
.useTransportSecurity() // TLS with ALPN support
.build();
} else {
this.channel = ManagedChannelBuilder.forAddress(host, port)
.usePlaintext() // No encryption
.build();
}
this.blockingStub = SpaceServiceGrpc.newBlockingStub(channel);
}
public void shutdown() throws InterruptedException {
channel.shutdown();
if (!channel.awaitTermination(5, java.util.concurrent.TimeUnit.SECONDS)) {
channel.shutdownNow();
}
}
}
Building a Basic Client
Client Initialization
import io.grpc.Channel;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
public class SpaceClient {
private final ManagedChannel channel;
private final SpaceServiceGrpc.SpaceServiceBlockingStub blockingStub;
public SpaceClient(String host, int port) {
this.channel = ManagedChannelBuilder.forAddress(host, port)
.usePlaintext()
.build();
this.blockingStub = SpaceServiceGrpc.newBlockingStub(channel);
}
public void shutdown() throws InterruptedException {
channel.shutdown();
if (!channel.awaitTermination(5, java.util.concurrent.TimeUnit.SECONDS)) {
channel.shutdownNow();
}
}
}
Basic Write Operation
public WriteResponse writeObject(String spaceName, String typeName,
Map<String, Object> properties) {
WriteRequest request = WriteRequest.newBuilder()
.setSpaceName(spaceName)
.setTypeName(typeName)
.putAllProperties(convertToProtoValues(properties))
.build();
return blockingStub.write(request);
}
Basic Read Operation
public ReadByIdResponse readById(String spaceName, String id) {
ReadByIdRequest request = ReadByIdRequest.newBuilder()
.setSpaceName(spaceName)
.setId(id)
.build();
return blockingStub.readById(request);
}
Helper Method for Type Conversion
private Map<String, Value> convertToProtoValues(Map<String, Object> properties) {
Map<String, Value> protoValues = new HashMap<>();
for (Map.Entry<String, Object> entry : properties.entrySet()) {
Value.Builder valueBuilder = Value.newBuilder();
Object value = entry.getValue();
if (value instanceof String) {
valueBuilder.setStringValue((String) value);
} else if (value instanceof Integer) {
valueBuilder.setIntValue((Integer) value);
} else if (value instanceof Long) {
valueBuilder.setLongValue((Long) value);
} else if (value instanceof Double) {
valueBuilder.setDoubleValue((Double) value);
} else if (value instanceof Boolean) {
valueBuilder.setBoolValue((Boolean) value);
} else if (value instanceof byte[]) {
valueBuilder.setBytesValue(com.google.protobuf.ByteString.copyFrom((byte[]) value));
}
protoValues.put(entry.getKey(), valueBuilder.build());
}
return protoValues;
}
Supported Space Operations
The SpaceService interface provides the following RPC methods:
WRITE Operations
-
write(WriteRequest) -> WriteResponse- Write a single object to the Space. -
writeMultiple(WriteMultipleRequest) -> WriteMultipleResponse- Write multiple objects in a batch operation.
READ Operations
-
readById(ReadByIdRequest) -> ReadByIdResponse- Read a single object by ID. -
readByIds(ReadByIdsRequest) -> ReadByIdsResponse- Read multiple objects by IDs. -
readMultiple(ReadMultipleRequest) -> ReadMultipleResponse- Read objects matching a query expression.
TAKE Operations
-
takeById(TakeByIdRequest) -> TakeByIdResponse- Take (remove and return) a single object by ID. -
takeByIds(TakeByIdsRequest) -> TakeByIdsResponse- Take multiple objects by IDs. -
takeMultiple(TakeMultipleRequest) -> TakeMultipleResponse- Take objects matching a query expression.
QUERY Operations
-
queryExpression(QueryExpressionRequest) -> QueryExpressionResponse- Execute a SQL-like query expression against the Space.
ADMIN Operations
-
registerType(RegisterTypeRequest) -> RegisterTypeResponse- Register a new data type with the Space. -
clear(ClearRequest) -> ClearResponse- Clear all objects of a type from the Space. -
count(CountRequest) -> CountResponse- Count objects of a type in the Space. -
change(ChangeRequest) -> ChangeResponse- Update specific properties of objects in the Space.
TASK Operations
-
executeDurableTask(ExecuteDurableTaskRequest) -> ExecuteDurableTaskResponse- Execute a durable task on the Space.
In-Memory Data Grid - achieve unparalleled speed, persistence, and accuracy.