inaka

Latest blog entries

/
Open-sourcing at Inaka (lasse, egithub, tirerl & more)

Contribute to open-source and learn at the same time.

Aug 25 2015 : Juan Facorro

/
Cowboy Swagger

Swagger integration for Cowboy

Aug 19 2015 : Carlos Andres Bolaños

/
Too big to compile

What's the maximum size of a source file in Erlang?

Aug 05 2015 : Hernán Rivas Acosta

/
Cowboy Trails

Cowboy routes on steroids!

Jul 20 2015 : Carlos Andres Bolaños

/
Erlang Meta Testing

Meta-Testing in Erlang.

Jul 17 2015 : Brujo Benavides

/
Erlang Operational Configuration Simplicity

An Erlang configuration trick.

Jul 14 2015 : Iñaki Garay

/
Get your Swagger on

Say what you mean, mean what you say.

Jun 23 2015 : Iñaki Garay

/
Android M quick review

Perfectionism over evolution

Jun 11 2015 : Emiliano Giaquinta

/
Erlang Dojo at Inaka

We held a coding dojo for Erlang at our offices.

Jun 09 2015 : Iñaki Garay

/
Server Sent Events (SSE): EventSource implementation on Swift.

An extremely simple Swift library that implements EventSource API (Server Sent Events SSE).

May 28 2015 : Andres Canal

/
Gadget

The Ultimate Code-Checking Machine

Apr 20 2015 : Brujo Benavides

/
Beautiful Code

Writing some poetry in Erlang

Apr 20 2015 : Brujo Benavides

/
(Not) failing with Node.js

Optimizing Node.js or why ORMs are not always good

Apr 10 2015 : Roberto Romero

/
Sorting by popularity like Reddit with Ruby, PostgreSQL and Elastic, part 2

Second part of a post showing how to rank items by popularity in a Ruby application following the Reddit style, this time with performance and pagination focus using Elastic.

Apr 01 2015 : Flavio Granero

/
Sorting by popularity like Reddit with Ruby, PostgreSQL and Elastic, part 1

First part of a post showing how we can sort items by popularity in a Ruby application following the Reddit style, using PostgreSQL and Elastic.

Mar 25 2015 : Flavio Granero

/
TDD, Coverage and Why Testing Exceptions

Why should I test the exceptions in my code?

Feb 24 2015 : Brujo Benavides

/
Galgo iOS Library

An iOS library to display logs as on screen overlays.

Feb 10 2015 : Andres Canal

/
Announcing xref_runner

Making full use of the Erlang toolbox

Feb 10 2015 : Iñaki Garay

/
Weird List Comprensions in Erlang

Some strange cases regarding List Comprenhensions

Jan 13 2015 : Brujo Benavides

/
How to automatically generate different production and staging builds

Use gradle to build different flavors of APKs

Dec 22 2014 : Henrique Boregio

/
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.