Skip to content

Instantly share code, notes, and snippets.

@jramb
Last active December 11, 2015 04:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jramb/4548223 to your computer and use it in GitHub Desktop.
Save jramb/4548223 to your computer and use it in GitHub Desktop.
Web Services in Clojure, simple example. Both server and client.
(ns wsclj.core
(:gen-class)
(:import [javax.jws WebService])
(:import [javax.xml.ws Endpoint]))
;; Based on a blog-comment by Jonathan Seltzer
(defprotocol Calculator
(hello [this yourname])
(add [this a b])
(append [this s1 s2]))
(deftype ^{WebService {:targetNamespace "http://example.com/wsclj-calculator/"}}
CalcWeb []
Calculator
(^{WebMethod []} hello [this yourname] (str "Hello, " yourname))
(^{WebMethod []} add [this a b] (+ a b) #_(let [result (+ a b)]
(println "Got" a (type a) "and" b (type b)
"-> will return" result (type result))
result))
(^{WebMethod []} append [this a b] (let [result (str a b)]
(println "Got" a (type a) "and" b (type b)
"-> will return" result (type result))
result)))
(defn -main [& args]
(let [url "http://localhost:8080/calcWeb"
endpoint (Endpoint/publish url (CalcWeb.))]
(println "Do your thing, I am listening:" url)))
(comment
;; run the above, then in a separate terminal do this to act as a client:
;;wsimport is a JDK tool
$ wsimport -keep -verbose -d target/classes http://localhost:8080/calcWeb?wsdl
$ lein repl
(def my-service (com.example.wsclj_calculator.CalcWebService.))
(def my-proxy (.getCalcWebPort my-service))
(.add my-proxy 4 5)
(.append my-proxy "abc" "cde")
(.hello my-proxy "Jörg")
(time (last (pmap #(.add my-proxy % %) (range 1001))))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment