|
Summary: A wrapper that allows any Java class to be written to the space, even if it does not comply with JavaSpaces requirements.
Generic Access to the SpaceThe ExternalEntry class (com.j_spaces.core.client.ExternalEntry; see Javadoc) class, an implementation of the net.jini.core.entry.Entry interface, provides special functionality that goes beyond the JavaSpaces specification.
Turns Any Class into a Jini EntryThe primary use of the ExternalEntry class is as a wrapper that allows any Java class to be written to a space. The JavaSpaces specification sets some requirements for classes and objects to be used in a space - Jini Entries must have a default empty constructor, all fields as objects, no private fields, and must implement the Entry interface class. While new code can be written with these requirements in mind, existing classes must be adapted or rewritten before they can be written to a space. The ExternalEntry solves this problem by creating a JavaSpaces-compatible wrapper that holds the complete meta-data of a class. This allows for any class, even one that does not comply with the requirements of JavaSpaces, to be written to a space as a Jini Entry. An existing information structure can be easily imported into GigaSpaces.
Sharing Objects Between Java , C# and C++When using ExternalEntry with Entry classes your ExternalEntry objects should have their field names/types/values array ordered in alphabetically order based on the field names. This is important when having Java applications sharing objects with C# and C++ applications. For example, lets take the following Entry class: public class MyClass implements Entry { public MyClass(){} Public Long a; Public String b; Public Long c; } The corresponding ExternalEntry should be declared in the following way: ExternalEntry msg = new ExternalEntry ("MyClass", values, new String[] {"a","b","c"} , new String[]{"java.lang.Long" , "java.lang.String", "java.lang.Long"}); Refers to Objects by Unique IdentifiersExternalEntry allows locating objects using their Unique ID (UID), as a powerful alternative to the standard methods offered by JavaSpaces. Identifying objects by UID is often faster and more accurate than matching and inheritance mechanism.
Using the ExternalEntry ClassThis section explains how to use ExternalEntry, in combination with other GigaSpaces methods, to read and write objects to a space.
All procedures referring to the read operation can also be applied to the take operation.
To write a new class to the space:
To write a new class instance to the space:
To read a single object from the space, querying by meta data:
To read a single object from the space, querying by UID:
To read multiple objects from the space, querying by UID:
Hello World ExampleThe Hello World sample application demonstrates a simple exchange of objects in a space. It creates a new message Entry object, writes it to the space and then reads a copy of it. The following figure shows a modified version of this example, which uses ExternalEntry (Constructor #4) to wrap the class Employee. import com.j_spaces.core.client.SpaceFinder; import com.j_spaces.core.client.ExternalEntry; import net.jini.core.lease.Lease; import net.jini.space.JavaSpace; public class HelloWorldXE { public static Void main(String[] args) { if ( args.length != 1 ) { System.out.println("Usage: <URL>"); System.out.println("jini://lookup host/container name/JavaSpaces"); System.exit(1); } try { String[] fields = new String[2]; String[] types= new String[2]; Object[] values = new Object[2]; fields[0] = "id"; values[0] = "12345"; fields[Look And Feel - ServiceGrid] = "name"; values[Look And Feel - ServiceGrid] = "John"; types[0] = "java.lang.String"; types[Look And Feel - ServiceGrid] = "java.lang.String"; ExternalEntry msg = new ExternalEntry ("Employee", values, fields,types); JavaSpace space = (JavaSpace)SpaceFinder.find( args[0] ); Lease ls = space.write(msg, null, Lease.FOREVER); ExternalEntry template = new ExternalEntry("Employee", null, null); ExternalEntry result = (ExternalEntry )space.read(template, null, Long.MAX_VALUE); System.out.println(result.m_FieldsNames[0] + " : " + result.m_FieldsValues[0]); ls.cancel(); System.exit(0); } catch (Exception e) { e.printStackTrace(); } } } Range Query ExampleThe following example demonstrates range query using the ExternalEntry. The MatchCodes and the ExternalEntry.setExtendedMatchCodes are using to define lower and upper limits for the range. Make sure your Entry classes include the __getSpaceIndexedFields method to index the appropriate field. The ExternalEntry in this case includes only the field you want to use for the query - i.e. the if field. select * from MyEntry where id>=12 and id<=34 The Entry Class: package com.gigaspaces.example.range; import net.jini.core.entry.Entry; public class MyEntry implements Entry { public Integer id; public String name; public MyEntry() { } public MyEntry(Integer anID, String aName) { this.id = anID; this.name = aName; } public static String[] __getSpaceIndexedFields() { String[] indexedFields = { "id" }; return indexedFields; } public String toString() { return "MyEntry [id=" + id + ",name=" + name + "]"; } } The application: package com.gigaspaces.example.range; import net.jini.core.entry.Entry; import net.jini.core.lease.Lease; import com.j_spaces.core.IJSpace; import com.j_spaces.core.client.ExternalEntry; import com.j_spaces.core.client.SpaceFinder; import com.j_spaces.core.client.TemplateMatchCodes; public class RangeExample { private static final int NUM_ENTRIES = 1000; public static void main(String[] args) { if (args.length != 1) { System.out.println("Usage: <spaceURL>"); System.out.println("<protocol>://host:port/containername/spacename"); System.exit(1); } try { // find the space with RMI, the Jini lookup or connect to an // embedded // instance IJSpace space = (IJSpace) SpaceFinder.find(args[0]); if (space == null) { System.out.println("Space not found: " + args[0]); System.exit(-1); } System.out.println("Connected to sapce"); MyEntry[] entries = new MyEntry[NUM_ENTRIES]; for (int i = 0; i < NUM_ENTRIES; ++i) { MyEntry mye = new MyEntry(i, "my entry #" + i); entries[i] = mye; } System.out.println("Going to write " + entries.length + " entries to the sapce"); space.writeMultiple(entries, null, Lease.FOREVER); // Lower limit String[] fields = new String[Look And Feel - ServiceGrid]; Object[] values = new Object[Look And Feel - ServiceGrid]; fields[0] = "id"; values[0] = 12; ExternalEntry q = new ExternalEntry("com.gigaspaces.rangeexample.MyEntry", values,fields); // match code short[] m = new short[Look And Feel - ServiceGrid]; m[0] = TemplateMatchCodes.GE; q.setExtendedMatchCodes(m); // Upper limit Object[] range = new Object[Look And Feel - ServiceGrid]; range[0] = 34; q.setRangeValues(range); System.out.println("Running query"); Entry results[] = space.readMultiple(q, null,1000); System.out.println("Got " + results.length + ", number of entries back"); System.out.println("Printing results..."); for(int i = 0 ; i < results.length ; ++i) { Entry e = ((ExternalEntry)results[i]).getEntry(space); System.out.println(e); } } catch (Exception e) { e.printStackTrace(); } } }
More in This Section |
(works on Firefox 2 and Internet Explorer 7)