Skip to content

Instantly share code, notes, and snippets.

@leandrosilva
Created June 7, 2012 07:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leandrosilva/2887043 to your computer and use it in GitHub Desktop.
Save leandrosilva/2887043 to your computer and use it in GitHub Desktop.
My MessagePack-RPC sample: Clojure Server & Java Client
package msgpack.rpc.sample.client;
import org.msgpack.rpc.Client;
import org.msgpack.rpc.loop.EventLoop;
public class ClientApp {
private static class SpawnRequest {
private SpawnRequest(final int clientCount, final int requestCount, final RPCInterface iface) {
new Thread(new Runnable() {
public void run() {
for (int rc = 1; rc <= requestCount; rc++) {
System.out.println("(" + clientCount + ":" + rc + ") 10 + 2 = " + iface.add(10, 2));
System.out.println("(" + clientCount + ":" + rc + ") 10 - 2 = " + iface.sub(10, 2));
System.out.println("(" + clientCount + ":" + rc + ") 10 * 2 = " + iface.mul(10, 2));
System.out.println("(" + clientCount + ":" + rc + ") 10 / 2 = " + iface.div(10, 2));
}
}
}).start();
}
}
public static interface RPCInterface {
int add(int a, int b);
int sub(int a, int b);
int mul(int a, int b);
double div(int a, int b);
}
public static void main(String[] args) throws Exception {
EventLoop loop = EventLoop.defaultEventLoop();
Client client = new Client("localhost", 1978, loop);
RPCInterface iface = client.proxy(RPCInterface.class);
final int CLIENT_COUNT = 1;
final int REQUEST_COUNT = 1;
System.out.println("Spawning " + CLIENT_COUNT + " clients to perform " + REQUEST_COUNT + " requests each");
for (int cc = 1; cc <= CLIENT_COUNT; cc++) {
new SpawnRequest(cc, REQUEST_COUNT, iface);
}
}
}
...
<repositories>
<repository>
<id>msgpack.org</id>
<name>MessagePack Repository for Maven</name>
<url>http://msgpack.org/maven2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.msgpack</groupId>
<artifactId>msgpack-rpc</artifactId>
<version>0.6.1-devel</version>
</dependency>
</dependencies>
...
(defproject msgpackrpc-sample "0.1.0-SNAPSHOT"
:description "A sample of MessagePack-RPC"
:dependencies [[org.clojure/clojure "1.4.0"]
[org.msgpack/msgpack-rpc "0.6.1-devel"]]
:repositories {"MessagePack Repository for Maven" "http://msgpack.org/maven2/"})
(ns msgpackrpc-sample.server
(:import [org.msgpack.rpc.loop EventLoop])
(:import [org.msgpack.rpc Server]))
(gen-class
:name MathServer
:methods [[add [int int] int]
[sub [int int] int]
[mul [int int] int]
[div [int int] double]])
(defn -add [this a b]
(+ a b))
(defn -sub [this a b]
(- a b))
(defn -mul [this a b]
(* a b))
(defn -div [this a b]
(/ a b))
(defn -main
"Starts a MessagePack-RPC based server"
[& args]
(let [loop (EventLoop/defaultEventLoop)]
(let [svr (Server.)]
(.serve svr (MathServer.))
(.listen svr 1978))
(println "MathServer running and listening on 1978")
(.join loop)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment