Guest User

Untitled

a guest
Feb 28th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. (ns rabbitmq
  2. (:import (com.rabbitmq.client ConnectionParameters
  3. ConnectionFactory
  4. QueueingConsumer)))
  5.  
  6. (defstruct connection-info
  7. :username :password :virtual-host :heartbeat :host :port)
  8.  
  9. (defn connect [info]
  10. (let [connection (.newConnection (ConnectionFactory.
  11. (doto (ConnectionParameters.)
  12. (.setUsername (info :username))
  13. (.setPassword (info :password))
  14. (.setVirtualHost (info :virtual-host))
  15. (.setRequestedHeartBeat (info :heartbeat))))
  16. (info :host)
  17. (info :port))
  18. channel (.createChannel connection)]
  19. [connection channel]))
  20.  
  21.  
  22. (defn disconnect [connection channel]
  23. (.close channel)
  24. (.close connection))
  25.  
  26. (defn deliver [consumer channel]
  27. (let [delivery (.nextDelivery consumer)]
  28. (.basicAck channel (.. delivery getEnvelope getDeliveryTag) false)
  29. delivery))
  30.  
  31. (def default-connection (struct-map connection-info
  32. :username "guest"
  33. :password "guest"
  34. :virtual-host "/"
  35. :heartbeat 0
  36. :host "localhost"
  37. :port 5672))
  38.  
  39. (defn produce []
  40. (let [[conn channel] (connect default-connection)]
  41. (.queueDeclare channel "SimpleQueue")
  42. (.basicPublish channel "" "SimpleQueue" nil (.getBytes (str "the time is")))
  43. (disconnect conn channel)))
  44.  
  45. (defn consume []
  46. (let [[conn channel] (connect default-connection)
  47. consumer (QueueingConsumer. channel)]
  48. (.queueDeclare channel "SimpleQueue")
  49. (.basicConsume channel "SimpleQueue" consumer)
  50. (prn "Message: " (String. (.getBody (deliver consumer channel))))))
Add Comment
Please, Sign In to add comment