【问题标题】:Message Trapping handle* callbacks Elixir消息捕获句柄* 回调 Elixir
【发布时间】:2014-09-07 19:30:24
【问题描述】:

我创建了一个引用 Elixir 练习曲的小型 genserver 应用程序 并且工作良好,当我启动服务器并执行 GenServer.call 时......我得到了所需的数据。

同时,我的应用程序也在接收来自其他生产者的消息,这些消息必须被捕获,因为现在只要我的应用程序收到如下任何消息,它就会在 shell 上可见。

iex(6)> subscription 0x7fdc83c0cdf0 ref 0x7fdc8403fc00
encode_message num 2
encode_message address topic://tryme
next/type PN_STRING
encode_message body alloc size 8192
encode_message formatted body "message body..."
write message sz: 130
Msg {message,#Ref<0.0.0.753>,
          #{address => "topic://tryme",
            body => "Enter some text here for the message body..."}}
AMQP Message received {message,#Ref<0.0.0.753>,
                            #{address => "topic://tryme",
                              body => "message body..."}}

如何在我的应用回调函数中获取此消息以进一步发送到数据库。据我所知,我会在我的 handle_info 中收到这条消息,因为消息不是使用 GenServer.call 调用的。但不确定如何。

【问题讨论】:

    标签: elixir gen-server


    【解决方案1】:

    如果您的 GenServer 正在接收来自另一个进程的消息,那么您应该能够简单地在 handle_info/2 中匹配它们。试试这样的:

    def handle_info(msg, state) do
      IO.inspect {:handle_info, msg}
      {:noreply, state}
    end
    

    但是,如果添加此行后没有打印任何内容,则表示您没有收到您期望的消息。

    【讨论】:

    • 添加后在shell中最后打印为,{:handle_info, {:message, %{address: 'queue://test', body: 'new message'}} }
    猜你喜欢
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    相关资源
    最近更新 更多