Skip to content

Instantly share code, notes, and snippets.

@tolitius
Created April 30, 2016 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tolitius/a5fe7dfab29be1402bd2f560a64e6900 to your computer and use it in GitHub Desktop.
Save tolitius/a5fe7dfab29be1402bd2f560a64e6900 to your computer and use it in GitHub Desktop.
mount: accessing states within jars
;; [tolitius:/tmp/ido]$ cat src/ido/core.clj
(ns ido.core
(:require [mount.core :refer [defstate]]))
(defstate kafka :start 42
:stop 0)
;; [tolitius:/tmp/ido]$
;; [tolitius:/tmp/ido]$ lein install
;; Created /private/tmp/ido/target/ido-0.1.0-SNAPSHOT.jar
;; Wrote /private/tmp/ido/pom.xml
;; Installed jar and pom into local repo.
;; ^^^ separate JAR / project
;; [tolitius:/tmp/micro-ido]$ cat project.clj
(defproject micro-ido "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.8.0"]
[mount "0.1.11-SNAPSHOT"]
[ido "0.1.0-SNAPSHOT"]]) ;; << depending on the "ido" jar we just created above
;; [tolitius:/tmp/micro-ido]$ cat src/micro_ido/core.clj
(ns micro-ido.core
(:require [mount.core :refer [defstate]]
[ido.core :refer [kafka]]))
(defn with-kafka [kafka-dep]
(println "the ultimate kafka topic always has" kafka-dep "messages"))
(defstate micro-service :start (with-kafka kafka))
;; [tolitius:/tmp/micro-ido]$ lein repl
;; nREPL server started on port 64400 on host 127.0.0.1 - nrepl://127.0.0.1:64400
user=> (require '[mount.core :as mount])
;; mount hasn't seen any states yet
user=> (mount/start)
{:started []}
;; now let's require the micro service
user=> (require '[micro-ido.core])
"|| mounting... #'ido.core/kafka"
"|| mounting... #'micro-ido.core/micro-service"
nil
;; ^^^ ok, so mound saw both, because micro-service _requires_ kafka, hence compiler saw it
;; let's kick it off
user=> (mount/start)
the ultimate kafka topic always has 42 messages
{:started ["#'ido.core/kafka" "#'micro-ido.core/micro-service"]}
;; great success :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment