[erlang-questions] Calling Java from Erlang

Robert Raschke <>
Tue Jan 10 12:15:14 CET 2012


On Mon, Jan 9, 2012 at 5:44 PM, ilya goberman <> wrote:

>  Hello,
> We are a market data API vendor. We have APIs to consume market data in
> Java, .Net, and C++. One of the clients indicated interest in using our API
> from Erlang.
> So I would like to find out if it is possible to interface with any of
> these language APIs from Erlang.
>
> I looked briefly at jinterface, but it appears to be designed to make
> calls from Java to Erlang, but not in the other directions.
> I have found a post
> http://www.javalimit.com/2010/06/a-java-api-for-erjang.html for the
> Erlang-java calls, but it does not seem to be a finished product.
>
> What is the best bet to accomplish the integration?
> Any advice is appreciated.
> Thanks
>
>
Erlang uses message passing as it's primary communication mechanism. Thus
you could write a small Java program utilising the Jinterface library to
enable it to receive messages from Erlang, marshalling to your API and
sending results back using the messaging again.

This is pretty straightforward.

Untested Java-like "pseudo"-code without any error checking:

public static void main(String[] args)
{
    String node_name = args[0];
    String mbox_name = args[1];
    String cookie = args[2];

    OtpNode node = new OtpNode(node_name, cookie);
    OtpMbox mbox =  = node.createMbox(mbox_name);

    while (true) {
        OtpErlangTuple msg = (OtpErlangTuple) mbox.receive();
        OtpErlangPid from = (OtpErlangPid) msg.elementAt(0);
        OtpErlangAtom request = (OtpErlangAtom) msg.elementAt(1);
        OtpErlangObject args = msg.elementAt(2);
        if (request.atomValue().equals("stop")) {
            break;
        } else {
            handle_request_and_send_result_back_to_requestor(from, request,
args);
        }
    }

    node.closeMbox(mbox);
    node.close();
}

In Erlang, you'd have a gen_server that launches this Java server (for
example, as a port program) and uses the defined node and mbox names to
communicate with it.

Robby
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120110/5aea0fba/attachment.html>


More information about the erlang-questions mailing list