This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
/
FunctionExecutionPeer2.java
102 lines (80 loc) · 3.49 KB
/
FunctionExecutionPeer2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package quickstart.function.execution;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.stereotype.Component;
import quickstart.MultiGetFunction;
import quickstart.MyArrayListResultCollector;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.execute.Execution;
import com.gemstone.gemfire.cache.execute.FunctionService;
import com.gemstone.gemfire.cache.execute.ResultCollector;
/**
* In this example of peer-to-peer function execution, one peer sends a request
* for function execution to another peer. FunctionExecutionPeer2 creates a region,
* populates the region and sends a function execution request to FunctionExecutionPeer1
* while simultaneously executing the function on its own region.
* It collects the result from its own execution as well as from FunctionExecutionPeer1.
* Please refer to the quickstart guide for instructions on how to run this
* example.
*
* @author GemStone Systems, Inc.
* @since 6.0
*/
@Component
public class FunctionExecutionPeer2 {
@Resource(name = "gemfireTemplate2")
private GemfireTemplate exampleRegion;
private final BufferedReader stdinReader;
public FunctionExecutionPeer2() {
this.stdinReader = new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) throws Exception {
new FunctionExecutionPeer2().run();
}
public void run() throws Exception {
writeToStdout("Peer sending function Execution request to other peer as well as executing function on its own region");
writeToStdout("Connecting to the distributed system and creating the cache... ");
// Get the exampleRegion
writeToStdout("Example region \"" + exampleRegion.getRegion().getFullPath()
+ "\" created in cache.");
// Populate the region
for (int i = 0; i < 20; i++) {
exampleRegion.put("KEY_" + i, "VALUE_" + i);
}
writeToStdout("Example region \"" + exampleRegion.getRegion().getFullPath()
+ "\" is populated.");
writeToStdout("Press Enter to continue.");
stdinReader.readLine();
writeToStdout("Executing Function : MultiGetFunction on region \""
+ exampleRegion.getRegion().getFullPath()
+ "\" with filter size " + 3 + " and with MyArrayListResultCollector.");
MultiGetFunction function = new MultiGetFunction();
FunctionService.registerFunction(function);
writeToStdout("Press Enter to continue.");
stdinReader.readLine();
Set<String> keysForGet = new HashSet<String>();
keysForGet.add("KEY_4");
keysForGet.add("KEY_9");
keysForGet.add("KEY_7");
Execution execution = FunctionService.onRegion(exampleRegion.getRegion()).withFilter(
keysForGet).withArgs(Boolean.TRUE).withCollector(
new MyArrayListResultCollector());
ResultCollector rc = execution.execute(function);
writeToStdout("Function executed successfully. Now getting the result");
List result = (List)rc.getResult();
writeToStdout("Got result with size " + result.size() + ".");
writeToStdout("Press Enter to continue.");
stdinReader.readLine();
System.out.println("Closing the cache and disconnecting.");
}
private static void writeToStdout(String msg) {
System.out.println(msg);
}
}