Skip to content

Instantly share code, notes, and snippets.

@mallipeddi
Created November 12, 2008 16:43
Show Gist options
  • Save mallipeddi/24202 to your computer and use it in GitHub Desktop.
Save mallipeddi/24202 to your computer and use it in GitHub Desktop.
Demonstrates the use of JInterface library to communicate between a Erlang node and a Java node.
/*
* EchoNode.java
*
* Demonstrates the use of JInterface library to communicate between a Erlang node and a Java node.
*
* Compile & run:
* javac -cp /opt/local/lib/erlang/lib/jinterface-1.4.1/priv/OtpErlang.jar EchoNode.java
* java -cp ".:/opt/local/lib/erlang/lib/jinterface-1.4.1/priv/OtpErlang.jar" EchoNode
*
* Call echoservice from Erlang:
*
* $erl -sname echoclient@localhost
* (echoclient@localhost)15> {echoservice, 'echonode@localhost'} ! {self(), 999}.
* {<0.35.0>,999}
* (echoclient@localhost)16> receive Any -> Any end.
* 999
*/
import com.ericsson.otp.erlang.*;
public class EchoNode {
public static void main(String[] args) throws Exception {
OtpNode self = new OtpNode("echonode@localhost");
OtpMbox mbox = self.createMbox("echoservice");
OtpErlangObject o;
OtpErlangTuple msg;
OtpErlangPid from;
while (true) {
try {
o = mbox.receive();
System.out.println("Received something.");
if (o instanceof OtpErlangTuple) {
msg = (OtpErlangTuple)o;
from = (OtpErlangPid)(msg.elementAt(0));
System.out.println("Echoing back...");
mbox.send(from,msg.elementAt(1));
}
}
catch (Exception e) {
System.out.println("" + e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment