1

I am puzzled by 2 things. One, the error message itself. And two, the fact that this code spins up so many threads. I was thinking it would spin up 2 or 3 threads in response to a single request, but when I run this on localhost and point my browser at it (just once) it spins up 40 threads. Admittedly, I was using Chrome, which sends 3 requests in the background, for various complicated reasons that Google defends as a "feature" (speed).

What does this error mean?

Exception in thread "Thread-39" java.lang.IllegalArgumentException: Key must be integer
at clojure.lang.APersistentVector.invoke(APersistentVector.java:250)
at serve_pages_from_memory.core$listen_and_respond$fn__51.invoke(core.clj:30)
at clojure.lang.AFn.run(AFn.java:24)
at java.lang.Thread.run(Thread.java:680)

I see similar errors have been discussed on StackOverflow before, but I don't see how they tie in to my problem:

clojure filter map by keys

I'm trying to build a simple app that serves a page. I do this mostly to test my own knowledge of Clojure. Here is most of the code:

(defn start-thread [f]
(doto (new Thread f) (.start)))

(defn serve-page [wrt]
(let [content-length (count page-string)] 
(. wrt println "HTTP/1.1 200 OK")
(. wrt println "Content-Type: text/html; charset=UTF-8")
(. wrt println "Connection: Keep-Alive")
(. wrt println (str "Content-Length: " content-length))
(. wrt print "\r\n")
(. wrt print page-string)
(. wrt flush)
(. wrt close)))

(defn listen-and-respond [ss]
(let [client (. ss accept)]
(start-thread #([client]
                  (let [wrt (new PrintWriter (new BufferedWriter (new OutputStreamWriter (. client (getOutputStream)))))]        
                    (serve-page wrt))))))

(defn create-server [port]
(let [ss (new ServerSocket port)]
(start-thread #(when-not (. ss (isClosed))
                 (try (listen-and-respond ss)
                      (catch SocketException e))
                 (recur)))
ss)) 

(defn -main [& args]
(println "Server is starting")
(let [port (Integer/parseInt (first args))]
(create-server port)))

The error seems to be complaining about this line:

                (serve-page wrt))))))

But that is a function name and a PrintWriter object. The error message would suggest this was something about a map with keys. The error makes no sense.

Any suggestions?

Community
  • 1
  • 1
cerhovice
  • 676
  • 1
  • 10
  • 24

1 Answers1

4
#([client]
   (let [wrt (new PrintWriter (new BufferedWriter (new OutputStreamWriter (. client (getOutputStream)))))]        
     (serve-page wrt)))

is nonsense. If you want a lambda with named parameters, use the fn form:

(fn [client]
  (let [wrt (new PrintWriter (new BufferedWriter (new OutputStreamWriter (. client (getOutputStream)))))]        
    (serve-page wrt)))
amalloy
  • 89,153
  • 8
  • 140
  • 205
  • Thanks, but with that change I now get: " Exception in thread "Thread-3" clojure.lang.ArityException: Wrong number of args (0) passed to: core$listen-and-respond$fn", Also, I'm still getting 8 threads after starting up and making 1 request from the browser. Any thoughts about the excess of threads? – cerhovice Sep 02 '12 at 22:28
  • You apparently want a nullary lambda: `(fn [] ...)`. You aren't expecting `client` to be passed to the function, but are simply closing over it. – amalloy Sep 04 '12 at 17:58