Java 互操作——Netty + Clojure

2024-04-15

我正在尝试通过 clojure 使用 netty。我可以启动服务器,但是它无法初始化接受的套接字。下面分别是错误消息和代码。有谁知道什么是/或可能是错误的?我相信问题在于(Channels/pipeline (server-handler)) Thanks.

错误信息

#<NioServerSocketChannel [id: 0x01c888d9, /0.0.0.0:843]>
Jun 6, 2012 12:15:35 PM org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink
WARNING: Failed to initialize an accepted socket.
java.lang.IllegalArgumentException: No matching method found: pipeline

项目.clj

(defproject protocol "1.0.0-SNAPSHOT"
  :description "Upload Protocol Server"
  :dependencies [
    [org.clojure/clojure "1.2.1"]
    [io.netty/netty "3.4.5.Final"]])

core.clj

(ns protocol.core
    (:import (java.net InetSocketAddress)
             (java.util.concurrent Executors)
             (org.jboss.netty.bootstrap ServerBootstrap)
             (org.jboss.netty.channel Channels ChannelPipelineFactory SimpleChannelHandler)
             (org.jboss.netty.channel.socket.nio NioServerSocketChannelFactory)
             (org.jboss.netty.buffer ChannelBuffers)))

(def policy
    "<content>Test</content>")


(defn server-handler
    "Returns netty handler."
    []
    (proxy [SimpleChannelHandler] []
        (messageReceived [ctx e]
            (let [ch (.getChannel e)]
                (.write ch policy)
                (.close ch)))

        (channelConnected [ctx e]
            (let [ch (.getChannel e)]
                (.write ch policy)
                (.close ch)))

        (exceptionCaught [ctx e]
            (let [ex (.getCause e)]
                (println "Exception" ex)
                (-> e .getChannel .close)))))

(defn setup-pipeline
    "Returns channel pipeline."
    []
    (proxy [ChannelPipelineFactory] []
        (getPipeline []
            (Channels/pipeline (server-handler)))))

(defn startup
    "Starts netty server."
    [port]
    (let [channel-factory (NioServerSocketChannelFactory. (Executors/newCachedThreadPool) (Executors/newCachedThreadPool))
          bootstrap (ServerBootstrap. channel-factory)]
        (.setPipelineFactory bootstrap (setup-pipeline))
        (.setOption bootstrap "child.tcpNoDelay" true)
        (.setOption bootstrap "child.keepAlive" true)
        (.bind bootstrap (InetSocketAddress. port))))

您的代码存在三个问题

  1. Java 与 vararg Channels.channel() 方法的互操作。 您可以创建一个通道处理程序向量并将其包装为(into-array ChannelHandler ..)

  2. 您不能将 String 对象直接写入 Netty Channel。 您必须先将字符串写入 ChannelBuffer,然后写入该缓冲区或使用 StringCodecHandler。

  3. 写入 Netty 通道是异步的,因此您无法立即关闭它。 您必须注册未来的侦听器并在完成后关闭通道。

这是工作代码。

  (ns clj-netty.core
   (:import (java.net InetSocketAddress)
            (java.util.concurrent Executors)
            (org.jboss.netty.bootstrap ServerBootstrap)
            (org.jboss.netty.buffer ChannelBuffers)
            (org.jboss.netty.channel Channels ChannelFutureListener ChannelHandler ChannelPipelineFactory SimpleChannelHandler)
           (org.jboss.netty.channel.socket.nio NioServerSocketChannelFactory)
           (org.jboss.netty.buffer ChannelBuffers)))


(def policy
  (ChannelBuffers/copiedBuffer
    (.getBytes "<content>Test</content>")))


(defn server-handler
  "Returns netty handler."
  []
  (proxy [SimpleChannelHandler] []
    (messageReceived [ctx e]
      (let [ch (.getChannel e)]
        (.addListener
          (.write ch policy)
          (ChannelFutureListener/CLOSE))))

    (channelConnected [ctx e]
      (let [ch (.getChannel e)]
        (.addListener
          (.write ch policy)
          (ChannelFutureListener/CLOSE))))

    (exceptionCaught [ctx e]
      (let [ex (.getCause e)]
        (println "Exception" ex)
        (-> e .getChannel .close)))))

(defn setup-pipeline
  "Returns channel pipeline."
  []
  (proxy [ChannelPipelineFactory] []
    (getPipeline []
      (let [handler (server-handler)]
        (Channels/pipeline (into-array ChannelHandler [handler]))))))



 (defn startup
      "Starts netty server."
      [port]
      (let [channel-factory (NioServerSocketChannelFactory. (Executors/newCachedThreadPool) (Executors/newCachedThreadPool))
            bootstrap (ServerBootstrap. channel-factory)]
        (.setPipelineFactory bootstrap (setup-pipeline))
        (.setOption bootstrap "child.tcpNoDelay" true)
        (.setOption bootstrap "child.keepAlive" true)
        (.bind bootstrap (InetSocketAddress. port))))

看一下Aleph https://github.com/ztellman/aleph(也使用 Netty)它可以使用漂亮的 Clojure API 以许多不同的协议构建客户端和服务器。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java 互操作——Netty + Clojure 的相关文章

  • JVM 语言中的嵌套函数和词法作用域是如何编译的?

    作为我的问题的具体示例 这里有一个 Python 代码片段 它应该对最广泛的人来说是可读的 并且无论如何都有 JVM 实现 def memo f cache def g args if args not in cache cache arg
  • 为什么在基于 Lisp 的语言中习惯上将许多右括号放在一行上?

    通常代码如下所示 one thing another thing arg1 f arg5 r another thing arg1 f arg5 r 为什么不喜欢这样 one thing another thing arg1 f arg5
  • Java 互操作——Netty + Clojure

    我正在尝试通过 clojure 使用 netty 我可以启动服务器 但是它无法初始化接受的套接字 下面分别是错误消息和代码 有谁知道什么是 或可能是错误的 我相信问题在于 Channels pipeline server handler T
  • 人们可以放心地忽略宏和内置宏之间的区别吗?

    我从 Clojure 开始 这也是我的第一个 lisp 显然有很多东西需要吸收 为了减轻认知负担 我尝试找到我可以安全地忽略的部分 目前 人们能否安全地以相同的方式处理带有宏的表单和带有内置函数的表单 或者是否会出现以后出现的陷阱 换句话说
  • 为什么我们真的需要多个 Netty boss 线程?

    我真的很困惑老板组的线程数量 我无法弄清楚我们需要多个老板线程的场景 在Boss 组是否需要多个线程 https stackoverflow com questions 22280916 do we need more than a sin
  • seq 和 list 之间的区别

    Clojure 语言中的 seq 和列表有什么区别 list 1 2 3 gt 1 2 3 seq 1 2 3 gt 1 2 3 这两种形式似乎被评估为相同的结果 首先 它们可能看起来相同 但实际上并非如此 class list 1 2 3
  • 从文件读取时在 clojure 中分割行

    我正在学校学习 clojure 马上就要考试了 我只是在做一些事情以确保我掌握了窍门 我试图从文件中逐行读取 正如我所做的那样 只要有 我就想分割该行 到目前为止 这是我的代码 defn readFile map fn line cloju
  • 在 Clojure 中创建映射条目

    是什么built inClojure 方式 如果有 创建单个映射条目 换句话说 我想要类似的东西 map entry key value 换句话说 结果应该或多或少等于 first key value Remarks 当然 我已经尝试过谷歌
  • pmap 和线程数

    user gt Runtime getRuntime availableProcessors 2 并评估这个例子 http clojuredocs org clojure core clojure core pmap example 684
  • 由于将请求从主线程传递到工作线程,netty 中出现延迟?

    我有一些关于 Netty 服务器端 TCP IP 应用程序的问题 我想知道在将请求从老板线程传递到工作线程时是否会因为 netty 由于缺少配置等 而出现延迟 我在用 new OrderedMemoryAwareThreadPoolExec
  • 如何将 CORS 添加到 compojure-api 应用程序?

    如何将 CORS 添加到此代码片段 def app api swagger ui docs spec swagger json GET route a a GET route b b GET route c c 我想用https githu
  • 如何使用 core.async 在 Clojure 中写入日志文件?

    我想使用 core async 作为写入文件的记录器 因此我创建了一个 test txt 文件 将其粘贴在我的资源文件夹中并编写了以下代码 use clojure java io use clojure core async def pri
  • core.async不是违背Clo​​jure原则吗?

    我看到许多 Clo jure 程序员对新的 core async 库充满热情 尽管它看起来很有趣 但我很难看出它如何符合 Clojure 原则 所以我有以下问题 它在任何地方都使用可变状态 正如函数名称通过感叹号所暗示的那样 例如 alt
  • 为什么 clojure future 会阻塞主线程?

    我有一个简单的 lein 项目 其中 main包含一个未来 def f future 42 defn main args println f 当我跑步时lein run它打印42但不返回 我不明白为什么它不返回 如何得到lein run回来
  • Clojure 宏:从地图创建本地变量 [重复]

    这个问题在这里已经有答案了 我有这个示例代码 我通过迭代映射的键值对来创建变量 defmacro block bindings body let mapcat fn k v if symbol k k symbol name k v bin
  • 将向量作为绑定传递给 for 宏时出现问题

    我有任意数量的列表 我想使用 for 宏来处理它们 我想创建一个传递向量作为绑定的函数 因为列表的数量各不相同 如果我对绑定进行硬编码 它会按我的预期工作 gt def list1 pink green gt def list2 dog c
  • Emacs/Swank/Paredit for Clojure 的温和教程

    我要转向 Emacs 来工作Clojure http en wikipedia org wiki Clojure Lisp 为了能够执行以下操作 我需要在 Emacs 上设置哪些信息 自动匹配 生成相应的右括号 自动缩进 Lisp Cloj
  • Leiningen 在构建可用的 uberjar 时遇到问题

    我们正在尝试与 Leiningen 一起构建我们的 Clojure 项目 我们通过执行以下操作成功创建了 uberjar 前提条件 project clj 文件列出了依赖项 main my project core在项目 clj中 core
  • Clojure 集合与序列的相等性

    我注意到 Clojure 1 4 似乎很乐意考虑向量等于seq相同的向量 但同样不适用于地图 1 2 seq 1 2 gt true 1 2 seq 1 2 gt false 为什么要这样的行为 这样会有所不同吗 Clojure 的 可以认
  • Clojure:只能从尾部位置重复

    我正在尝试递归地反转列表 但是我得到了Can only recur from tail position运行时 这到底意味着什么 如何改进我的代码才能使其正常工作 defn recursive reverse coll loop coll

随机推荐