[英]Make an rpc call from Erlang/Elixir to Java via JInteface
是否可以使用JInterface從erlang到java進行rpc:call
?
如果是,那么call函數中的Module
參數應該設置為什么?
call(Node, Module, Function, Args) -> Res | {badrpc, Reason}
我的工作是這個(簡單的消息發送,見下面的代碼):
> {javambox, server@javaapp} ! {self(), greet, <<"Hello man">>}.
> flush().
Shell got {bye,10000}
但不是rpc電話。 這是我的嘗試:
> rpc:call(server@javaapp, javambox, greet, <<"Hello man">>, 1000).
{badrpc,timeout}
MyInterface.java:
import com.ericsson.otp.erlang.*;
import java.lang.reflect.InvocationTargetException;
public class MyInterface {
OtpErlangPid from = null;
OtpMbox myOtpMbox = null;
public static void main(String[] args) {
MyInterface i = new MyInterface();
}
public MyInterface() {
setupMBox();
}
private void setupMBox() {
System.out.println("Setting up mbox");
try {
// server@java-app??
OtpNode myOtpNode = new OtpNode("server");
myOtpNode.setCookie("secret");
myOtpMbox = myOtpNode.createMbox("javambox");
System.out.println("System ready to accept messages.");
System.out.println("Hostname is:");
System.out.println(java.net.InetAddress.getLocalHost().getHostName() );
System.out.println("List of known names:");
System.out.println(String.join(" , ", myOtpNode.getNames()));
System.out.println("Secret cookie is:");
System.out.println(myOtpNode.cookie());
while (true) {
OtpErlangTuple tuple = (OtpErlangTuple) myOtpMbox.receive();
System.out.println("GOT MESAGE!");
from = (OtpErlangPid) tuple.elementAt(0);
OtpErlangAtom dispatch = (OtpErlangAtom) tuple.elementAt(1);
if (dispatch.toString().equals("settext")) {
final OtpErlangBinary message = (OtpErlangBinary) tuple.elementAt(2);
System.out.println("Setting text to: " + new String(message.binaryValue()));
} else if (dispatch.toString().equals("greet")) {
final OtpErlangBinary message = (OtpErlangBinary) tuple.elementAt(2);
// Send reply
OtpErlangAtom myAtom = new OtpErlangAtom("bye");
OtpErlangObject[] reply = new OtpErlangObject[2];
reply[0] = myAtom;
reply[1] = new OtpErlangInt(10000);
OtpErlangTuple myTuple = new OtpErlangTuple(reply);
myOtpMbox.send(from, myTuple);
System.out.println("Greet got, bye!");
} else{
System.out.println("Got unexpected message....");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Setting up mbox
System ready to accept messages.
Hostname is:
javaapp
List of known names:
javambox
Secret cookie is:
secret
GOT MESAGE!
Greet got, bye!
Java端沒有模塊的概念,因此您可以在RPC中使用任何名稱。 檢查來源以查看如何將呼叫編碼為消息,並且不要忘記發送回復。 我發現簡單消息的推理更容易,但也許你不想關心遠程節點是erlang還是java。 希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.