forked from yapsterapp/clj-nats-async
/
core.clj
93 lines (81 loc) · 3.1 KB
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
(ns clj-nats-async.core
(:require [clojure.string :as str]
[clojure.tools.logging :as log]
[clojure.edn :as edn]
[manifold.stream :as s])
(:import [io.nats.client ConnectionFactory Connection MessageHandler Message]))
(defn create-nats
"creates a Nats connection, returning a Nats object
- urls : nats server urls, either a seq or comma separated"
[& urls]
(let [servers (flatten (map #(str/split % #",") urls))
j-servers (into-array String servers)
cf (ConnectionFactory. j-servers)]
(.createConnection cf)))
(defprotocol INatsMessage
(msg-body [_]))
(defrecord NatsMessage [nats-message]
INatsMessage
(msg-body [_]
(edn/read-string
(String. (.getData nats-message)
"UTF-8"))))
(defn ^:private create-nats-subscription
[nats subject {:keys [queue] :as opts} stream]
(.subscribeAsync
nats
subject
queue
(reify
MessageHandler
(onMessage [_ m]
(s/put! stream (map->NatsMessage {:nats-message m}))))))
(defn subscribe
"returns a a Manifold source-only stream of INatsMessages from a NATS subject.
close the stream to dispose of the subscription"
([nats subject] (subscribe nats subject {}))
([nats subject opts]
(let [stream (s/stream)
source (s/source-only stream)
nats-subscription (create-nats-subscription nats subject opts stream)]
(s/on-closed stream
(fn []
(log/info "closing NATS subscription: " subject)
(.close nats-subscription)))
source)))
(defn publish
"publish a message
- subject-or-fn : either a string specifying a fixed subject or a
(fn [item] ...) which extracts a subject from an item"
([nats subject-or-fn] (publish nats subject-or-fn "" {}))
([nats subject-or-fn body] (publish nats subject-or-fn body {}))
([nats subject-or-fn body {:keys [reply] :as opts}]
(let [is-subject-fn? (or (var? subject-or-fn) (fn? subject-or-fn))
subject (if is-subject-fn? (subject-or-fn body) subject-or-fn)]
(if subject
(.publish nats subject reply (.getBytes (pr-str body) "UTF-8"))
(log/warn (ex-info
(str "no subject "
(if is-subject-fn? "extracted" "given"))
{:body body}))))))
(defn publisher
"returns a Manifold sink-only stream which publishes items put on the stream
to NATS"
([nats subject-or-fn]
(let [stream (s/stream)]
(s/consume (fn [body]
(publish nats subject-or-fn body))
stream)
(s/sink-only stream))))
(defn pubsub
"returns a Manifold source+sink stream for a single NATS subject.
the source returns INatsMessages, while the sink accepts
strings"
([nats subject] (pubsub nats subject {}))
([nats subject opts]
(let [pub-stream (s/stream)
sub-stream (s/stream)
nats-subscription (create-nats-subscription nats subject opts sub-stream)]
(s/consume (fn [body] (publish nats subject body)) pub-stream)
(s/on-closed sub-stream (fn [] (.close nats-subscription)))
(s/splice pub-stream sub-stream))))