Elixir from java

Hello everyone, Is it possible and if so can someone explain how could I do something in the lines of having a .beam or .exs file and use functions from that file in a java application?
I’m new to Elixir but I’m curious if there’s a way to achieve this with the Jinterface (or other method but right know I’ve only come up with the Jinterface), I tried to read the erlangs Jinterface documentation but there’s some stuff I don’t understand yet, but I’d like to be able to try this specific thing anyway.

Thanks in advance for the help!

Hi,

With Jinterface your Erlang and Java sub-systems will run as separate nodes in a distributed erlang cluster. A detailed description on how to connect and do an RPC call are included here: http://erlang.org/doc/apps/jinterface/jinterface_users_guide.html#id58082.

To get a feel of how RPC would work in a distributed Erlang, you can try the following:

Terminal #1 (connect only after starting both sessions):

~ ❯❯❯ iex --name alice@127.0.0.1
iex(alice@127.0.01)1> Node.connect :"bob@127.0.0.1"
true
iex(alice@127.0.0.1)2> Node.list [:known]
[:"alice@127.0.0.1", :"bob@127.0.0.1"]
iex(alice@127.0.0.1)3> :rpc.call(:"bob@127.0.0.1", Node, :self, [])
:"bob@127.0.0.1"

Terminal #2:

~ ❯❯❯ iex --name bob@127.0.0.1
iex(bob@127.0.0.1)1> Node.list [:known]
[:"bob@127.0.0.1", :"alice@127.0.0.1"]

The function Node.self/0 is called from node alice and executed in node bob.

When using Jinterface, the Java application will just be one of the nodes in the cluster.

1 Like

I see, what if I wanted to have a java application start a BEAM process and use functions from it? Without having to do things manually from the elixir side, like for example, having a class in java that starts up said process and calls an Elixir “sort” function on a list?

You certainly can start the Elixir/Erlang application from Java using Runtime.exec() (or the more recent ProcessBuilder), and then connect to it via the Distributed Erlang JInterface API.

You should note that this is better suited if you’re working with long-lived processes on both the Erlang and Java sides.

To have this complex setup I assume you need more than just “call an Elixir sort function on a list”. Think of the Elixir/Erlang application as a separate service, where you use JInterface, Json RPC, Thrift or any other RPC mechanism to interop. Wether your start it from the Java app or directly from the OS service manager depends on how manage your deployments.

1 Like

Yes it’s for processing data from/sending concurrent requests for an API in which the GUI and the base of the app is in Java. That seems like a good solution for what I’m trying to do, thanks a lot!

1 Like

I would suggest if you plan to connect to the BEAM via distributed erlang and not via HTTP, websockets, ect. you should think twice as this is a security vulnerability. You won’t want anyone to be able to connect to your backend and run any function since they could use that as an attack vector to try to gain unauthorized access to the machine. Erlang wasn’t meant to be used this way, its intended purpose was for local trusted network connections. The erlang serialization format is open sourced and available to anyone who would want to connect to your system. And the cookie is only there as a small safeguard, not a way to lock down the server. While I’ve heard running SSL is possible, it isn’t easy either.

EDIT: Using Phoenix Channels as a way of running backend code easily is the method I’d recommend you do since you can put authentication around it safely and run it over SSL easily. Its fairly trivial to setup and there are clients available in java that make talking to elixir simple.

3 Likes

One option is SSH: GitHub - jbenden/esshd: Simple SSH server for Elixir and Erlang applications.

It isn’t the same as distributed erlang, but it gives a well secured way to access an application remotely without resorting the to overhead (developer or system) of HTTP.

That said, if the Elixir application is local to the Java application, it probably doesn’t matter.

2 Likes

I haven’t looked into Phoenix yet but I will, thanks!

1 Like

Yes the app is local to java, seems also like a good solution, thanks!

1 Like

If it is local to the Java app on an otherwise trusted system, then using distributed Elixir / Erlang is just fine …

1 Like

It’s pretty easy, actually, but examples are lacking and it can be confusing getting started.

1 Like