Skip to content

Instantly share code, notes, and snippets.

@xsc xsc/repl.clj
Last active Dec 16, 2015

Embed
What would you like to do?
Clojure and Thrift using Java Interop (intended for the REPL)
;; Processor
(import '(org.example Person PersonIndex PersonIndex$Processor
PersonIndex$Client PersonIndex$Iface))
(def person-index-processor
(PersonIndex$Processor.
(proxy [PersonIndex$Iface] []
(store [p]
(println "Storing Person:")
(println " First Name:" (.getFirstName p))
(println " Last Name:" (.getLastName p))
(println " Age:" (.getAge p))
true))))
;; Server
(import '(org.apache.thrift.server TServer TServer$Args TSimpleServer))
(import '(org.apache.thrift.transport TSocket TServerSocket))
(defn person-index-server
[port]
(let [transport (TServerSocket. port)
args (TServer$Args. transport)
processor (.processor args person-index-processor)]
(TSimpleServer. processor)))
(def server (person-index-server 7007))
(future (.serve server))
;; Client
(import '(org.apache.thrift.protocol TBinaryProtocol))
(defn person-index-client
[host port]
(let [transport (TSocket. host port)
protocol (TBinaryProtocol. transport)
client (PersonIndex$Client. protocol)]
(.open transport)
[transport client]))
(def client-data (person-index-client "localhost" 7007))
(def client (second client-data))
(defn stop-client [] (.close (first client-data)))
;; Data
(def p (Person. "Scherer" 24))
(.setFirstName p "Yannick")
(println p)
;; Send/Receive
(.store client p)
;; Cleanup
(stop-client)
(.stop server)
namespace java org.example
struct Person {
1: optional string firstName,
2: string lastName,
3: byte age
}
service PersonIndex {
bool store(1:Person p)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.