2

I use the following Clojure code to wrap .ping from the riak-java-client. But I don't like being this blunt with exception catching:

(ns myapp.riak
  (:import [com.basho.riak.client IRiakClient]))

(set! *warn-on-reflection* true)

(defn alive?
  "Is Riak client alive? Returns true or false, unlike `.ping`."
  [^IRiakClient client]
  (try
    (.ping client) true
    (catch Exception e false)))

I tried catching these:

  • com.basho.riak.client.RiakException
  • java.net.ConnectException
  • java.io.IOException

But none of these (alone or in combination) worked.

What am I doing wrong?

3:56 pm EDT update: I'm using RiakFactory/httpClient, in case it matters.

4:13 pm EDT update: One interesting aspect is the debugging process. While debugging, ConnectException showed up in the REPL. But RiakIORuntimeException -- part of a solution below -- did not show up. I wonder why not.

1 Answer 1

2

ping throws RiakException - https://github.com/basho/riak-java-client/blob/master/src/main/java/com/basho/riak/client/DefaultRiakClient.java#L210

Next function works for me. I don't have riak installed and it returns false

(ns riak.core
  (:import [com.basho.riak.client IRiakClient RiakFactory RiakException]
           [com.basho.riak.client.http.response RiakIORuntimeException]))

(set! *warn-on-reflection* true)
;; (def client (RiakFactory/pbcClient))
(def client (RiakFactory/httpClient))

(defn alive?
  "Is Riak client alive? Returns true or false, unlike `.ping`."
  [^IRiakClient client]
  (try
    (.ping client)
    true
    (catch RiakException e false)
    (catch RiakIORuntimeException e false)))

REPL:

riak.core> (alive? client)
false

UPDATE: Added RiakIORuntimeException case for httpClient

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.