1, Java through JNI call DLL library implementation and background C (c + +) communication code implementation or some trouble.
2, the emergence of Golang, so that some of the C code is not very familiar to the programmer to see the hope, although the current for Golang there are a lot of questioning the voice,
However, personally think that for the general background business, Golang is fully capable.
3, there is another advantage is that Java Golang background business is very convenient to call, the following an example to illustrate.
4, the Golang Service end code is as follows (RPCSERVER.GO):
Package main
//We create a simple Golang RPC server based on socket
import (
"FMT"
"NET"
net/r PC "
net/rpc/jsonrpc"
)
type Counter struct {
Sum int
}
func (this *counter) Add (i int, r *int) Error {this
. Sum + = i
*r = this. Sum
FMT. Printf ("I:%v", i) return
nil
}
func main () {
RPC. Register (New (Counter))
L, err: = Net. Listen ("TCP", ": 3333")
If Err!= nil {
FMT. Printf ("Listener TCP err:%s", err)
return
} for
{
FMT. Println ("wating ...")
conn, err: = L.accept ()
If err!= nil {
FMT. Sprintf ("Accept connection err:%s\n", conn)
} go
jsonrpc. SERVECONN (conn)
}
}
5, the Java implementation of the client code is as follows
Package com.test;
Import com.googlecode.jsonrpc4j.JsonRpcClient;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import java.net.*;
public class HelloWorld {public
static void Main (string[] args) {
try {
socket socket = new Socket ("192.168.1 10.157 ", 3333);
Jsonrpcclient client = new Jsonrpcclient ();
InputStream ips = Socket.getinputstream ();
OutputStream Ops = Socket.getoutputstream ();
int reply = Client.invokeandreadresponse ("Counter.add", New object[]{1001}, Int.class, OPS, IPS);
System.out.println ("Reply:" + reply);
} catch (IOException e) {
e.printstacktrace ();
} catch (Throwable throwable) {
throwable.printstacktrace ();
}
}
}
6, compile Golang server code and start the service side
1) Go build rpcserver.go
2)./rpcserver
root@docker1:/home/docker/xu/go-pro/java-go#./rpcserver
wating
...
wating ...
wating ...
i:1001wating ... i:1001
7, run the Java client program, the implementation of the results are as follows:
reply:2002
Experiment needed jar please visit the link: http://download.csdn.net/detail/xuguokun1986/9749486