 
 (defn unlisten
   "Pass the result of a call to listen or respond to de-register the handler.
-You only need to do this if you wish to stop the handler's destination before
-your app is undeployed."
+   You only need to do this if you wish to stop the handler's
+   destination before your app is undeployed."
   [^javax.jms.XAConnection listener]
   (.close listener))
 
 (defn stop
   "Destroy a message destination. Typically not necessary since it
-  will be done for you when your app is undeployed. This will fail
-  with a warning if any handlers are listening"
+   will be done for you when your app is undeployed. This will fail
+   with a warning if any handlers are listening"
   [name]
   (if (or (queue-name? name) (topic-name? name))
     (stop-destination name)

        (codecs/decode-if decode? encoded)))))

(defn message-seq
  "A lazy sequence of messages received from a destination. Accepts same options as receive"
  [dest & opts]
  (lazy-seq (cons (apply receive dest opts) (message-seq dest))))

(defn listen
  "The handler function, f, will receive each message sent to dest. dest can
either be the name of the destination or a javax.jms.Destination.

The following options are supported [default]:
  :concurrency  the number of threads handling messages [1]
  :selector     A JMS (SQL 92) expression matching message properties
  :decode?      if true, the decoded message body is passed to f. Otherwise, the
                javax.jms.Message object is passed [true]
  :host         the remote host to connect to (default is to connect in-vm) [nil]
  :port         the remote port to connect to (requires :host to be set) [nil,
                or 5445 if :host is set]
  :username     the username to use to auth the connection (requires :password
                to be set) [nil]
  :password     the password to use to auth the connection (requires :username
                to be set) [nil]"
  [dest f & {:keys [concurrency selector decode?] :or {concurrency 1 decode? true} :as opts}]
  (let [connection (.createXAConnection (connection-factory opts)
                                        (:username opts)


(defn publish
  "Send a message to a destination. dest can either be the name of the
   destination or a javax.jms.Destination. If the message is a
   javax.jms.Message, then the message is sent without modification to
   dest. Returns the JMS message object that was published.

   The following options are supported [default]:
     :encoding        :clojure :json or :text [:clojure]
     :priority        0-9 or :low :normal :high :critical [4]
     :ttl             time to live, in ms [0=forever]
     :persistent      whether undelivered messages survive restarts [true]
     :properties      a hash to which selectors may be applied [nil]
     :correlation-id  used to set the JMSCorrelationID [nil]
                      see http://docs.oracle.com/javaee/6/api/javax/jms/Message.html#setJMSCorrelationID(java.lang.String) 
     :host            the remote host to connect to (default is to connect in-vm)
                      [nil]
     :port            the remote port to connect to (requires :host to be set)
                      [nil, or 5445 if :host is set]
     :username        the username to use to auth the connection (requires :password
                      to be set) [nil]
     :password        the password to use to auth the connection (requires :username
                      to be set) [nil]"
  [dest message & {:as opts}]
  (with-connection opts
    (let [session (session)
