Summary: GigaSpaces JavaSpaces API Plain Old Java Object support - the
POJO.
Overview
GigaSpaces JavaSpaces POJO support allows you to use JavaBean classes as space domain classes, and perform space operations using these objects. POJO domain Classes should follow rules similar to the ones defined by JPA, Hibernate and other domain class frameworks.
GigaSpaces POJO rules:
Do not implement the net.jini.core.entry interface.
Include space class metadata decorations (indexed fields, affinity-keys, persisted mode, etc.).
You can define space classes metadata by class and field level decorations. These can be defined via annotations (supported in JVM 1.5 and above) or XML configurations files (gs.xml file, supported from JVM 1.4).
This page deals with the POJO class as a space domain class, used to model the space, and store application data into the IMDG.
POJO classes deployed as services into the Service Grid are described in the Data Event Listener and Remoting (SVF) sections. In these cases, the POJO class is used to process incoming data, or is invoked remotely.
A POJO as a Space Domain Class
Guidelines
When using a POJO as a space domain class, follow these guidelines:
A POJO class must implement a default (zero argument) constructor.
A POJO class cannot implement the net.jini.core.entry interface; otherwise, it is treated differently.
A POJO class should have space class metadata decorations using annotations or a gs.xml file with relevant metadata (indexed field list, version field, FIFO mode, persistency mode, primary key (i.e. id)). If neither are provided, the defaults are presumed. (The default settings might not always match your needs.)
Getter/setter methods for fields that you want to be persisted in the space.
Non-primitive fields must implement Serializable or Externalizable. For example, if you are using a POJO class that contains a nested class.
When performing matching/queries using primitive fields (int , long , double, etc.) thenullValueannotation ornull-valuetag must be specified with relevant values, to function correctly.
The UID can be determined using the @SpaceId annotation or the id tag.
When using POJOs, the write operation uses the UpdateModifiers.UPDATE_OR_WRITE mode by default. This means that when the space already includes an object with the same UID (@SpaceId(autoGenerate=false) with the same value), the POJO is updated, and a new object is not inserted.
When using SpaceId(autoGenerate=true), the UID is stored inside the SpaceId field, causing an overhead when indexed.
TokenQuery is not supported with POJOs.
POJO space mapping files gs.xml files can be loaded from:
<CLASSPATH>\config\mapping folder, or
The same package where the class file is located using the format <<Class Name>>.gs.xml.
A null value as template is not supported. Use new Object() instead.
FIFO mode is supported only at the POJO class level. You can't specify a template with fifo=false and write an object with fifo=true. You should use a different space proxy in such cases.
A POJO class must implement the Serializable or Externalizable interface if used as a parameter for a remote call (OpenSpaces remoting).
interface, you can use generics when conducting space operations.
The @Spaceid annotation or id tag must be declared when performing update operations.
To persist a POJO object using the ExternalDataSource , Mirror Service, JDBC Storage Adapter, or indexed file options, the persist decoration must have the value true.
When a space is configured to use the ExternalDataSource, the @Spaceid annotation or id tag auto-generate attribute should be set to false. The object must include a unique value with the SpaceId field when written into the space.
The SpaceId field can be java.lang.String type or any other type that implements the toString() which provides a unique value.
All net.jini.core.entry.Entry based classes meta data methods are not supported with POJO based classes. These include: _setEntryInfo() , __getEntryInfo() , __setEntryUID() , __getEntryUID() ,_getSpaceIndexedFields(). With POJO based space domain classes, meta data is declared using relevant annotations or xml tags.
Primitive boolean should not be used as a POJO field as this could lead to problems when doing template matching. Boolean should be used instead.
In order to query a base abstract class you, due to the fact that one can't create an instance of an abstract class, SQLQuery should be used.
Batch Operations
Batch operations boost performance, since they interact with the server in one call, and retrieve the result from the space with one space operation. This allows the client and server to utilize the network bandwidth in an efficient manner - in some cases, up to 10 times faster than single based operations.
readMultiple should be handled with care, since it can return a large data set (potentially all the space data). This might cause an out of memory error in the space and client process. You should use the GSIterator to return the result in batches in such cases.
Destructive batch operations (take , write , update) should be performed with transactions - this allows the client to roll back the space to its initial state before the operation was started, in case of a failure.
When calling writeMultiple or updateMultiple, make sure null values are not part of the passed array.
When using writeMultiple, you should verify that duplicated entries (with the same ID) do not appear as part of the passed array, since the identity of the object is determined based on its ID and not based on its reference. This is extremely important with an embedded space, since writeMultiple injects the ID value into the object after the write operation.
readMultiple and takeMultiple do not support timeout operations. The simple way to achieve this is by calling the read operation with the proper timeout, and if non-null values are returned, perform the batch operation.
UrlSpaceConfigurer urlSpaceConfigurer = new UrlSpaceConfigurer("jini://*/*/mySpace");
GigaSpace space = new GigaSpaceConfigurer(urlSpaceConfigurer.space())
.defaultTakeTimeout(1000)
.defaultReadTimeout(1000)
.gigaSpace();
The following writes an Employee object and reads it back using a simple template:
GigaSpace space;
Employee employee = new Employee("Last Name", newInteger(32));
employee.setFirstName("first name");
space.write(employee);
Employee template = new Employee();
Employee result = space.read(template);
Notifications
Registering for Notifications
The following registers for notifications:
GigaSpace space;
SimpleNotifyEventListenerContainer
notifyEventListenerContainer = new SimpleNotifyContainerConfigurer(space)
.template(new Employee())
.eventListenerAnnotation(newObject()
{
@SpaceDataEvent
public void eventHappened(Object event) {
System.out.println("onEvent called Got" + event);
}
})
.fifo(true)
.notifyWrite(true)
.notifyUpdate(true)
.notifyContainer();
Writing a Batch
Writing a Batch of Objects
When writing a batch of objects into the space, these should be placed into an array to be used by the GigaSpace.writeMultiple operation. The returned array will include the corresponding LeaseContext object.
GigaSpace space;
Employee emps[] = new Employee[2];
emps[0] = new Employee("Last Name A", newInteger(10));
emps[1] = new Employee("Last Name B", newInteger(20));
LeaseContext[] leaseContexts = space.writeMultiple(emps);
for (int i = 0;i<leaseContexts.length ; i++)
{
System.out.println ("Object UID " + leaseContexts[i].getUID() + " inserted into the space");
}
Reading using SQLQuery
Reading Objects using SQLQuery
The following queries the space using SQL:
GigaSpace space;
String querystr = "employeeID=1 or employeeID=2";
SQLQuery query = new SQLQuery(Employee.class.getName(), querystr);
Employee results[] = space.readMultiple(query , 10000);
Constructing SQLQuery objects is a relatively expensive operation. You should not construct these with every space query operation. Instead, it is recommended to construct it once, and then use it with dynamic query options: SQLQuery.setParameters and SQLQuery.setParameter.
Clearing
Clearing Objects
You can use the SQLQuery with the GigaSpace.clear to remove objects from the space:
When using the SQLQuery with bigger/less than queries, turn on the extended indexing.
Updating an Object
Updating an Object
The GigaSpace.write with the UpdateModifiers.UPDATE_ONLY modifier should be used to explicitly perform an update operation. The UpdateModifiers.UPDATE_OR_WRITE is the default mode with write operations. This means that subsequent calls to the write operation with an object with identical SpaceId will result in an update operation - i.e. a new object will not be inserted into the space.
Make sure your Space Class will have the SpaceId(autoGenerate=false) when performing update operations.
The GigaSpace.write has a few activity modes - With each mode the return object options are different.:
Inserting or updating an existing object - The UpdateModifiers.UPDATE_OR_WRITE modifier should be used. This is the default mode.
. Thrown only when running in Optimistic Locking mode. This Exception includes the existing version id of the object within the space and the client side version id of the object. In this case you should read the object again and retry the update operation. See Optimistic Locking for more details.
When updating an object, you can specify 0 (ZERO) as the lease time. This will instruct the space to use the original lease time used when the object has been written into the space.
try
{
LeaseContext ret = space.write(employee ,/*lease*/ 0 ,/*timeout*/ 1000 , UpdateModifiers.UPDATE_ONLY);
if ( ret == null)
{
// Object is locked - unsuccessful update
}
elseif (ret.getObject() instanceof Employee)
{
// successful update
}
}
catch (EntryNotInSpaceException enise)
{
// Object not in space - unsuccessful update
}
catch (SpaceOptimisticLockingFailureException solfe)
{
// Client holds wrong version of the object - unsuccessful update. We need to read it again and issue the update call again.
}
Updating a Batch
Updating a Batch of Objects
Make sure your Space Class will have the SpaceId(autoGenerate=false) when performing update operations.
The GigaSpace.updateMultiple returns an array of objects which correspond to the input object array. The returned object element can be one of the following:
mode does not support timeout based updates, there is no way to identify if an updated object is already locked under a transaction - i.e. the UpdateOperationTimeoutExceptionis not returned as part of the returned array elements. With a transactional system, it is recommended to perform batch updates using the UpdateModifiers.UPDATE_ONLY modifier.
Next subchapter:POJO Support - Advanced - This advanced section deals with the annotations and gs.xml mapping file, troubleshooting procedures, considerations, UID generation and usage, as well as frequently used code snippets.