Erlang/OTP Forums

Author Message

<  Open Telecom Platform (OTP)  ~  Can jinterface talk from Erlang to Java?

jacek99
Posted: Fri Jun 12, 2009 1:08 pm Reply with quote
User Joined: 26 May 2009 Posts: 27 Location: Montreal, Canada
I was looking at all the jinterface docs and they only have examples of Java talking to Erlang.

Is there a way to go the other way: the entry point is Erlang/OTP, but the end recipient of a message may be a running Java app?
View user's profile Send private message
vigyana
Posted: Thu Jun 25, 2009 1:17 am Reply with quote
Joined: 04 May 2009 Posts: 3
see below my log client .....

... 1. create java node & mbox
... (in my experience, one mbox per one node.
... (i'm running Thread.run env.)
...
Code:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ericsson.otp.erlang.OtpNode;
import com.ericsson.otp.erlang.OtpMbox;
import com.ericsson.otp.erlang.OtpErlangAtom;
import com.ericsson.otp.erlang.OtpErlangPid;
import com.ericsson.otp.erlang.OtpErlangObject;
import com.ericsson.otp.erlang.OtpErlangString;
import com.ericsson.otp.erlang.OtpErlangTuple;

import java.io.IOException;


public class MboxWorker {

  // -------------------------------------- Constructor
  public MboxWorker(String nodeName, String mboxName) throws IOException {
    node = new OtpNode(nodeName);
    mbox = node.createMbox(mboxName);
  }

  // -------------------------------------- Constants
  private static final Logger logger  =
    LoggerFactory.getLogger(MboxWorker.class);

  // -------------------------------------- Static Variables

  // -------------------------------------- Variables
  OtpNode node;
  OtpMbox mbox = null;
  OtpErlangAtom _logAtom = new OtpErlangAtom("pn0000");
 
  // -------------------------------------- Protected Methods

  // -------------------------------------- Public Methods
 
  public void doService() throws Exception {
    try {
      OtpErlangObject o;
      OtpErlangTuple msg;
      OtpErlangPid from;
      OtpErlangAtom logAtom;
      OtpErlangString logstr;
     
      logger.debug("{} : waiting", mbox.getName());
      while (true) {
        try {
          o = mbox.receive();
          if (o instanceof OtpErlangTuple) {
            msg = (OtpErlangTuple) o;
            from = (OtpErlangPid) (msg.elementAt(0));
            logAtom = (OtpErlangAtom)(msg.elementAt(1));
            logstr = (OtpErlangString)(msg.elementAt(2));
           
            if (logAtom.equals(this._logAtom)) {
              logger.info(logstr.stringValue());
              logger.debug("{} : write a log", mbox.getName());
              mbox.send(from, msg.elementAt(3));
            }
            else {
              logger.warn("{} : Illegal Message received : From["
                  + from + "] logstr[" + logstr + "]", mbox.getName());
            }         
          }
        }
        catch (Exception e) {
          logger.error(e.getMessage());
          e.printStackTrace();
        }
        finally {
        }
      }
    }
    catch (Exception e) {
      throw e;
    }
    finally {
    }
  }
}


... 2. send message in erlang node
%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% Function:
%% Description:
%%--------------------------------------------------------------------
Code:

write_log(LogLine, Target) ->
    {Target, mboxName@nodeName} ! {self(), pn0000, LogLine, ok},

    receive
        ok ->
            ok;
        Error ->
            Error
    after 1500 ->
            false
    end.
View user's profile Send private message
jacek99
Posted: Mon Jun 29, 2009 5:08 pm Reply with quote
User Joined: 26 May 2009 Posts: 27 Location: Montreal, Canada
Thanks, that's very useful.
View user's profile Send private message
timrila
Posted: Thu Jun 07, 2012 9:59 am Reply with quote
User Joined: 28 Mar 2012 Posts: 32
Excellent post.thanks for the sharing, that greatly helped me.Good articles should share to every person ,hope you can write more and more good articles.
Custom Soccer Jerseys
England Soccer Jersey
France Soccer Jersey
Real Madrid Jersey
View user's profile Send private message
trapples
Posted: Sat Jun 09, 2012 8:15 am Reply with quote
User Joined: 02 Apr 2012 Posts: 45
I'm not clear about java.

_________________
music make our life sounds beautiful

View user's profile Send private message Visit poster's website

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum