Implementing Java and. NET mutual calls via interfaces-jninterface
Using C # Programming for many years, but also very grateful to Microsoft in the language architecture, grammar sugar, editor, etc. to bring themselves to the convenience. But because of recent work in contact with Java, and gradually found that indeed, as everyone said, Java is very good, to find something almost all have ready-made, so naturally thought can be used. NET to call Java.
With understanding, there is a JNBridge software, can "Bridge any Java with any. NET, anywhere", perhaps very useful, but paid, do not like.
Also understand the other methods, are not common, support is not sound.
Of course, through the webservice certainly is possible, but has not liked the webservice bloated.
So I wrote a lightweight small component, Java and. NET, each of which contains a client and server, two of the clients can communicate with two of this version of the server.
In a nutshell, the server listens to the client's request, and when the request is received, a pre-registered handler is found, processed and returned to the client. (If this process is not understood, then it is not known to the HTTP, and this time there is always a web developer to me that HTTP is stateful, this part of the people should not understand ...) )
The process is very simple, the first step is to define the message body, can be understood as the HTTP protocol definition of the message body, HTTP request and response, there is naturally two similar things, and real life, like a lot of examples, such as someone asked You "you eat?" ", out of politeness, whether you eat or not eat, then the mood, should reply to others."
Here I define the Jninvokemessage and jnreturnmessage two message classes (in order to look clear, to post C # simplified code, Java is basically the same)
JNInvokeMessage.cs
| 1234567891011121314151617181920212223242526272829303132 |
publicclassJNInvokeMessage{ publicstringtargetName { get; set; } publicDictionary<string, object> parameters; publicJNInvokeMessage(stringtargetName) { this.targetName = targetName; } publicJNInvokeMessage setParam(stringkey, objectvalue) { lock(this) { if(parameters == null) parameters = newDictionary<string, object>(); } parameters[key] = value; returnthis; } publicobject getParam(stringkey) { if(parameters == null) returnnull; object obj; if(parameters.TryGetValue(key, outobj)) { returnobj; } returnnull; }} |
JNReturnMessage.cs
| 12345678 |
publicclassJNReturnMessage{ publicboolok { get; set; } publicstringerror { get; set; } public objectvalue { get; set; }} |
The next step is to define an interface contract, in C #, you can define a delegate:
| 1 |
publicdelegateJNReturnMessage IJNInterface(JNInvokeMessage invokeMessage); |
In Java, there is no delegation, so we define an interface:
| 123 |
publicinterfaceIJNInterface { JNReturnMessage invoke(JNInvokeMessage invokeMessage);} |
It's all the same thing. NET delegate is actually a class.
Next is the implementation of the client and server, basically are some communication logic, the old routines have on the Internet, no code, the main is the server has a interfaces, to save the interface processing program, The handler is equal to the action in the MVC controller. Use map in. NET with Dictionary,java:
| 1 |
privateDictionary<string, IJNInterface> interfaces = newDictionary<string, IJNInterface>(); |
| 1 |
privateMap<String, IJNInterface> interfaces = newHashMap<String, IJNInterface>(); |
The dictionary key is equivalent to the URL, and the only handler that determines the call.
Both sides of the open service and client calls are basically the same, the following shows the Java Open service, called in the. NET client:
Java Service
| 1234567891011121314151617181920 |
staticvoidstartServer() throwsIOException { JNServer server = newJNServer(); server.addInterface("test", invokeMessage -> { System.out.println(invokeMessage.toJson()); JNReturnMessage returnMessage = newJNReturnMessage(true, "hello .net"); return returnMessage; }); server.addInterface("home/index", newIJNInterface() { @Override publicJNReturnMessage invoke(JNInvokeMessage invokeMessage) { System.out.println(invokeMessage.toJson()); JNReturnMessage returnMessage = newJNReturnMessage(true, newint[]{1, 2, 3, 4, 5}); returnreturnMessage; } }); server.start();} |
. NET Client:
| 123456789101112 |
staticvoidMain(string[] args){ //new Thread(new ThreadStart(startServer)).Start(); //Thread.Sleep(1000); JNClient client = newJNClient(); JNInvokeMessage msg = newJNInvokeMessage("test"); varret = client.Invoke(msg); Console.WriteLine(ret.toJson());} |
The. NET console displays, indicating that we succeeded in calling:
If the call fails, the OK is false,error is the wrong description.
To register a Java program or. NET program as a service, it is recommended to use Srvany.exe on Windows, this is srvanyui_1.0 download, here are the Java Service settings for my test:
The program path is Java.exe, and the startup parameter is-jar "path to the service jar package"
Attention:
The message format is Json,java with Fastjson,. NET uses Newtonsoft.json, so if a complex object or collection array class is used in the return value or parameter, the deserialization should be jobject and Jarray in Fastjson Jsonobject and Jsonarray,newtonsoft.json.
Download:
Jninterface
Source:
Http://git.oschina.net/loogn/Loogn.JNInterface.JAVA
Http://git.oschina.net/loogn/Loogn.JNInterface.NET
Category:. NET
Java and. NET Mutual calls