| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package jpype.proxy; |
| 17 |
|
| 18 |
import java.util.ArrayList; |
| 19 |
import java.util.Collection; |
| 20 |
import java.util.Collections; |
| 21 |
import java.util.LinkedList; |
| 22 |
import java.util.List; |
| 23 |
import java.util.concurrent.Callable; |
| 24 |
import java.util.concurrent.ExecutorService; |
| 25 |
import java.util.concurrent.Executors; |
| 26 |
import java.util.concurrent.Future; |
| 27 |
import java.util.concurrent.TimeUnit; |
| 28 |
|
| 29 |
public class ProxyExecutor |
| 30 |
{ |
| 31 |
|
| 32 |
private ExecutorService executor; |
| 33 |
|
| 34 |
private List<Future<Integer>> executedTasks = new ArrayList<Future<Integer>>(); |
| 35 |
private List<ProxyCaller> proxies = new ArrayList<ProxyCaller>(); |
| 36 |
|
| 37 |
public ProxyExecutor(int noOfThreads) |
| 38 |
{ |
| 39 |
executor = Executors.newFixedThreadPool(noOfThreads); |
| 40 |
} |
| 41 |
|
| 42 |
public void shutdown() |
| 43 |
{ |
| 44 |
executor.shutdown(); |
| 45 |
try |
| 46 |
{ |
| 47 |
|
| 48 |
if (!executor.awaitTermination(5, TimeUnit.SECONDS)) |
| 49 |
{ |
| 50 |
executor.shutdownNow(); |
| 51 |
|
| 52 |
if (!executor.awaitTermination(5, TimeUnit.SECONDS)) |
| 53 |
System.err.println("Pool did not terminate"); |
| 54 |
} |
| 55 |
} catch (InterruptedException ie) |
| 56 |
{ |
| 57 |
|
| 58 |
executor.shutdownNow(); |
| 59 |
|
| 60 |
Thread.currentThread().interrupt(); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
public void registerProxy(TestInterface4 proxy, int noOfExecutions) |
| 65 |
{ |
| 66 |
|
| 67 |
for (int i = 0; i < noOfExecutions; i++) |
| 68 |
{ |
| 69 |
proxies.add(new ProxyCaller(proxy)); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public int getExpectedTasks() |
| 74 |
{ |
| 75 |
return proxies.size(); |
| 76 |
} |
| 77 |
|
| 78 |
public void runExecutor() |
| 79 |
{ |
| 80 |
|
| 81 |
|
| 82 |
for (ProxyCaller proxy : proxies) |
| 83 |
{ |
| 84 |
Future<Integer> future = executor.submit(proxy); |
| 85 |
executedTasks.add(future); |
| 86 |
} |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
public int waitForExecutedTasks() |
| 91 |
{ |
| 92 |
int returns = 0; |
| 93 |
|
| 94 |
for (Future<Integer> task : executedTasks) |
| 95 |
{ |
| 96 |
try |
| 97 |
{ |
| 98 |
returns += task.get(); |
| 99 |
} catch (Exception ex) |
| 100 |
{ |
| 101 |
System.out.println("An exception has thrown during execution"); |
| 102 |
ex.printStackTrace(); |
| 103 |
} |
| 104 |
} |
| 105 |
return returns; |
| 106 |
} |
| 107 |
|
| 108 |
public class ProxyCaller implements Callable<Integer> |
| 109 |
{ |
| 110 |
|
| 111 |
private TestInterface4 proxy; |
| 112 |
|
| 113 |
public ProxyCaller(TestInterface4 proxy) |
| 114 |
{ |
| 115 |
this.proxy = proxy; |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public Integer call() throws Exception |
| 120 |
{ |
| 121 |
|
| 122 |
int result = 0; |
| 123 |
|
| 124 |
try |
| 125 |
{ |
| 126 |
int intValue = proxy.testMethodInt(); |
| 127 |
ReturnObject objectValue = proxy.testMethodObject(); |
| 128 |
String stringValue = proxy.testMethodString(); |
| 129 |
List<ReturnObject> listValue = proxy.testMethodList(5); |
| 130 |
} catch (Exception ex) |
| 131 |
{ |
| 132 |
ex.printStackTrace(); |
| 133 |
} |
| 134 |
|
| 135 |
return 1; |
| 136 |
} |
| 137 |
|
| 138 |
} |
| 139 |
} |