Skip to content

Commit

Permalink
testIPC client can now make parallel calls using executor service
Browse files Browse the repository at this point in the history
  • Loading branch information
lynus committed Jul 10, 2016
1 parent 81b8784 commit 846c17e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
5 changes: 4 additions & 1 deletion testIPC/my.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@
<name>ipc.rdma.bind.port</name>
<value>9090</value>
</property>

<property>
<name>client.task.size</name>
<value>5</value>
</property>
</configuration>
36 changes: 32 additions & 4 deletions testIPC/src/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.net.SocketFactory;

Expand All @@ -14,6 +18,8 @@
import server.TestConf;
public class Client {
public static void main(String[] args) {
final String TASK_THREAD_NUM_KEY = "client.task.size";
final int TASK_THREAD_NUM_DEFAULT = 1;
Configuration conf = new Configuration(false);
ClassLoader cl = TestConf.class.getClassLoader();
String p = cl.getResource("server/TestConf.class").getPath();
Expand All @@ -34,12 +40,34 @@ public static void main(String[] args) {
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
if (protocol != null) {
int i = protocol.mycall("haha");
System.out.println("get message: " + i);
RPC.stopProxy(protocol);

int nThreads = conf.getInt(TASK_THREAD_NUM_KEY, TASK_THREAD_NUM_DEFAULT);
ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
for (int i = 0; i < nThreads; i++) {
executorService.submit(new Task(i, protocol));
}
executorService.shutdown();
try {
executorService.awaitTermination(3, TimeUnit.SECONDS);
} catch (InterruptedException e) {}
RPC.stopProxy(protocol);
}

static class Task implements Runnable {
private int id;
private DemoProtocol proto = null;
public Task(int id, DemoProtocol proto) {
this.id = id;
this.proto = proto;
}
@Override
public void run() {
int i = proto.mycall("task #" + id);
System.out.println("Task #" + id + " get reply: " + i);
}

}

}

0 comments on commit 846c17e

Please sign in to comment.