How to create a clojure application with a call server

I got a clojure project with a library of rings. This is project.clj:

(defproject words "1.0.0-SNAPSHOT" :description "Websocket handler for sessions" :dependencies [[org.clojure/clojure "1.4.0"] [org.clojure/clojure-contrib "1.2.0"] [aleph "0.3.0-alpha1"] [org.clojure/data.json "0.1.2"] [clj-redis "0.0.13-SNAPSHOT"] [compojure "0.6.2"] [clj-http "0.1.3"]] :main words.play ;; Lein ring plugin will provide `lein ring server` functionality ;; (and some other relative to ring actions) :plugins [[lein-ring "0.6.6"]] :ring {:handler words.api/engine}) 

In the development environment, I run it with two commands: lane server lane server and it works.

In a production environment, I want to minimize addictions and put them together in a standalone bank using

 lein uberjar 

How can I create it and run both servers from the same jar file?

+7
source share
3 answers

Relatively

 :main words.play 

I advise you to implement the -main function in words.play something like

 (defn -main [& args] (case (first args) "server1" (do (println "Starting server1") (start-server1)) "server2" (do (println "Starting server2") (start-server2)) (println "Enter server name, pls"))) 

Note that :gen-class required in the namespace definition:

 (ns words.play (:gen-class)) 

The implementation for start-server1 and start-server2 should depend on specific frameworks: (run-jetty ...) for ring, (start-http-server ...) for aleph, etc. (you can find more information in the relevant documentation).

Using:

 lein uberjar ## to start first server java -jar my-project-1.0.0-SNAPSHOT-standalone.jar server1 ## to start second one java -jar my-project-1.0.0-SNAPSHOT-standalone.jar server2 
+5
source

The easiest approach is to precompile the class from the clojure source file that launches your application. Your -main function should end up calling something like (run-jetty #'engine {:port 8080}) .

Here is a good tutorial if you are not familiar with clojure compiling time ahead ("aot"): http://kotka.de/blog/2010/02/gen-class_how_it_works_and_how_to_use_it.html

Then it is a matter of creating a shell script that launches your application with something like java -cp you-uber.jar words.Main or somesuch.

Please note that the name of your class “launcher application” and the final ban name are completely arbitrary.

+4
source

You can use lein ring uberjar . This will start the call server. You can start another server in :init hook lein-ring.

0
source

All Articles