Q:

Sending message to Clojure application from terminal

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!

A:

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.

