Last active
December 11, 2015 04:49
-
-
Save jramb/4548223 to your computer and use it in GitHub Desktop.
Web Services in Clojure, simple example. Both server and client.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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