Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Clojure among Java

Clojure among Java

A story of applying Clojure within a pure Java environment (primarily among Tomcat servlets) with practical recommendations and emphasis on ease of introspection and using the app in production.

Volodymyr Kyrylov

May 24, 2013
Tweet

More Decks by Volodymyr Kyrylov

Other Decks in Programming

Transcript

  1. local.MyServlet.service org.apache.catalina.core.ApplicationFilterChain.internalDoFilter org.apache.catalina.core.ApplicationFilterChain.doFilter org.apache.catalina.core.StandardWrapperValve.invoke org.apache.catalina.core.StandardContextValve.invoke org.apache.catalina.authenticator.AuthenticatorBase.invoke org.apache.catalina.core.StandardHostValve.invoke org.apache.catalina.valves.ErrorReportValve.invoke org.apache.catalina.valves.AccessLogValve.invoke org.apache.catalina.core.StandardEngineValve.invoke

    org.apache.catalina.connector.CoyoteAdapter.service org.apache.coyote.http11.AbstractHttp11Processor.process org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run java.util.concurrent.ThreadPoolExecutor.runWorker java.util.concurrent.ThreadPoolExecutor$Worker.run java.lang.Thread.run
  2. % cat project.clj (defproject web "0.1.0-SNAPSHOT" :dependencies [[org.clojure/clojure "1.5.1"] [org.clojure/tools.nrepl

    "0.2.2"] [digest "1.3.0"] [ring "1.2.0-beta2"] [ring/ring-servlet "1.2.0-beta2-SNAPSHOT"] [clj-stacktrace "0.2.5"] [compojure "1.1.5"]] :plugins [[lein-ring "0.8.5-SNAPSHOT"] [lein-checkouts "1.1.0"] [lein-checkout-deps "1.0.0"]] :ring {:handler web.core/handler :web-xml "resources/web.xml"})
  3. DTrace + hotspot provider + jstack() (solaris/osx) % env \

    LD_AUDIT_64=/usr/lib/dtrace/64/libdtrace_forceload.so \ java -XX:+ExtendedDTraceProbes
  4. DTrace + hotspot provider + jstack() ( solaris) % env

    \ LD_AUDIT_64=/usr/lib/dtrace/64/libdtrace_forceload.so \ java -XX:+ExtendedDTraceProbes % vagrant box add omnios \ http://omnios.omniti.com/media/omnios-latest.box
  5. % lein repl nREPL server started on port 63155 REPL-y

    0.1.10 Clojure 1.5.1 Exit: Control+D or (exit) or (quit) Commands: (user/help) Docs: (doc function-name-here) (find-doc "part-of-name-here") Source: (source function-name-here) (user/sourcery function-name-here) Javadoc: (javadoc java-object-or-class-here) Examples from clojuredocs.org: [clojuredocs or cdoc] (user/clojuredocs name-here) (user/clojuredocs "ns-here" "name-here") user=>
  6. % lsof -nPi | grep 63155 java 35453 proger 27u

    IPv6 0x94fc11a576507639 0t0 TCP 127.0.0.1:63155 (LISTEN) java 35453 proger 32u IPv6 0x94fc11a576506e79 0t0 TCP 127.0.0.1:63157->127.0.0.1:63155 (ESTABLISHED) java 35453 proger 33u IPv6 0x94fc11a54fec6259 0t0 TCP 127.0.0.1:63155->127.0.0.1:63157 (ESTABLISHED)
  7. (def nrepl-agent (safe-agent {})) (defroutes api (POST "/nrepl" _request (when

    (nil? (:nrepl @nrepl-agent)) (send-off nrepl-agent (fn [mp] (assoc mp :nrepl (nr/start-server)))) (await nrepl-agent)) (plain-text @nrepl-agent))
  8. % curl -X POST http://tomcat.local/app/nrepl {:nrepl #clojure.tools.nrepl.server.Server{ :server-socket #<ServerSocket ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=63798]>,

    :port 63798, :open-transports #<Atom@ef4100a: #{}>, :transport #<transport$bencode clojure.tools.nrepl.transport$bencode@5148c277>, :greeting nil, :handler #<middleware$wrap_conj_descriptor$fn__304 clojure.tools.nrepl.middleware$wrap_conj_descriptor $fn__304@bb92b69>, :ss #<ServerSocket ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=63798]>}}
  9. % lein repl :connect 63798 REPL-y 0.1.10 Clojure 1.5.1 reply.eval-modes.nrepl=>

    (ns app.core) nil app.core=> (ns-publics *ns*) {handler ...}
  10. % lein repl :connect 63798 ... app.core=> (add-classpath "file:/tank/ proger/.m2/repository/org/clojure/tools.trace/

    0.7.5/tools.trace-0.7.5.jar") nil app.core=> (use 'clojure.tools.trace) nil app.core=> (trace-ns *ns*) nil ; watch STDOUT! app.core=> (untrace-ns *ns*) nil https://github.com/cemerick/pomegranate http://clojure.github.io/tools.trace/
  11. Legal http://www.camunda.com/wp-content/uploads/2012/08/java.gif http://www.youtube.com/watch? feature=player_detailpage&v=gZy2I8csVQU#t=4s http://slant.investorplace.com/files/2013/03/Enterprise_5_hr.jpg http://d310a9hpolx59w.cloudfront.net/product_photos/1952530/ Mustache_20_2B_20Glasses_original.png (derived) http://joyent.com/blog/friday-funday-the-origins-of-the-dtrace-pony https://si0.twimg.com/profile_images/1246802441/lisp_.png

    http://www.deargrumpycat.com/wp-content/uploads/2013/04/Grumpy- Cat.jpg http://content.comrz.com/AcuCustom/Sitename/DAM/374/ApacheTomcat.png http://blog.catmoji.com/wp-content/uploads/2012/09/lying-tard.jpeg http://2.bp.blogspot.com/-iNP88dCJP3Y/Tbz_heuq9HI/AAAAAAAABRM/ jtyOOhjEyKA/s1600/Portal-2%255B1%255D.jpg