|
Search XAP 7.0
Offline Documentation
Download latest offline documentation in HTML format:
|
Summary: Overview of GigaSpaces in memory data grid - how to create a data grid, connect to it and interact with it
OverviewThis section covers the main APIs of GigaSpaces XAP. It explains how to:
Classpath Settings for Development and RuntimeTo compile and run code that interacts with the Space from within the IDE, you should include all the jars under <GigaSapces root>/lib/required directory in your build path (total of 4 jars - gs-runtime.jar, gs-openspaces.jar, spring.jar, commons-logging.jar). Instantiating a SpaceA Space is identified by its name and is comprised of one or more Space instances, which form the Space cluster. The Space instances interact with one another based on the Space topology defined for the Space. The topology is defined using a cluster schema, and the following schemas are provided out of the box:
The Space cluster is typically accessed from a remote JVM. Alternatively, applications that are collocated on the same JVM with a Space instance can access that instance locally without being exposed to the other instances (this is useful for implementing SBA applications). The Space can be instantiated in a number of ways:
Deploying a Space onto the Service Grid InfrastructureThe service grid infrastructure is composed of one or more GigaSpaces Containers (GSC) and at least one GigaSpaces manager (GSM). When deploying on to the service grid, the deployment tool you use connects to the GSM and instructs to provision Space instances to the running GSCs based on the deployment details (Space topology, number of instances, etc.).
Examples: gs deploy-space -cluster schema=sync_replicated total_members=2 mySpace A-sync-replicated cluster with 2 nodes: gs deploy-space -cluster schema=async_replicated total_members=2 mySpace Partitioned cluster with 2 nodes and a backup for each node: gs deploy-space -cluster schema=partitioned-sync2backup total_members=2,1 mySpace To see the full list of options available with this command please refer to this page.
Creating and Deploying a Processing onto the Service Grid InfrastructureBy using processing units you can deploy full blown applications onto the service grid and leverage on the Space's messaging and code execution capabilities such as remoting and task execution. This allows you to execute business logic close to the Space instance for the best possible performance. A processing unit can define an embedded Space in the processing unit's pu.xml file. <os-core:space id="space" url="/./mySpace"/>
This defines an embedded Space within the processing unit. The fact that the Space is embedded is determined by the url property. As you'll see below, a URL that start with the jini:// prefix indicates that a connection to a remote Space will be created and not an embedded Space. <os-sla:sla cluster-schema="partitioned-sync2backup" number-of-instances="2" number-of-backups="1"/>
Please refer to this page for more details on how to configure the Space component, and to this page for more details about the SLA definitions. Creating the Space via SpringIf you would like to create a Space within your own Spring application and do not wish to deploy it as a processing unit onto the GigaSpaces service grid, you can create an embedded Space instance within the application's JVM much the same way you would do in a regular processing unit configuration. The main difference with this approach is that when deploying on to the service grid, the GSM automatically starts the right amount of Space instances for you and assigns the instance ID to each of the instances. <os-core:space id="space" url="/./mySpace?total_members=2&id=1"/>
Creating the Space ProgrammaticallyThe last option is to create the Space Programmatically from within a plain Java application. Note that this option has the same limitation as creating the Space in your standalone Spring application, namely you have to start each of the instances separately and provide the instance ID to each of the started Space instances. Here's an example for starting the first instance of a sync-replicated Space with 10 instances: ClusterInfo clusterInfo = new ClusterInfo("sync-replicated", 1, null, 10, null); IJSpace space = new UrlSpaceConfigurer("/./mySpace").clusterInfo(clusterInfo).space(); Please refer to this page for more details on how to configure the Space component programmatically (click the "Code" tabs in all of the examples). Accessing the SpaceThe handle to the Space is represented by the low level IJSpace interface. When creating the Space in one of the above ways (programmatically, via Spring or within a processing unit), you get a handle to the local Space instance within your JVM. Although you can access the Space directly via the IJSpace interface (which was the mainstream approach until version 6.0), it is much more recommended to access the Space via one of the higher level APIs, namely the org.openSpaces.core.GigaSpace interface, the Map API or the JDBC API. The most recommended API is the GigaSpaces interface. It is the closest to the older IJSpace interface, but is different since it supports Java 5 generics, declarative Spring transactions and task execution over the Space. They support to 4 basic "verbs" of this model, namely read, write, take and notify (see below for more details).
Interacting with the Space Using the GigaSpace InterfaceThe rest of this page describes the GigaSpace interface and how to perform basic data access operations with to access the Space.
Basic Data Access Space OperationsThe GigaSpace interface supports a number of Space operations:
As per the JavaSpaces model, when you read an object from the Space, you can specify a timeout for the read operation. This means that the calling code can block until a certain object is written to the Space, which allows for much more sophisticated communication patterns that use the Space as a collaborative work area between multiple Space clients.
Creating a GigaSpace InstanceCreating a GigaSpace instance is done by wrapping an existing IJSpace instance or providing details about the Space you would like to connect to. This can be done programmatically or via Spring. Here's an example for this: Once you have access to a GigaSpace instance, you can start operating on the Space, namely write objects to it and read and take objects from it. POJOs as Data ObjectsAs mentioned earlier, one of the differences between the GigaSpace interface the classic net.jini.Space.JavaSpace interface is its support for POJOs as Space entries. The JavaSpace interface is rather intrusive and forces objects that are written to the Space to implement the net.jini.core.entry.Entry interface and mark any field to be stored on the Space as public. In terms of preconditions, your POJO classes need to follow the JavaBeans conventions, i.e. have a no-argument constructor, and declare a getter and setter to every field you want saved on the Space. Also, they cannot implement the net.jini.core.entry.Entry interface (there shouldn't be any reason to that anyway since it's an empty tagging interface). The POJO class does not have to implement java.io.Serializable, but it's properties must. The reason for that is that the POJOs fields are extracted when written to the Space and stored in a special tuple format that enables the Space to index and analyze them more easily. Therefore the actual POJO is not sent over the network, but rather it properties. Providing Metadata to the Space about the POJO ClassWhen writing POJOs to the Space, you can provide some metadata about the POJOs class to the space about it using Java 5 annotations or XML configuration. This overview will use annotation to provide metatdata. For a complete reference of POJO annotations and XML configuration please refer to this page.
Here's a sample POJO class: @SpaceClass public class Person { private Integer id; private String name; private String lastName; private Integer age; ... public Person() {} @SpaceId(autoGenerate=false) @SpaceRouting public Integer getId() { return id;} public void setId(Integer id) { this.id = id; } @SpaceProperty(index=BASIC) public Long getLastName() { return lastName; } public void setLastName(String type) { this.lastName = lastName; } ... } Space OperationsWriting objects to SpaceIn order to write or update objects in the Space, you should use the write method of the GigaSpace interface. The write method is used to write objects if these are introduced for the first time, or update them if these already exist in the space. In order to override these default semantics, you can use the overloaded write methods which accept update modifiers such as UpdateModifiers.UPDATE_ONLY. Person person = new Person(); person.setName("foo"); person.setLastName("bar"); person.setAge(25); gigaSpace.write(person); The GigaSpace interface also supports writing objects in batches. The corresponding methods for this are named writeMultiple and are mainly used to batch operations over a single network call in case you write to a remote space. Reading objects from SpaceThe read methods are used to retrieve objects from the Space. The read method returns a copy of the matching object to the client. To read more than one object, you should use the readMultiple methods of the GigaSpace interface. To define the criteria for the operation, all of these method accept either a template object or a SQLQuery instance. A template object is an example object of the class you would like to read. For an object in the space to match the template, each of the non-null properties in the template must match its values for these properties. // readById Integer id = ...//get the id Integer routing = ...//get the routing value (not mandatory, but more efficient) Person result1 = gigaSpace.readById(Person.class, id, routing); //second argument determines routing // readByIds Integer[] ids = new Integer[] { .... } // initialize an ids array Integer routingKey = ... // set a routing key value (not mandatory, but more efficient) ReadByIdsResult<Person> result = gigaSpace.readByIds(Person.class, ids, routingKey); for (Person person : result) { // ... } // template matching Person template = new Person(); template.setFirstName("foo"); template.setLastName(null);//means any value Person result2 = gigaSpace.read(template) ; // read by template //50 is the maximum number of results to retrieve Person[] multipleResults = gigaSpace.readMultiple(template, 50); // SQLQuery SQLQuery<Person> query = new SQLQuery<Person>("(name = ?) AND (age>? AND age<?)"); template.setParameters("foo" , 25 , 30); // returns all Person objects whose name is 'foo' and age between 25-30 (non-inclusive) Person[] multipleResults2 = gigaSpace.readMultiple(query, 50); Note that you can specify a timeout for the read operations. This will cause the calling code to block until a result becomes becomes available (or until the specified number of results is available in case of readMultiple Taking objects from SpaceThe take methods have similar method signatures as the read methods. The main difference is that the take removes the object from the Space in addition to returning a copy of it to the client. // takeById Integer id = ...//get the id Integer routing = ...//get the routing value (not mandatory, but more efficient) Person result1 = gigaSpace.takeById(Person.class, id, routing); // takeByIds Integer[] ids = new Integer[] { .... } // initialize an ids array Integer routingKey = ... // set a routing key value (not mandatory, but more efficient) TakeByIdsResult<Person> result = gigaSpace.takeByIds(Person.class, ids, routingKey); for (Person person : result) { // ... } //template matching Person template = new Person(); template.setFirstName("foo"); template.setLastName(null);//means any value Person result2 = gigaSpace.take(template) ; // take by template //50 is the maximum number of results to take Person[] multipleResults = gigaSpace.takeMultiple(template, 50); //SQLQuery SQLQuery<Person> query = new SQLQuery<Person>("(name = ?) AND (age>? AND age<?)"); template.setParameters("foo" , 25 , 30); // returns all Person objects whose name is 'foo' and age between 25-30 (non-inclusive) Person[] multipleResults2 = gigaSpace.takeMultiple(query, 50); Ordering of ResultsThe Space matches stored object with the template to return a result. Matched objects are stored in the Space and returned from it in no particular order. However you can use FIFO ordering or the ORDER BY statement to control the ordering. |
Deploying and Interacting with the Space
IMPORTANT: This is an old version of GigaSpaces XAP. Click here for the latest version.
(None)





"Deploy In-Memory Data Grid" button (top left, second button). 


