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 |
public
class
JNInvokeMessage
{
public
string
targetName {
get
;
set
; }
public
Dictionary<
string
,
object
> parameters;
public
JNInvokeMessage(
string
targetName)
{
this
.targetName = targetName;
}
public
JNInvokeMessage setParam(
string
key,
object
value)
{
lock
(
this
)
{
if
(parameters ==
null
) parameters =
new
Dictionary<
string
,
object
>();
}
parameters[key] = value;
return
this
;
}
public
object getParam(
string
key)
{
if
(parameters ==
null
)
return
null
;
object obj;
if
(parameters.TryGetValue(key,
out
obj))
{
return
obj;
}
return
null
;
}
}
|
JNReturnMessage.cs
12345678 |
public
class
JNReturnMessage
{
public
bool
ok {
get
;
set
; }
public
string
error {
get
;
set
; }
public object
value {
get
;
set
; }
}
|
The next step is to define an interface contract, in C #, you can define a delegate:
1 |
public delegate JNReturnMessage IJNInterface(JNInvokeMessage invokeMessage); |
In Java, there is no delegation, so we define an interface:
123 |
public interface IJNInterface { 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 |
private Dictionary< string , IJNInterface> interfaces = new Dictionary< string , IJNInterface>(); |
1 |
private Map<String, IJNInterface> interfaces = new HashMap<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 |
static
void
startServer()
throws
IOException {
JNServer server =
new
JNServer();
server.addInterface(
"test"
, invokeMessage -> {
System.out.println(invokeMessage.toJson());
JNReturnMessage returnMessage =
new
JNReturnMessage(
true
,
"hello .net"
);
return returnMessage;
});
server.addInterface(
"home/index"
,
new
IJNInterface() {
@Override
public
JNReturnMessage invoke(JNInvokeMessage invokeMessage) {
System.out.println(invokeMessage.toJson());
JNReturnMessage returnMessage =
new
JNReturnMessage(
true
,
new
int
[]{
1
,
2
,
3
,
4
,
5
});
return
returnMessage;
}
});
server.start();
}
|
. NET Client:
123456789101112 |
static
void
Main(
string
[] args)
{
//new Thread(new ThreadStart(startServer)).Start();
//Thread.Sleep(1000);
JNClient client =
new
JNClient();
JNInvokeMessage msg =
new
JNInvokeMessage(
"test"
);
var
ret = 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