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