通过JInteface从Erlang / Elixir进行rpc调用

时间:2017-06-28 19:57:09

标签: erlang elixir jinterface

是否可以使用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}

完整Java代码

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();
        }
    }
}

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!

1 个答案:

答案 0 :(得分:0)

Java端没有模块的概念,因此您可以在RPC中使用任何名称。检查来源以查看如何将呼叫编码为消息,并且不要忘记发送回复。我发现简单的消息更容易推理,但也许你不想在乎远程节点是erlang还是java。希望这可以帮助。