2

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.

Thanks in advance!

1

1 Answer 1

8

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)))

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!"

There are many options for interprocess communication on the JVM, but this approach is unique to Clojure.

Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant! Thanks a bunch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.