|
Summary: Client applications that use the Map API to read POJO data from a space and, if the data is not found in the space, require the space to read from a different datasource, which can be a database or any other type of external application.
OverviewThis section discusses client applications that use the Map API to read POJO data from a space and, if the data is not found in the space, require the space to read from a different datasource, which can be a database or any other type of external application. The Map API read space operation results in a call to the CacheLoader Interface user implementation.
The examples of the CacheLoader method implementation in this section use the JDBC API to access RDBMS.
Reading a Single ObjectThis section discusses how to read or remove a single POJO object.
This section focusses on the CacheLoader.load call. The write-through section discusses the CacheStore.erase method.
The figure illustrates how a client application reads a single Entry from the space where the actual data is loaded from a database or other external application.
CacheLoader.load ImplementationThe CacheLoad Interface method load is called by the space and has the following signature: Object loadedObject = load (IGSEntry key)
The object data is extracted from the IGSEntry key parameter using the MapEntry Interface: IGSEntry.MapEntry mEntry = key.getMapEntry(); Person key = (Person)mEntry.getKey(); The data from the individual properties of the Entry object can now be extracted and JDBC Query created to fetch the desired data from the database. Sample CodeClient application: Map API : public class Person implements Serializable{ String firstName; String lastName; int id; // must have empty constructor public Person(){} public Person(String firstName, String lastName, int id) { this.firstName = firstName; this.lastName = lastName; this.id = id; } public String getFirstName() {return firstName;} public void setFirstName(String firstName) { this.firstName = firstName; } public int getId() {return id;} public void setId(int id) {this.id = id;} public String getLastName() {return lastName;} public void setLastName(String lastName) { this.lastName = lastName; } } The application code: Person JohnDoe = IMap.get(person(null, null, 27), waitForResponse); CacheLoader implementation: load method: MyCacheLoader extends AbstractCacheLoader { public Object load(Object key) { Object values[] = null; Person loaded_person = null; System.out.println(" ********* load " + key); Connection con = null; try { con = getConnection(); int keyValue = 0; Map.Entry mEntry = ((IGSEntry) key).getMapEntry(); if (mEntry != null) { keyValue = ((Integer) mEntry.getKey()).intValue(); } else { keyValue = ((Integer) getId((IGSEntry) key)).intValue(); } PreparedStatement stP = con.prepareStatement("select * from " + tableNames.get(Person.class.getName()) + " where ID = ? "); stP.setInt(1, keyValue); ResultSet rs = stP.executeQuery(); int sz = rs.getMetaData().getColumnCount(); values = new Object[sz]; while (rs.next()) { for (int i = 0; i < sz; i++) { values[i] = rs.getObject(i + 1); } // HERE WE MAP table row to Person object loaded_person = new Person(String.valueOf(values[0]), String .valueOf(values[1]), Integer.valueOf(String .valueOf(values[2]))); break; } rs.close(); con.close(); } catch (Exception e) { try { con.close(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } e.printStackTrace(); } return getConvertor().toIGSEntry(loaded_person); } } Reading Multiple ObjectsThis section describes how to read multiple POJO objects.
The figure illustrates how a client application reads a fixed number of POJO Entries from the space where the actual data is loaded from a database or other external application.
CacheLoader.load ImplementationThe CacheLoad Interface method load is called repeatedly by the space and has the following signature: Object loadedObject = load (IGSEntry)
The object key data is extracted from the IGSEntry by: key = IGSEntry.getKey() The data from the individual properties of the Entry object can now be extracted and an SQLQuery created using the JDBC API. Sample Code
In client application: Map API Using POJO object: Map personMap = IMap.getAll(personKeys); Special Read OperationsThe following read operations can be used with the Map API.
|
Wiki Content Tree
Your Feedback Needed!
We need your help to improve this wiki site. If you have any suggestions or corrections, write to us at techw@gigaspaces.com. Please provide a link to the wiki page you are referring to.


Add Comment