[erlang-questions] JInterface and multi-threading

Raimo Niskanen <>
Thu Apr 24 14:02:43 CEST 2008

On Wed, Apr 23, 2008 at 06:49:26PM +0200, Alexander Lamb wrote:
> Hello,
> Now I have a better understanding of JInterface (from previous post  
> answer), I looked into the Java code.
> It it is not clear to me if rpc from Jinterface is thread safe. Or, in  
> other words, will this be a bottleneck for an application in an app  
> server with many concurent users.

It seems the code is written to be thread safe. It synchronizes
on the socket writes and reads. The socket to the other (Erlang)
node will always be a bottleneck (absolutely not necessarily the smallest).

> Indeed, having only one OtpConnection (which seems logical now), I  
> will need to do rpc calls from potentially simultanious threads.
> How will this be handled? If one call is taking longer than expected,  
> I will block on connection.receiveRPC().

Ok. I have now started to read the Jinterface documentation for
this. It seems OtpConnection and OtpSelf are conveniance classes
for a node with only one "Erlang" process that is only one OtpMbox.

The more Erlanghish way is to create an OtpNode and in that
create many OtpMbox:es. Each OtpMbox could be used in a dedicated
Java thread and call servers, e.g the rpc server 'rex' or
one of your own.

A few conveniance function in OtpConnection, that is
sendRPC and receiveRPC can be used as a template for
doing RPC calls from an OtpMbox.

sendRPC sends:
{self(), {call, Mod, Fun, ArgList, user}}
to {rex,OtherNode}

receiveRPC receives:
{rex,Result} and returns Result.

This is a sneak way to call the RPC server. If you do it from
Erlang rpc:call uses gen_server calls, but that should also
be possible to do from Jinterface.

> Is there a correct way to handle this problem? Do I need for example  
> to create a mailbox in a thread for each session in my server? This  
> would allow my Java sesions to run regardless of any other session  
> blocking on a call to the erlang server. Now, obviously, my Erlang  
> server might be blocked but I could create an Erlang process on the  
> server for each client process connecting from Java.
> What is the correct way to handle those issues.

Since OtpSelf and OtpConnection only have one OtpMbox (internally),
OtpConnection.sendRPC and OtpConnection.receiveRPC will only
do one RPC call at the time, for that OtpSelf object.

If you create one OtpNode per app server, and one OtpMbox
per user session, the sending of RPC calls from several
OtpMbox:es will compete for the connection between the
Java node (OtpNode) and the Erlang node, but the RPC
server in Erlang will spawn a new process for each
new RPC call, so one RPC call in progress will not
block other RPC calls. That is, while receiving one
RPC reply in one OtpMbox, other OtpMbox:es can
do RPC sends and receives.

> Thanks
> Alex
> --
> Alexander Lamb
> Founding Associate
> 4 ch. de la Tour de Champel
> 1206 Geneva
> Switzerland
> Tel:  022 347 77 37
> Fax: 022 347 77 38
> http://www.rodanotech.ch
> _______________________________________________
> erlang-questions mailing list
> http://www.erlang.org/mailman/listinfo/erlang-questions


/ Raimo Niskanen, Erlang/OTP, Ericsson AB

More information about the erlang-questions mailing list