JSON-RPC Cross language calls between Golang and Java __js

Source: Internet
Author: User
Tags throwable

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.