OverviewGigaSpaces 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. POJOs as Data ObjectsOne of the differences between the GigaSpace interface and 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. The GigaSpace interface fully supports POJOs, and is less intrusive than the original JavaSpace interface. Although Entry is still supported, this is discouraged, and you should only use POJOs as a Space data object. 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 do that anyway, since it is an empty tagging interface). The POJO class does not have to implement java.io.Serializable, but its properties must. The reason for this, 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 its properties. You can override the default serialization behaviour of your POJO by implementing the java.io.Externalizable interface. Providing Metadata to the Space about the POJO ClassWhen writing POJOs to the Space, you can provide some metadata about the POJO's class to the space, using Java 5 annotations, or an XML configuration. This overview uses annotation to provide metadata. For a complete reference to POJO annotations and XML configuration, refer to the POJO Metadata section. Here is an overview of the most commonly used POJO annotations:
Here is 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; } ... } GigaSpaces POJO rules:
A POJO as a Space Domain ClassWhen using a POJO as a space domain class, follow these guidelines:
Controlling Space Class Fields IntroductionTo force GigaSpaces to ignore a POJO field when the space class is introduced to the space you should use one of the following:
Space POJO Class Metadata FilesPOJO space mapping files gs.xml files can be loaded from:
Jini Entry HandlingAll 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. see the POJO Metadata for details. Reference HandlingWhen running in embedded mode, Space object fields are passed by reference to the space. Extra caution should be taken with non-primitive none mutable fields such as collections (HashTable, Vector). Changes made to those fields outside the context of the space will impact the value of those fields in the space and may result in unexpected behavior. For example, index lists aren't maintained because the space is unaware of the modified field values. For those fields it is recommended to pass a cloned value rather then the reference itself. Passing a cloned value is important when several threads access the Object fields - for example application threads and replication threads. Non-Indexed FieldsNon-Indexed fields that are not used for queries should be placed within a user defined class (payload object) and have their getter and setter placed within the payload class. This improves the read/write performance since these fields would not be introduced to the space class model.
Code Snippets
Write and Read
Write and ReadGetting Space proxy: 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", new Integer(32)); employee.setFirstName("first name"); LeaseContext<Employee> lc = space.write(employee); Employee template = new Employee(); Employee result = space.read(template); NO_RETURN_VALUE Modifier By default the write operation returns a LeaseContext object with Lease object or the previous version of the object (via the LeaseContext.getObject()). To avoid this overhead you should use the NO_RETURN_VALUE modifier with the write operation. Once used, the write operation will have a null as a return value. This avoids the usual network traffic generated by sending the previous version of the object (update operation) or the Lease object (write operation) back into the client side. Use this option to improve application write operation performance both with remote and embedded space. Here is how you can use the NO_RETURN_VALUE modifier: gigaspace.write(employee, Lease.FOREVER, 0, UpdateModifiers.NO_RETURN_VALUE | UpdateModifiers.UPDATE_OR_WRITE); Another option to turn on the NO_RETURN_VALUE mode is to set the UpdateModifiers default mode once you get the space proxy: GigaSpace gigaspace = new GigaSpaceConfigurer(new UrlSpaceConfigurer("jini://*/*/mySpace")).gigaSpace(); gigaspace.getSpace().setUpdateModifiers(UpdateModifiers.NO_RETURN_VALUE | UpdateModifiers.UPDATE_OR_WRITE); ... gigaspace.write(employee); The writeMultiple call (batch write) support the NO_RETURN_VALUE as well: gigaspace.writeMultiple(employeesArray, Lease.FOREVER, UpdateModifiers.NO_RETURN_VALUE | UpdateModifiers.UPDATE_OR_WRITE );
Notification Registration Notification RegistrationThe following registers for notifications: GigaSpace space; SimpleNotifyEventListenerContainer notifyEventListenerContainer = new SimpleNotifyContainerConfigurer(space) .template(new Employee()) .eventListenerAnnotation(new Object() { @SpaceDataEvent public void eventHappened(Object event) { System.out.println("onEvent called Got" + event); } }) .fifo(true) .notifyWrite(true) .notifyUpdate(true) .notifyContainer(); Batch Write Batch WriteWhen 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", new Integer(10)); emps[1] = new Employee("Last Name B", new Integer(20)); try { 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"); } } catch (WriteMultipleException e) { IWriteResult[] writeResult = e.getResults(); for (int i = 0;i< writeResult.length ; i++) { System.out.println ("Problem with Object UID " + writeResult "); } } Batch Read Batch ReadThe following queries the space using SQL: GigaSpace space; String querystr = "age>40"; SQLQuery query = new SQLQuery(Employee.class, querystr); Employee results[] = space.readMultiple(query , Integer.MAX_VALUE);
Clear Clear ObjectsYou can use the SQLQuery with the GigaSpace.clear to remove objects from the space: GigaSpace space; String querystr = "age>30"; SQLQuery query = new SQLQuery(Employee.class, querystr); space.clear(query);
Updating an Object Updating an ObjectThe GigaSpace.write with the modifier should be used to explicitly perform an update operation. The 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.
The GigaSpace.write has a few activity modes - With each mode the return object options are different.:
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. UPDATE_OR_WRITE Example: try { LeaseContext ret = space.write(employee ,/*lease*/ 0 ,/*timeout*/ 1000 , UpdateModifiers.UPDATE_OR_WRITE); if ( ret.getObject() == null) { // successful write } if (ret.getObject() instanceof Employee) { // successful update } } catch (UpdateOperationTimeoutException uote) { // Object is locked - unsuccessful update } WRITE_ONLY Example: try { LeaseContext ret = space.write(employee ,/*lease*/ 0 ,/*timeout*/ 1000 , UpdateModifiers.WRITE_ONLY); if ( ret.getObject() == null) { // successful write } } catch (EntryAlreadyInSpaceException eainse) { // Object already exists - unsuccessful write } UPDATE_ONLY Example: try { LeaseContext ret = space.write(employee ,/*lease*/ 0 ,/*timeout*/ 1000 , UpdateModifiers.UPDATE_ONLY); if ( ret == null) { // Object is locked - unsuccessful update } else if (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. } PARTIAL_UPDATE Example: GigaSpace space = new GigaSpaceConfigurer (new UrlSpaceConfigurer("jini://*/*/mySpace").noWriteLease(true)).gigaSpace(); // initial insert MyClass obj = new MyClass(); obj.setId("1"); obj.setField1("A"); obj.setField2("B"); obj.setField3("C"); space.write(obj); // reading object back from the space MyClass obj2 = space.readById(MyClass.class , "1"); // updating only field2 obj2.setField1(null); obj2.setField2("BBBB"); obj2.setField3(null); try { space.write(obj2,0,0,UpdateModifiers.PARTIAL_UPDATE); } catch (EntryNotInSpaceException enise) { // Object not in space - unsuccessful update } Batch Update Batch Update
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:
|
![]() |
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence |