从Java发送消息到已注册的Erlang gen_server



如何从Jinterface Java服务器向全局注册的gen_server发送消息?

例如,我的gen_server是这样启动的:
  start_link ({global, myServerName}, Mod, Args, Options).

mbox。发送(myServerName MyMessage)。是行不通的。没有消息到达myServerName:handle_info.

一种方法是将global:send/2作为服务器节点上的RPC调用。Java代码看起来像这样:

import java.io.IOException;
import com.ericsson.otp.erlang.*;
public class JClient {
    static String server = "server";
    static String global = "myServerName";
    public static void main(String[] args) throws Exception {
        OtpSelf client = new OtpSelf("client", "java");
        OtpPeer peer = new OtpPeer(server);
        OtpConnection conn = client.connect(peer);
        conn.sendRPC("global", "send", new OtpErlangObject[] {
                new OtpErlangAtom(global),
                new OtpErlangTuple(new OtpErlangObject[] {
                        new OtpErlangAtom("publish"),
                        new OtpErlangTuple(new OtpErlangObject[] {
                                new OtpErlangAtom("all"),
                                new OtpErlangString("this is a test")})
                    })});
    }
}

OtpSelf构造函数的第二个参数是Erlang cookie,它必须匹配服务器节点的cookie,我们将在下面看到。代码使用OtpConnectionsendRPC方法来调用服务器节点上的global:send/2函数,如果成功,则返回全局注册进程的pid,如果失败则返回错误(但Java代码不检查此)。

服务器可以是一个简单的gen_server,看起来像这样:

-module(svr).
-behaviour(gen_server).
-export([start_link/0, stop/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).
-record(state, {}).
start_link() ->
    gen_server:start_link({global, myServerName}, ?MODULE, [], []).
stop() ->
    gen_server:cast({global, myServerName}, stop).
init([]) ->
    {ok, #state{}}.
handle_call(_Request, _From, State) ->
    {reply, ok, State}.
handle_cast(stop, State) ->
    {stop, normal, State};
handle_cast(_Msg, State) ->
    {noreply, State}.
handle_info({publish, {Topic, Msg}}, State) ->
    io:format("publish for topic ~p: ~p~n", [Topic, Msg]),
    {noreply, State};
handle_info(_Msg, State) ->
    {noreply, State}.
terminate(_Reason, _State) ->
    ok.
code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

请注意,我将服务器注册为全局名称myServerName,以匹配您在问题中使用的名称,但在实践中,我从不使用这样的混合大小写原子名称。

要运行服务器,首先启动Erlang shell:

erl -sname server -setcookie java

请注意,cookie与Java代码中的内容相匹配,并且我使用-sname来命名服务器节点;我根本就没试过用长名字。

然后在Erlang shell中运行svr进程:

Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [smp:8:8] [async-threads:10] [kernel-poll:false] [dtrace]
Eshell V5.10.4  (abort with ^G)
(server@myhost)1> svr:start_link().
{ok,<0.42.0>}

然后,运行Java客户机,服务器应该发出以下消息:

publish for topic all: "this is a test"

这种方法的主要缺点是每条消息都必须经过RPC服务器。另一种替代方法是将RPC发送到global:whereis_name/1以查找已注册进程的pid,然后使用send而不是sendRPC直接向其发送消息,但缺点是,如果全局注册进程死亡并重新启动,Java代码将需要检测pid不再有效并重新查找。

最新更新