从终端向Clojure应用程序发送消息
如何将消息发送到正在运行的Clojure应用程序?例如,如果我有一个特定的变量或标志,我想在uberjar运行时从终端设置-这可能吗?
How does one send a message to a running clojure application? For example, if I have a particular variable or flag I want to set from the terminal when the uberjar is running - is this possible?
一种方法是读取文件在应用程序中可以更改,但是听起来很笨拙。
One way is to read a file in the application that you can change, but that sounds clunky.
预先感谢!
一种方法是让您的应用托管nREPL(网络REPL)。然后,您可以连接到正在运行的应用程序的nREPL并四处乱逛。
One way to do this is by having your application host a nREPL (network REPL). You can then connect to your running app's nREPL and mess around.
例如,您的应用程序可能看起来像这样:
For example, your app may look like this:
(ns sandbox.main
(:require [clojure.tools.nrepl.server :as serv]))
(def value (atom "Hey!"))
(defn -main []
(serv/start-server :port 7888)
(while true
(Thread/sleep 1000)
(prn @value)))
正在运行时,您可以 lein repl:从其他地方连接localhost:7888
并更改 value
原子:
While that's running you can lein repl :connect localhost:7888
from elsewhere and change that value
atom:
user=> (in-ns 'sandbox.main)
#object[clojure.lang.Namespace 0x12b094cf "sandbox.main"]
sandbox.main=> (reset! value "Bye!")
"Bye!"
此时,您应用的控制台输出应如下所示:
By this time the console output of your app should look like this:
$ lein run
"Hey!"
"Hey!"
<...truncated...>
"Bye!"
"Bye!"
在JVM上进行进程间通信的选项很多,但这是Clojure独有的方法。
There are many options for interprocess communication on the JVM, but this approach is unique to Clojure.