Yes, the code is same, but with no filter it is giving below exception
[severe 2013/07/18 12:38:39.454 MDT FunctionExecutionPeer2 <Function Execution Processor1> tid=0x32] UnExpected exception during function execution on local node Partitioned Region
- java.lang.NullPointerException
at com.gemstone.gemfire.tutorial.storage.MultiGetFunction.execute(MultiGetFunction.java:58)
at com.gemstone.gemfire.internal.cache.execute.AbstractExecution.executeFunctionLocally(AbstractExecution.java:341)
at com.gemstone.gemfire.internal.cache.execute.AbstractExecution$1.run(AbstractExecution.java:275)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at com.gemstone.gemfire.distributed.internal.DistributionManager.runUntilShutdown(DistributionManager.java:687)
at com.gemstone.gemfire.distributed.internal.DistributionManager$8$1.run(DistributionManager.java:1111)
at java.lang.Thread.run(Thread.java:662)
Please find the complete listing of code
- Class MultiGetFunction
publicvoid execute(FunctionContext fc) {
if(fc instanceof RegionFunctionContext){
RegionFunctionContext context = (RegionFunctionContext)fc;
Set keys = context.getFilter();
Set keysTillSecondLast = newHashSet();
int setSize = keys.size();
Iterator keysIterator = keys.iterator();
for (int i = 0; i < (setSize - 1); i++) {
keysTillSecondLast.add(keysIterator.next());
}
for (Object k : keysTillSecondLast) {
context.getResultSender().sendResult(
(Serializable)PartitionRegionHelper.getLocalDataForContext(context)
.get(k));
}
Object lastResult = keysIterator.next();
context.getResultSender().lastResult(
(Serializable)PartitionRegionHelper.getLocalDataForContext(context)
.get(lastResult));
}else {
fc.getResultSender().lastResult(Runtime.getRuntime().freeMemory()/(1024*1024));
}
}
@Override
public String getId() {
return getClass().getName();
}
}
- Class MyArrayListCollector
publicclass MyArrayListResultCollector implements
ResultCollector<Object,Object> {
final ArrayList<Object> result = new ArrayList<Object>();
publicvoid addResult(DistributedMember memberID,
Object resultOfSingleExecution) {
this.result.add(resultOfSingleExecution);
}
public Object getResult() throws FunctionException {
returnthis.result;
}
public Object getResult(long timeout, TimeUnit unit)
throws FunctionException, InterruptedException {
returnthis.result;
}
publicvoid clearResults() {
result.clear();
}
publicvoid endResults() {}
- Class FunctionExecutionPeer1
publicclass FunctionExecutionPeer1 {
publicstaticfinal String EXAMPLE_REGION_NAME = "gemfireRegion";
publicstaticvoid main(String[] args) throws Exception {
new FunctionExecutionPeer1().populateDataToFunctionExecution();
}
publicvoid populateDataToFunctionExecution() throws Exception {
System.out.println("Peer to which other peer sends request for function Execution");
System.out.println("Connecting to the distributed system and creating the cache... ");
// Create the cache which causes the cache-xml-file to be parsed
Cache cache = new CacheFactory()
.set("name", "FunctionExecutionPeer1")
.set("cache-xml-file", "D:/9_19_Release/GEMFIRE/spring-tool-suite-3.2.0.RELEASE-e3.8.2-win32/springsource/sts-3.2.0.RELEASE/testgemfire/src/main/resources/xml/FunctionExecution.xml")
.create();
// Get the exampleRegion
Region<String, Object> exampleRegion = cache.getRegion(EXAMPLE_REGION_NAME);
System.out.println("Example region>>PAth \"" + exampleRegion.getFullPath()
+ "\" created in cache.");
System.out.println("Populating the data into gemfireRegion");
// Populate the region
Map<String,Map<String,Object>> templateMap = LoadData.loadDataFromFile();
long start = System.nanoTime();
//populateDataInRegion(templateMap,exampleRegion);
exampleRegion.put("templateMap", templateMap);
long end = System.nanoTime();
System.out.println("Total time to populate region is"+(end-start));
System.out.println("Registering the function MultiGetFunction on Peer");
MultiGetFunction function = new MultiGetFunction();
FunctionService.registerFunction(function);
System.out.println("Please start Other Peer And Then Press Enter to continue.");
/*System.out.println("Closing the cache and disconnecting.");
cache.close();*/
}
- Class FunctionExecutionPeer2
public class FunctionExecutionPeer2 {
public static final String EXAMPLE_REGION_NAME = "gemfireRegion";
public static void main(String[] args) throws Exception {
fetchValuesUsingFunctionExectuion();
System.out.println("Executed the client and returned the value");
}
private static void fetchValuesUsingFunctionExectuion() throws Exception {
System.out.println("Peer sending function Execution request to other peer as well as executing function on its own region");
System.out.println("Connecting to the distributed system and creating the cache... ");
System.out.println("The path of xml file is");
// Create the cache which causes the cache-xml-file to be parsed
Cache cache = new CacheFactory()
.set("name", "FunctionExecutionPeer2")
.set("cache-xml-file", "D:/9_19_Release/GEMFIRE/spring-tool-suite-3.2.0.RELEASE-e3.8.2-win32/springsource/sts-3.2.0.RELEASE/testgemfire/src/main/resources/xml/FunctionExecution.xml")
.create();
// Get the exampleRegion
Region<String, String> exampleRegion = cache.getRegion(EXAMPLE_REGION_NAME);
System.out.println("Example region \"" + exampleRegion.getFullPath()
+ "\" created in cache.");
Set<String> inputKeySet = new HashSet<String>();
inputKeySet.add("templateMap");
System.out.println("Example region \"" + exampleRegion.getFullPath()
+ "\" is populated.");
MultiGetFunction functionId = new MultiGetFunction();
ResultCollector rc = FunctionService.onRegion(exampleRegion).withFilter(inputKeySet).withCollector(new MyArrayListResultCollector()).execute(functionId);
Object result = rc.getResult();
System.out.println("Function executed successfully. Now getting the result");
//List result = (List)rc.getResult();
if (result!= null) {
ArrayList list = (ArrayList)result;
for (Object object :list) {
Map<String, Map<String, Object>> templateMap = (Map<String, Map<String, Object>>) object;
if (templateMap != null && !templateMap.isEmpty()) {
System.out.println("Got result with size >>>>"
+ templateMap.size() + ".");
for (Map.Entry<String, Map<String, Object>> map : templateMap
.entrySet()) {
System.out.println(map.getKey());
}
}
}
}
cache.close();
}
- XML FunctioonExecution
<?xmlversion="1.0"?>
<!DOCTYPEcachePUBLIC
"-//GemStone Systems, Inc.//GemFire Declarative Caching 6.6//EN"
"cache6_6.dtd">
<!--
| FunctionExecution.xml
|
-->
<cache>
<regionname="gemfireRegion">
<region-attributesrefid="PARTITION">
<partition-attributeslocal-max-memory="50"redundant-copies="0"total-num-buckets="13"/>
</region-attributes>
</region>
</cache>