Configuring a Spring Data GigaSpaces Application
When developing a GigaSpaces application using Spring Data, you need to configure a connection to the active 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. inside the Spring IoC container. This topic explains how a basic connection can be created using XML- or Java-based Spring configurations.
You can start an embedded Space or set up a service grid. When using an embedded Space, you don't have to start any additional processes in your environment.
To start the data grid, use the following command:
gs.bat host run-agent --auto --gsc=2
gs.bat space deploy --partitions=1 --ha space
gs.sh host run-agent --auto --gsc=2
gs.sh space deploy --partitions=1 --ha space
For more information, see Your First Data Grid in the Getting Started section of the documentation website.
In your project (assuming you build it with Maven) add the following to the dependencies section of the pom.xml file:
<dependencies>
<!-- other dependency elements omitted -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-gigaspaces</artifactId>
<version>15.0-SNAPSHOT</version>
</dependency>
</dependencies>
Connecting to the Space with XML Metadata
To use the GigaSpaces repository you need to provide a connection to the Space with an instance of GigaSpace
. This can be configured with the Spring XML configuration.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:gigaspaces-data="http://www.springframework.org/schema/data/gigaspaces"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:os-core="http://www.openspaces.org/schema/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/gigaspaces http://www.springframework.org/schema/data/gigaspaces/spring-data-gigaspaces-15.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.openspaces.org/schema/core http://www.openspaces.org/schema/15.0/core/openspaces-core.xsd">
<context:annotation-config/>
<!-- A bean representing the Space proxy (requires an active data grid with the same name) -->
<os-core:space-proxy id="space" name="space"/>
<!-- GigaSpace interface implementation used for SpaceClient injection -->
<os-core:giga-space id="gigaSpace" space="space"/>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:gigaspaces-data="http://www.springframework.org/schema/data/gigaspaces"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:os-core="http://www.openspaces.org/schema/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/gigaspaces http://www.springframework.org/schema/data/gigaspaces/spring-data-gigaspaces-15.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.openspaces.org/schema/core http://www.openspaces.org/schema/15.0/core/openspaces-core.xsd">
<context:annotation-config/>
<!-- A bean representing an embedded Space -->
<os-core:embedded-space id="space" name="space"/>
<!-- GigaSpace interface implementation used for SpaceClient injection -->
<os-core:giga-space id="gigaSpace" space="space"/>
</beans>
Connecting to the Space with Java Metadata
The same configuration can be achieved with Java-based bean metadata.
@Configuration
public class ContextConfiguration {
/**
* Builds a space instance with settings that allow it to connect to the 'space'.
*/
@Bean
public GigaSpace space() {
UrlSpaceConfigurer urlSpaceConfigurer = new UrlSpaceConfigurer("jini://*/*/space");
return new GigaSpaceConfigurer(urlSpaceConfigurer).gigaSpace();
}
}
@Configuration
public class ContextConfiguration {
/**
* Builds a space instance with settings that allow it start the embedded space with name 'space'.
*/
@Bean
public GigaSpace space() {
UrlSpaceConfigurer urlSpaceConfigurer = new UrlSpaceConfigurer("/./space");
return new GigaSpaceConfigurer(urlSpaceConfigurer).gigaSpace();
}
}
Additional Space Configuration
GigaSpaces has additional Space features and functionality that you can configure for your project.
Local Cache
A local cache is a client-side cache that maintains a subset of the master Space's data based on the client application's recent activity. The local cache is created empty, and whenever the client application executes a query the local cache first tries to fulfill it from the cache, otherwise it executes it on the master Space and caches the result locally for future queries.
For more information about the GigaSpaces local cache, see Local Cache in the developer section of the documentation website.
Local View
A local view is a client-side, read-only cache that maintains a subset of the master Space's data, allowing the client to read distributed data without performing any remote calls or data serialization. Data is streamed to the client's local view based on predefined criteria (a collection of SQLQuery objects) specified by the client when the local view is created.
For more information about the GigaSpaces local view, see Local View in the developer section of the documentation website.
Space Persistence
Space persistence is a configuration where Space data is persisted into permanent storage and retrieved from it.
For more information about Space persistence, see Persist to Database in the Getting Started section of the documentation website.
Space Security
GigaSpaces provides comprehensive support for securing your data and/or Space operations . GigaSpaces provides a set of authorities granting privileged to access data and for performing operations on the Space.
For more information about configuring security for a GigaSpaces-based environment, see the Security section of the documentation website.
Using Native Write and Read Operations
The GigaSpace
configured above can be used to perform interactions directly with the Space. To do so, inject the GigaSpace
bean to your Repository classes. For example, the following code has a Person Repository that defines the basic operations:
@SpaceClass
public class Person {
private Integer id;
private String name;
private Integer age;
public Person() {
}
@SpaceId(autoGenerate = true)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
// getters and setters for other fields are omitted
}
@Repository
public class XapPersonRepository implements PersonRepository {
// GigaSpace can be injected and used directly in Repository layer
@Autowired
private GigaSpace space;
public void create(Person person) {
space.write(person);
}
public List<Person> findById(String personId) {
return space.readById(Person.class, personId);
}
...
}
The class is marked with a @SpaceClass
annotation, which allows Spring Data GigaSpaces to look for entities in your data model and automatically handle their structure. Additionally, the getId()
method is marked with a @SpaceId(autogenerate = true)
annotation, which tells the Space to handle IDs automatically.
Modeling your Data
Spring Data GigaSpaces comes with transparent support of native GigaSpaces features. Additional configurations can be applied to your POJOs to boost performance and reduce memory usage. When building the data model using Spring Data GigaSpaces, you may want to consider some of the features described below.
Indexing
The most well-known data store function that helps boost the performance of common queries is indexing. GigaSpaces provides several options: NONE, EQUAL, ORDERED, and EQUAL_AND_ORDERED. These indexes can be applied by annotating POJO Plain Old Java Object.
A regular Java object with no special restrictions other than those forced by the Java Language Specification and does not require any classpath. classes or their fields, such as with @SpaceIndex
or @CompoundSpaceIndex
annotations.
For more information about indexing, see the Indexing section of the documentation website.
Storage Types
You can define how objects are stored in the Space, either using annotations on each POJO in your model or by defining a default storage type for the Space. This is done to save time on serialization and deserialization, reduce memory usage. or to define a schema that will change over time. The following Sstorage types are available for POJOs: OBJECT
, BINARY
and COMPRESSED
.
For more information about storage types, see the Storage Types topic in the documentation website.
Exclusion
You can mark POJO properties with @SpaceExclude
to disable writing their values to the Space. This also affects Querydsl Q...
class generation from POJOs, because marked fields won't be available for querying in Querydsl
style.
Other Annotation-Based Features
For a full list of the available annotations, see the Annotation-Based Metadata section of the documentation website.