我正在 Clojure 中编写一个嵌入式 Jetty 应用程序,并将以下内容作为主要命名空间:
(ns rudkus.core
(:require [clojure.tools.cli :as cli]
[ring.adapter.jetty :as jetty])
(:gen-class))
(defn handler [request]
{:status 200
:headers {"Content-Type" "text/html"}
:body "Hello, World!"})
(def ^{:private true} server (atom nil))
(defn start [port]
(swap! server #(if (not (nil? %))
(throw (IllegalStateException. "Server already started."))
(jetty/run-jetty handler
{:port port
:join? false}))))
(defn stop []
(swap! server #(if (nil? %)
(throw (IllegalStateException. "Server already stopped."))
(do (.stop %)
nil))))
(defn -main [& args]
(let [[options extra-args banner] (cli/cli args
["-p" "--port" "Port" :default 80 :parse-fn #(Integer. %)])]
(if (not-empty extra-args)
(println banner)
(start (:port options)))))
这工作正常 - 我希望能够通过 REPL 启动和停止,以及从运行 JAR 的控制台启动和启动(尽管如果有更好的 Clojure-Ring-Jetty 程序的启动/停止模式,我很想知道)。问题是如果我跑
lein run
或者
lein trampoline run
并且 jetty/run-jetty 抛出了一个异常(它包含了一个 Server.start() 调用)——例如,在我的 MacBook Pro 上,如果我尝试在端口 80 上启动 Jetty 服务器,它会出错——Clojure 程序不会不要退出。但是,异常是在线程“main”中。那么程序为什么不退出呢?我唯一能想到的是 Jetty 有线程浮动。但是 start() 失败了!!!那么这些线程(池)在做什么?
这是 Java 的东西、Clojure 的东西、Leiningen 的东西、Jetty 的东西还是 Ring 的东西?
编辑:
这是堆栈跟踪:
$ java -jar rudkus-0.1.0-SNAPSHOT-standalone.jar
2012-09-09 15:54:59.664:INFO:oejs.Server:jetty-7.x.y-SNAPSHOT
2012-09-09 15:54:59.803:WARN:oejuc.AbstractLifeCycle:FAILED SelectChannelConnector@0.0.0.0:80: java.net.SocketException: Permission denied
java.net.SocketException: Permission denied
at sun.nio.ch.Net.bind(Native Method)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:124)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:59)
at org.eclipse.jetty.server.nio.SelectChannelConnector.open(SelectChannelConnector.java:173)
at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:311)
at org.eclipse.jetty.server.nio.SelectChannelConnector.doStart(SelectChannelConnector.java:251)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at org.eclipse.jetty.server.Server.doStart(Server.java:272)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at ring.adapter.jetty$run_jetty.invoke(jetty.clj:86)
at rudkus.core$start$fn__16.invoke(core.clj:16)
at clojure.lang.Atom.swap(Atom.java:37)
at clojure.core$swap_BANG_.invoke(core.clj:2108)
at rudkus.core$start.invoke(core.clj:14)
at rudkus.core$_main.doInvoke(core.clj:31)
at clojure.lang.RestFn.invoke(RestFn.java:397)
at clojure.lang.AFn.applyToHelper(AFn.java:159)
at clojure.lang.RestFn.applyTo(RestFn.java:132)
at rudkus.core.main(Unknown Source)
2012-09-09 15:54:59.806:WARN:oejuc.AbstractLifeCycle:FAILED org.eclipse.jetty.server.Server@6e5dfaf1: java.net.SocketException: Permission denied
java.net.SocketException: Permission denied
at sun.nio.ch.Net.bind(Native Method)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:124)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:59)
at org.eclipse.jetty.server.nio.SelectChannelConnector.open(SelectChannelConnector.java:173)
at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:311)
at org.eclipse.jetty.server.nio.SelectChannelConnector.doStart(SelectChannelConnector.java:251)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at org.eclipse.jetty.server.Server.doStart(Server.java:272)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at ring.adapter.jetty$run_jetty.invoke(jetty.clj:86)
at rudkus.core$start$fn__16.invoke(core.clj:16)
at clojure.lang.Atom.swap(Atom.java:37)
at clojure.core$swap_BANG_.invoke(core.clj:2108)
at rudkus.core$start.invoke(core.clj:14)
at rudkus.core$_main.doInvoke(core.clj:31)
at clojure.lang.RestFn.invoke(RestFn.java:397)
at clojure.lang.AFn.applyToHelper(AFn.java:159)
at clojure.lang.RestFn.applyTo(RestFn.java:132)
at rudkus.core.main(Unknown Source)
Exception in thread "main" java.net.SocketException: Permission denied
at sun.nio.ch.Net.bind(Native Method)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:124)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:59)
at org.eclipse.jetty.server.nio.SelectChannelConnector.open(SelectChannelConnector.java:173)
at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:311)
at org.eclipse.jetty.server.nio.SelectChannelConnector.doStart(SelectChannelConnector.java:251)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at org.eclipse.jetty.server.Server.doStart(Server.java:272)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at ring.adapter.jetty$run_jetty.invoke(jetty.clj:86)
at rudkus.core$start$fn__16.invoke(core.clj:16)
at clojure.lang.Atom.swap(Atom.java:37)
at clojure.core$swap_BANG_.invoke(core.clj:2108)
at rudkus.core$start.invoke(core.clj:14)
at rudkus.core$_main.doInvoke(core.clj:31)
at clojure.lang.RestFn.invoke(RestFn.java:397)
at clojure.lang.AFn.applyToHelper(AFn.java:159)
at clojure.lang.RestFn.applyTo(RestFn.java:132)
at rudkus.core.main(Unknown Source)