Skip to content

The JInterface Problem

Duncan McGreggor edited this page Mar 5, 2017 · 2 revisions

Prior to the jiface and clojang libraries, Clojure coders who needed to interact with BEAM (Erlang/LFE/Elixir) nodes needed to use the JInterface library. Even though one's applications had a pretty terrible aesthetic appeal after doing so, the code itself was excruciating: a great deal of fussy, extremely explicit code was required (much more so than in native BEAM applications).

There is one Clojure example below that hints and the awkwardness (to realize the true impact, one would have to see a full application developed around JInterface ... and we don't want to do that to you).

The example below should be contrasted with jiface and clojang versions of the same. The latter especially shines in ease of use and concise expression, rivaling the simplicity of LFE itself.

Without further ado, raw JInterface usage in Clojure:

(import '[com.ericsson.otp.erlang
           OtpErlangObject
           OtpErlangAtom
           OtpErlangTuple
           OtpNode
           OtpMbox])

(def gurka (new OtpNode "gurka"))
(def inbox (.createMbox gurka))
(.registerName inbox "echo")

(def msg (into-array
           OtpErlangObject
           [(.self inbox)
            (new OtpErlangAtom "hello, world")]))

(.send inbox "echo" "gurka" (new OtpErlangTuple msg))
(.receive inbox)

#object[com.ericsson.otp.erlang.OtpErlangTuple
        0x4c9e3fa6
        "{#Pid<gurka@mndltl01.1.0>,'hello, world'}"]

From LFE:

(lfe@mndltl01)> (! #(echo gurka@mndltl01) `#(,(self) hej!))
#(<0.35.0> hej!)

Then back in Clojure:

(def data (.receive inbox))
(def lfe-pid (.elementAt data 0))
(def lfe-msg (-> data
                 (.elementAt 1)
                 (.toString)
                 (clojure.string/replace "'" "")
                 (keyword)))
(.send inbox lfe-pid (new OtpErlangTuple msg))

Then, back in LFE:

(lfe@mndltl01)> (c:flush)
Shell got {<5926.1.0>,'hello, world'}

Clone this wiki locally