Erlang Central

Difference between revisions of "How to communicate java and erlang"

From ErlangCentral Wiki

(9 intermediate revisions by 2 users not shown)
Line 3:Line 3:
  
 
==Overview==
 
==Overview==
The purpose of this tutorial is to illustrate how to use <tt>jinterface</tt> for communicating with an Erlang backend
+
The purpose of this tutorial is to illustrate how to use <tt>jinterface</tt> for communicating with an Erlang backend.Here discussed how '''RPC Calls'''(Remote Procedure Calls) are made from java.
  
 
The reader is also encouraged to read the mailing list  [http://www.trapexit.org/forum/viewtopic.php?p=43715 question].It's about  ''''Strategies to connect from Java'''' Refer Erlang Manual for more details on <tt>jinterface</tt>
 
The reader is also encouraged to read the mailing list  [http://www.trapexit.org/forum/viewtopic.php?p=43715 question].It's about  ''''Strategies to connect from Java'''' Refer Erlang Manual for more details on <tt>jinterface</tt>
Line 9:Line 9:
 
==Introduction==
 
==Introduction==
 
This tutorial demonstrates how to communicate with  an Erlang process (node) using a Java program
 
This tutorial demonstrates how to communicate with  an Erlang process (node) using a Java program
For this example I have used java 1.5.0_07 and Erlang OTP12B distribution.
+
For this example java 1.5.0_07 and Erlang OTP12B distribution is used.
  
  
==PartOneErlang Backend==
+
==PartOne : Erlang Backend==
 
First we need to have a erlang process (node)to communicate with Java program.
 
First we need to have a erlang process (node)to communicate with Java program.
If we are going to  connect to an existing erlang node.So in this case
+
If we are going to  connect to an existing erlang node.  
we have to know the name of the Erlang node  
+
we need to know the name of the Erlang node and the cookie.
First we create a Erlang node. In windows you can create a erlang node by using the command prompt using below command.
+
Otherwise we can create an Erlang node.
 +
In windows we can create a erlang node by using the command prompt
 +
try the command.
 
<pre>
 
<pre>
 
     werl -sname enode -setcookie  erlang
 
     werl -sname enode -setcookie  erlang
Line 26:Line 28:
  
  
 +
==PartTwo : Connect Using Java==
  
 +
Let's connect to Erlang node ''''enode'''' using java program
 +
<pre>
  
 +
import com.ericsson.otp.erlang.OtpConnection;
 +
import com.ericsson.otp.erlang.OtpErlangObject;
 +
import com.ericsson.otp.erlang.OtpPeer;
 +
import com.ericsson.otp.erlang.OtpSelf;
  
  
 +
public class ErlConnection {
  
 +
private static OtpConnection conn;
 +
public OtpErlangObject received;
 +
private final String peer;
 +
private final String cookie;
 +
 +
public static void main(String []args){
 +
new ErlConnection("enode","erlang");
 +
}
  
 +
  public ErlConnection(String _peer, String _cookie) {
 +
  peer = _peer;
 +
  cookie = _cookie;
 +
  connect();
  
 +
          /*Do Calls to Rpc methods and then close the connection*/
 +
  disconnect();
  
 +
  }
 +
 +
  private void connect() {
 +
  System.out.print("Please wait, connecting to "+peer+"....\n");
 +
 +
  String javaClient ="java";
 +
  try {
 +
    OtpSelf self = new OtpSelf(javaClient, cookie.trim());
 +
    OtpPeer other = new OtpPeer(peer.trim());
 +
    conn = self.connect(other);
 +
    System.out.println("Connection Established with "+peer+"\n");
 +
  }
 +
  catch (Exception exp) {
 +
    System.out.println("connection error is :" + exp.toString());
 +
    exp.printStackTrace();
 +
  }
 +
 +
}
 +
 +
public void disconnect() {
 +
  System.out.println("Disconnecting....");
 +
  if(conn != null){
 +
    conn.close();
 +
  }
 +
  System.out.println("Successfuly Disconnected");
 +
}
 +
 +
}
 +
 +
</pre>
 +
 +
You need to have <tt>OtpErlang.jar</tt> in your class path.Which comes with Erlang distribution.
 +
Now Compile and run the class.You will see a output like
 +
 +
<pre>
 +
 +
Please wait, connecting to enode....
 +
Connection Established with enode
 +
 +
Disconnecting....
 +
Successfuly Disconnected
 +
</pre>
  
  

Revision as of 11:50, 22 January 2009

Contents

Author

Thilani Abeysinghe <a.thilani at gmail.com>

Overview

The purpose of this tutorial is to illustrate how to use jinterface for communicating with an Erlang backend.Here discussed how RPC Calls(Remote Procedure Calls) are made from java.

The reader is also encouraged to read the mailing list question.It's about 'Strategies to connect from Java' Refer Erlang Manual for more details on jinterface

Introduction

This tutorial demonstrates how to communicate with an Erlang process (node) using a Java program For this example java 1.5.0_07 and Erlang OTP12B distribution is used.


PartOne : Erlang Backend

First we need to have a erlang process (node)to communicate with Java program. If we are going to connect to an existing erlang node. we need to know the name of the Erlang node and the cookie. Otherwise we can create an Erlang node. In windows we can create a erlang node by using the command prompt try the command.

    werl -sname enode -setcookie  erlang

For Linux

    erl -sname enode -setcookie  erlang


PartTwo : Connect Using Java

Let's connect to Erlang node 'enode' using java program


import com.ericsson.otp.erlang.OtpConnection;
import com.ericsson.otp.erlang.OtpErlangObject;
import com.ericsson.otp.erlang.OtpPeer;
import com.ericsson.otp.erlang.OtpSelf;


public class ErlConnection {

	private static OtpConnection conn;
	 public OtpErlangObject received;
	 private final String peer;
	 private final String cookie;
	 
	 public static void main(String []args){
		 new ErlConnection("enode","erlang");
	 }

	  public ErlConnection(String _peer, String _cookie) {
		  peer = _peer;
		  cookie = _cookie;
		  connect();

           /*Do Calls to Rpc methods and then close the connection*/
		  disconnect();

	  }

	  private void connect() {
	   System.out.print("Please wait, connecting to "+peer+"....\n");

	   String javaClient ="java";
	   try {
	     OtpSelf self = new OtpSelf(javaClient, cookie.trim());
	     OtpPeer other = new OtpPeer(peer.trim());
	     conn = self.connect(other);
	     System.out.println("Connection Established with "+peer+"\n");
	   }
	   catch (Exception exp) {
	     System.out.println("connection error is :" + exp.toString());
	     exp.printStackTrace();
	   }

	 }

	 public void disconnect() {
	   System.out.println("Disconnecting....");
	   if(conn != null){
	     conn.close();
	   }
	   System.out.println("Successfuly Disconnected");
	 }

}

You need to have OtpErlang.jar in your class path.Which comes with Erlang distribution. Now Compile and run the class.You will see a output like


Please wait, connecting to enode....
Connection Established with enode

Disconnecting....
Successfuly Disconnected