Created
January 10, 2009 03:43
-
-
Save wilkes/45368 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns rabbitmq | |
(:import (com.rabbitmq.client ConnectionParameters | |
ConnectionFactory | |
QueueingConsumer))) | |
(defstruct connection-info | |
:username :password :virtual-host :heartbeat :host :port) | |
(defn connect [info] | |
(let [connection (.newConnection (ConnectionFactory. | |
(doto (ConnectionParameters.) | |
(.setUsername (info :username)) | |
(.setPassword (info :password)) | |
(.setVirtualHost (info :virtual-host)) | |
(.setRequestedHeartBeat (info :heartbeat)))) | |
(info :host) | |
(info :port)) | |
channel (.createChannel connection)] | |
[connection channel])) | |
(defn disconnect [connection channel] | |
(.close channel) | |
(.close connection)) | |
(defn deliver [consumer channel] | |
(let [delivery (.nextDelivery consumer)] | |
(.basicAck channel (.. delivery getEnvelope getDeliveryTag) false) | |
delivery)) | |
(def default-connection (struct-map connection-info | |
:username "guest" | |
:password "guest" | |
:virtual-host "/" | |
:heartbeat 0 | |
:host "localhost" | |
:port 5672)) | |
(defn produce [] | |
(let [[conn channel] (connect default-connection)] | |
(.queueDeclare channel "SimpleQueue") | |
(.basicPublish channel "" "SimpleQueue" nil (.getBytes (str "the time is"))) | |
(disconnect conn channel))) | |
(defn consume [] | |
(let [[conn channel] (connect default-connection) | |
consumer (QueueingConsumer. channel)] | |
(.queueDeclare channel "SimpleQueue") | |
(.basicConsume channel "SimpleQueue" consumer) | |
(prn "Message: " (String. (.getBody (deliver consumer channel)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment