inaka

Latest blog entries

/
Function Naming In Swift 3

How to write clear function signatures, yet expressive, while following Swift 3 API design guidelines.

Sep 16 2016 : Pablo Villar

/
Jenkins automated tests for Rails

How to automatically trigger rails tests with a Jenkins job

Sep 14 2016 : Demian Sciessere

/
Erlang REST Server Stack

A description of our usual stack for building REST servers in Erlang

Sep 06 2016 : Brujo Benavides

/
Replacing JSON when talking to Erlang

Using Erlang's External Term Format

Aug 17 2016 : Hernan Rivas Acosta

/
Gadget + Lewis = Android Lint CI

Integrating our Android linter with Github's pull requests

Aug 04 2016 : Fernando Ramirez and Euen Lopez

/
Passwordless login with phoenix

Introducing how to implement passwordless login with phoenix framework

Jul 27 2016 : Thiago Borges

/
Beam Olympics

Our newest game to test your Beam Skills

Jul 14 2016 : Brujo Benavides

/
Otec

Three Open Source Projects, one App

Jun 28 2016 : Andrés Gerace

/
CredoCI

Running credo checks for elixir code on your github pull requests

Jun 16 2016 : Alejandro Mataloni

/
Thoughts on rebar3

Thoughts on rebar3

Jun 08 2016 : Hernán Rivas Acosta

/
7 Heuristics for Development

What we've learned from Hernán Wilkinson at our Tech Day

May 31 2016 : Brujo Benavides

/
Introducing Dayron - The Elixir REST client you've always wanted

Meet our library to interact with RESTful APIs in your Elixir Applications.

May 24 2016 : Flavio Granero

/
Inaka's First Tech Day

Inaka's First Tech Day

May 20 2016 : Brujo Benavides

/
PictureViewMaster

The best image projector ever. Period.

May 18 2016 : The Gera

/
Meet Jayme

Your best friend when it comes to abstract server interconnections in Swift

May 09 2016 : Pablo Villar

/
What's in a Hackathon?

The benefits of Hackathons, and a summary of ours

May 01 2016 : Stephanie Goldner

/
How do we host our internal tools ...

Inaka internal hosting

Apr 14 2016 : Ignacio Mendizabal

/
Last Year Meetups at Inaka

Last Year Meetups at Inaka

Mar 01 2016 : David Cao

/
5 Critical Lessons of App Building

While most may think that the meat of building an app is in the development, we’ve found that a large part of a successful project is the work done up front.

Feb 29 2016 : Stephanie Goldner

/
Presenting Lewis, our own Android Lint extension

Rock your Android

Feb 15 2016 : Fernando Ramirez

/
See all Inaka's blog posts >>

/
From Erlang to Java and Back Again: Part 1

Fernando "Brujo" Benavides wrote this on September 05, 2013 under dev, erlang, java .

Introduction

Not so long ago, I was required to write an Erlang application that uses some Java libraries through JInterface. I finally did it, and I learned a lot of things in the process. This article is the first one in a series describing the things I've learned along the way. As we walk through this series, we'll create an application like the one I wrote. It will be called fetjaba (as in From Erlang To Java And Back Again).

In This Article

One of my first goals was for this new application to be used as any other OTP Application, not requiring any Java knowledge from its users. For that, I needed to be able to:

  • compile its Java pieces along with the Erlang ones using rebar compile and, in general, be able to include the app as a regular rebar dependency
  • start the application with just application:start/1 on an Erlang shell, without any Java or shell commands to start the Java part
  • stop the application with application:stop/1 on an Erlang shell, not leaving any Java process(es) around when the application stops

In this article, I'll show you how I implemented that based on this library written by @fredlund.

Directory Structure

For this project I used the standard OTP directory structure, including the classical ebin, src and include folders, deps for the rebar dependencies (like lager), etc. I put the java code in a new java_src folder (in the root folder of the project) and I put all the external jars I needed in the priv folder. I also created a bin folder to place the compiled java classes.

Compiling Java Code

As I stated before, I wanted to be able to just rebar compile the project and have all the erlang and java code compiled at once. To do this, I added the following lines to my rebar.config:

{pre_hooks, [{compile, "mkdir -p bin"}]}.
{post_hooks, [{clean, "rm -rf bin priv/fejtaba.jar"},
              {compile, "javac -g -verbose -deprecation -sourcepath java_src 
                               -classpath ./bin:/usr/local/lib/erlang/lib/jinterface-1.5.6/priv/OtpErlang.jar:./priv/*
                               -d bin `find java_src -name *.java`"},
              {compile, "jar cf priv/fejtaba.jar -C bin ."}]}.

The pre_hook for compile basically just creates a folder where java will output the .class files. Then, I added 3 post_hooks: one for cleaning up by removing the jar generated when compiling, one for compiling the java sources into the bin folder, and the last one to put them all into a jar file in priv.

Starting the Java Node from Erlang

The next task was to be able to start the java node when the Erlang application starts, as well as detect if it goes down at some point. To do so, I wrote the following code:

Erlang

On the Erlang side, I created a gen_server with the following init function:

init([]) ->
  %% First of all we need to have java installed
  case os:find_executable("java") of
    [] ->
      throw({stop, java_missing});
    Java ->
      %% We want to let the java node know who we are
      ThisNode = atom_to_list(node()), 
      %% We build a fancy name for the java node
      JavaNode =
        case string:tokens(ThisNode, "@") of
          [Name, Server] -> list_to_atom(Name ++ "_java@" ++ Server);
          _Node -> throw({bad_node_name, node()})
        end,
      %% Finding the priv dir is a tricky thing, this function does it for us
      Priv = priv_dir(fetjaba),
      %% In the same way, finding an otp lib dir is also tricky, hence otp_lib
      %% We build the classpath with our priv dir (where we put all the jars we need)
      %% and OtpErlang.jar from jinterface
      Classpath = otp_lib("/OtpErlang.jar") ++ [$: | Priv ++ "/*"],
      Port =
        erlang:open_port({spawn_executable, Java},
                         [{line,1000}, stderr_to_stdout,
                          {args, ["-classpath", Classpath, %% The classpath
                                 %% The class with the main method on our Java code
                                  "net.inaka.fetjaba.Node",
                                  %% The command line arguments
                                  %% including the node names and
                                  %% the cookie (you'll see how I use them on java side)
                                  ThisNode, JavaNode,
                                  erlang:get_cookie()]}]),
      %% Then we wait for a signal from the Java side that everything is ready
      wait_for_ready(#state{java_port = Port, java_node = JavaNode})
  end.

%% @doc Skips every console message until it finds the word "READY"
%%             Then starts monitoring the Java node
wait_for_ready(State = #state{java_port = Port}) ->
  receive
    {Port, {data, {eol, "READY"}}} ->
      true = erlang:monitor_node(State#state.java_node, true),
      {ok, State};
    Info ->
      case handle_info(Info, State) of
        {noreply, NewState} ->
          wait_for_ready(NewState);
        {stop, Reason, _NewState} ->
          {stop, Reason}
      end
  end.

Since we are monitoring the Java node, we can handle 'nodedown' messages, and at least stop this gen_server, so its supervisor can act accordingly:

handle_info({nodedown, JavaNode}, State = #state{java_node = JavaNode}) ->
  lager:error("Java node is down!"),
  {stop, nodedown, State};

Java

On Java side, I created a class called net.inaka.fetjaba.Node that, up to this point, just starts a node:

public class Node {
  /** This Erlang node */
  public static OtpNode   NODE;

  /** Peer node name */
  public static String    PEER;

  public static void main(String[] args) {
    String peerName = args.length >= 1 ? args[0]
        : "fetjaba@nohost";
    String nodeName = args.length >= 2 ? args[1]
        : "fetjaba_java@nohost";
    try {
      // Depending on whether there is a provided cookie or not…
      NODE = args.length >= 3 ? new OtpNode(nodeName, args[2])
          : new OtpNode(nodeName);
      PEER = peerName;

      do_our_stuff(); // Most likely, at least start a thread with an Erlang mbox

      System.out.println("READY");
    } catch (IOException e1) {
      e1.printStackTrace();
      System.exit(1);
    }
  }
}

Stopping the Java Node from Erlang

My last goal was to be able to stop the running Java node when I executed application:stop(fetjaba) on the Erlang side. There are many ways to do it, but since I already had a gen_server on the Erlang side, I decided to put an Erlang Mbox (which is almost equivalent to an Erlang process) on Java side and just link it to my gen_server. This way, when one dies, the other can take action. So, at some point inside what replaced that call to do_our_stuff() on the Java code, I started a Thread with a named Mbox and handled the exit signals like this:


    // We create an mbox to link
    final OtpMbox mbox = NODE.createMbox("fetjaba_server");
    new Thread(mbox.getName()) {
      @Override
      public void run() {
        while(true) { // This thread runs forever
          try {
            OtpErlangObject msg = mbox.receive(); // Gets the message
            processMsg(msg); // … and deals with it, before getting the next one
          } catch (OtpErlangExit oee) { // Until it gets an exit signal
            System.exit(1); // And, this is a little bit violent, but it gets the job done.
          }
        }
      }
    }.start();

Then, on the Erlang side, we change our wait_for_ready/1 function just a little bit…

wait_for_ready(State = #state{java_port = Port}) ->
  receive
    {Port, {data, {eol, "READY"}}} ->
      true = erlang:link(process()),
      true = erlang:monitor_node(State#state.java_node, true),
      {ok, State};
    Info ->
      case handle_info(Info, State) of
        {noreply, NewState} ->
          wait_for_ready(NewState);
        {stop, Reason, _NewState} ->
          {stop, Reason}
      end
  end.

I coded the processMsg(msg) method on my Java class to handle stop and {pid, pid()} properly so that we could see the application in action:

protected static boolean processMsg(OtpErlangObject msg, OtpMbox mbox)
      throws OtpErlangDecodeException {
    if (msg instanceof OtpErlangAtom
        && ((OtpErlangAtom) msg).atomValue().equals("stop")) {
      mbox.close();
      return false;
    } else if (msg instanceof OtpErlangTuple) {
      OtpErlangObject[] elements = ((OtpErlangTuple) msg).elements();
      if (elements.length == 2) {
        if (elements[0] instanceof OtpErlangAtom
            && ((OtpErlangAtom) elements[0]).atomValue().equals(
                "pid") && elements[1] instanceof OtpErlangPid) {
          OtpErlangPid caller = (OtpErlangPid) elements[1];
          mbox.send(caller, new OtpErlangTuple(new OtpErlangObject[] {
              elements[0], mbox.self() }));
          return true;
        }
      }
    }
    throw new OtpErlangDecodeException("Bad message: " + msg.toString());
  }

Trying Things Out For Real

Now let's try all this stuff out, shall we? Since, at this point, it's just a rebarized OTP Application, we can start it inside an Erlang node, like this:

1> application:start(fetjaba).

=INFO REPORT==== 17-Jul-2013::16:57:18 ===
Java node started

ok

=PROGRESS REPORT==== 17-Jul-2013::16:57:18 ===
         application: fetjaba
          started_at: 'fetjaba@inaki.local'
2>

We can then check that the java node is up, and responds to our messages:

2> net_adm:names().
{ok,[{"fetjaba",59454},{"fetjaba_java",59458}]}
3> fetjaba:process().
<10516.1.0>

Now we should verify that both sides of the app die at once. Let's start by tearing down the Java node first:

4> fetjaba:stop().
stop
=ERROR REPORT==== 17-Jul-2013::17:14:06 ===
Java node is down!

=ERROR REPORT==== 17-Jul-2013::17:14:06 ===
** Generic server fetjaba terminating



=INFO REPORT==== 17-Jul-2013::17:14:06 ===
    application: fetjaba
    exited: nodedown
    type: temporary

5> net_adm:names().
{ok,[{"fetjaba",59454}]}
6> erlang:whereis(fetjaba).
undefined

So, when the Java process terminated, its node was torn down. Then, our gen_server noticed it and stopped our application. That's what we wanted. Now, what happens if we stop our application instead?

7> application:start(fetjaba).

=INFO REPORT==== 17-Jul-2013::17:20:27 ===
Java node started

ok

=PROGRESS REPORT==== 17-Jul-2013::17:20:27 ===
         application: fetjaba
          started_at: 'fetjaba@inaki.local'
8> net_adm:names().
{ok,[{"fetjaba",59454},{"fetjaba_java",59527}]}
9> fetjaba:process().
<10516.1.0>
10> application:stop(fetjaba).
ok

=INFO REPORT==== 17-Jul-2013::17:21:42 ===
    application: fetjaba
    exited: stopped
    type: temporary
11> net_adm:names().
{ok,[{"fetjaba",59454}]}
12> fetjaba:process().
** exception throw: timeout

As you can see, when we stop our application, the java process is stopped, too. Exactly what we wanted!

See For Yourself

The code for this application is hosted on Inaka's Github. You can clone that repo and try all this stuff out. We also accept pull requests! :)

What's next?

In the following articles on this series, I'll show you how to create and start some useful processes on the Java node, as well as how to communicate back and forth between them and the Erlang ones.