|
Summary: GigaSpaces JavaSpaces API Plain Old Java Object support - the POJO.
Overview
The Jini/JavaSpaces specification defines the Entry to be used in distributed algorithms for which exact-match lookup semantics are useful. An Entry is a typed set of objects, each of which may be tested for an exact match with a template. A service that uses Entries supports methods that let you use Entry objects. The term "operation" is also used to describe such methods. There are three types of operations:
It is possible for a single method to provide more than one of the operation types. For example, a method that returns an Entry that matches a given template can be logically split into two operation types (match and fetch). In this way, any statements made in this specification about either operation type apply to the appropriate part of the method's behavior. An Entry is a typed group of object references represented by a class that implements the marker interface net.jini.core.entry.Entry. Two different Entries have the same type only if they are of the same class. package net.jini.core.entry; public interface Entry extends java.io.Serializable { }
Other fields of an Entry are not affected by Entry operations. In particular, when an Entry object is created and filled in by a fetch operation; only the public, non-static, non-transient, and non-final fields of the Entry are set. Other fields are not affected, except as set by the class's no-arg constructor. Each Entry class must provide a public no-arg constructor. Entries cannot have fields of a primitive type (int, boolean, etc.), however, the objects they refer to may have primitive fields and non-public fields. When performing any type of operation, an attempt to use a malformed Entry type that has primitive fields, or does not have a no-arg constructor, throws IllegalArgumentException.
JavaSpaces API supportThe GigaSpaces POJO JavaSpaces support allows you to perform JavaSpaces operations using Java objects that:
The following JavaSpaces APIs have been modified to support POJOs:
The methods that were added into the IJSpace APIs to support POJO are:
ExamplesPerson ClassPerson package com.j_spaces.examples.hellospacepojo; import com.gigaspaces.annotation.pojo.SpaceClass; import com.gigaspaces.annotation.pojo.SpaceField; @SpaceClass(replicate=true,persist=false,fifo=false) public class Person { private String lastName; private String firstName; public Person(){} public Person(String lastName, String firstName) { this.lastName = lastName; this.firstName = firstName; } @SpaceProperty(index=IndexType.BASIC) private String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } private String getLastName() { return lastName; } public void setLastName(String name) { this.lastName = name; } public boolean equals(Object other) { if (!(other instanceof Person)) return false; else { Person otherBean = (Person) other; return ((otherBean.lastName != null && otherBean.lastName.equals(lastName) || otherBean.lastName == lastName)) && ((otherBean.getFirstName() != null && otherBean.getFirstName().equals(getFirstName()) || otherBean.getFirstName() == getFirstName())); } } public String toString() { return " \tlastName: " + lastName + ", \tfirstName: " + firstName; } } Employee ClassEmployee package com.j_spaces.examples.hellospacepojo; import com.gigaspaces.annotation.pojo.Key; import com.gigaspaces.annotation.pojo.SpaceClass; import com.gigaspaces.annotation.pojo.SpaceField; @SpaceClass(replicate=true,persist=false,fifo=false) public class Employee extends Person { private Integer employeeID; public Employee(){} public Employee(Integer employeeID) { this.employeeID = employeeID; } public Employee(String lastName, Integer employeeID) { this.employeeID = employeeID; setLastName(lastName); } @SpaceId private Integer getEmployeeID() { return employeeID; } private void setEmployeeID(Integer employeeID) { this.employeeID = employeeID; } public boolean equals(Object other) { if (!(other instanceof Employee)) return false; else { Employee otherBean = (Employee) other; return ((otherBean.getEmployeeID() != null && otherBean.getEmployeeID().equals(getEmployeeID()) || otherBean.getEmployeeID() == getEmployeeID())); } } public String toString() { return super.toString() + ", \temployeeID: " + employeeID; } } Writing and Reading POJOsThe following writes an Employee object, and reads it back using a template: IJSpace space = (IJSpace) SpaceFinder.find(args[0]); Employee employee = new Employee("Last Name", new Integer(32)); employee.setFirstName("first name"); space.write(employee, null, Lease.FOREVER); Employee templatePojo = new Employee(); Employee result = (Employee) space.read(templatePojo,null,Long.MAX_VALUE); Registering for NotificationsThe notify registration: session.addListener(templatePojo,new HelloWorldPOJONotifyDelegator(),Lease.FOREVER, null,null, NotifyActionType.NOTIFY_ALL); The listener: public class HelloWorldPOJONotifyDelegator implements RemoteEventListener { public void notify(RemoteEvent theEvent) throws UnknownEventException, RemoteException { try { // since we are using NotifyDelegator, we can obtain the entry // that triggered the event EntryArrivedRemoteEvent arrivedRemoteEvent = (EntryArrivedRemoteEvent) theEvent; Employee backToPojo = (Employee) arrivedRemoteEvent.getObject(); int notifyType = arrivedRemoteEvent.getNotifyType(); String msgStr = "NOTIFY- Type:" + getNotifyDesc(notifyType) + "; " + "Event-Sequence#: " + theEvent.getSequenceNumber() + "; " + "Content: '" + backToPojo + "';"; System.out.println(msgStr); } catch (Exception ex) { ex.printStackTrace(); } } private String getNotifyDesc(int notifyType) { String desc = ""; if (NotifyModifiers.isWrite(notifyType)) desc = "Write"; if (NotifyModifiers.isTake(notifyType)) desc = "Take"; if (NotifyModifiers.isLeaseExpiration(notifyType)) desc = "LeaseExpiration"; if (NotifyModifiers.isUpdate(notifyType)) desc = "Update"; if (NotifyModifiers.isALL(notifyType)) desc = "ALL"; return desc; } } TransactionsLocalTransactionManager trManager = (LocalTransactionManager) LocalTransactionManager. getInstance((IJSpace) space); Transaction.Created tCreated = TransactionFactory.create( trManager, Transaction txn = tCreated.transaction; Employee employee = new Employee("Last Name", new Integer(32)); space.write(employee, txn, Lease.FOREVER); txn.commit(10000); SQLQueryString querystr = "employeeID=1 or employeeID=2"; SQLQuery query = new SQLQuery(Employee.class.getName(), querystr); Object result1[] = (Object[]) space.readMultiple((Object)query,null,Integer.MAX_VALUE);
POJO UID Generation and Usage ScenariosInserting POJOs into the SpaceYou can insert POJOs into the space using the write() and writeMultiple() methods. Space UID for POJOs can be created in three different ways:
Fetching POJOs from the SpacePOJOs can be fetched from the space using the methods read(), readMultiple(), readIfExists(), take(), takeMultiple(), takeIfExists(). A POJO can be fetched from the space in three different ways:
Considerations, Known Issues and Limitations
|
(works on Firefox 2 and Internet Explorer 7)