是否可以使用JInterfacerpc:call
从 erlang 到 java ?
如果是,调用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}
完整的 Java 代码
我的接口.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();
}
}
}
Java 节点输出
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!