I am writing a web application using ring and clojure. I am using the jetty adapter for the development server and emacs/SLIME for IDE. While wrap-reload does help, run-jetty blocks my slime session and I would like to be able to start/stop it at will without having to run it in a separate terminal session. Ideally, I would like to define a server agent and functions start-server and stop-server that would start/stop the server inside the agent. Is this possible?
3 Answers
I usually have a line in my Ring app that looks like the following:
(defonce server (run-jetty #'my-app {:port 8080 :join? false}))
This prevents locking up the REPL. It also allows me to recompile this file without worrying that my server will get redefined. It also lets you interact at the REPL like so:
user=> (.stop server)
and
user=> (.start server)

- 18,496
- 4
- 62
- 71
-
I'm getting this error: `No matching field found: start for class clojure.lang.PersistentList [Thrown class java.lang.IllegalArgumentException]`. Any ideas? – Tyler Jun 25 '10 at 13:59
-
3Never mind, the problem was that the defonce wasn't re-defing - imagine that :) – Tyler Jun 25 '10 at 14:10
-
Is this still current? I have a working `run-jetty` in my repl but when I use this, a `.start` or `.stop` return `nil` but do nothing. – Matt Apr 24 '20 at 00:59
-
2022: this works fine – jgomo3 Mar 22 '22 at 23:22
-
Just to clarify: it is the `:join? false` that prevents locking up the thread until server is stopped (defaults is `true`) – svitanok Jul 28 '23 at 11:27
The Jetty documentation has some information on graceful shutdown of Jetty. That's probably not enough information but it may get you started.
I haven't started playing with compojure yet, but overall I prefer to work with Tomcat. It's more full-featured; among other things, there is a well-documented API for starting it up and shutting it down, it listens for the shutdown command on a dedicated port; there are ant
tasks to do this, and they could of course be called from a Java app as well. I just don't know what kind of magic Compojure does with connecting the REPL to a running instance of the Web container, and if/how automatic class reloading happens... hopefully someone else will be able to provide more information.

- 66,391
- 18
- 125
- 167
-
1Well, in this instance, I am not even using compojure, I am just using ring.jetty.adapter to start Jetty server. The reason to go with Jetty is so I don't have to recompile all the time. – Mad Wombat Apr 24 '10 at 21:39
-
1Wow, Jetty lets you do that? Cool. Will have to learn about that soon-ish. Thanks! – Carl Smotricz Apr 24 '10 at 21:40
-
3
12 years later.
VSCode / Calva:
If you are like me starting jetty from the repl inside VSCode / Calva you have to CTRL-C the server process at the terminal not the REPL.
In fact the server process is bound to the terminal not to the REPL.

- 2,467
- 4
- 30
- 39
-
12 years later. My question specifically mentions emacs, not vscode. Although to be honest, it has been a long time since I touched either emacs or clojure or jetty. – Mad Wombat May 09 '22 at 17:13
-
haha awesome.. I randomly stumbled upon your thread while searching on how to stop the jetty process inside vscode. Though I was already using the join stuff as mentioned in the comments above I wasn't able to ctrl-c myself out of the process. Stupid me hasn't realize that the process can be killed from the vscode's terminal instead. It only appears to be running inside the repl due to all the server start up messages. Not that you would be interested in all that :) – simou May 11 '22 at 09:54