Skip to content

Instantly share code, notes, and snippets.

@dpwiz
Created February 28, 2013 08:32
Show Gist options
  • Save dpwiz/5055202 to your computer and use it in GitHub Desktop.
Save dpwiz/5055202 to your computer and use it in GitHub Desktop.
Auto-recovering AMQP session using Data.Pool from resource-pool package.
{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty
import Network.AMQP
import Control.Monad.Trans (liftIO)
import qualified Control.Exception as E
import qualified Data.Text.Lazy as TL
import qualified Data.ByteString.Lazy.Char8 as LBS
import Data.Pool
main = do
amqp <- openConnection "127.0.0.1" "/" "guest" "guest" `amqpPool` 2
let send msg = withResource amqp $ \(_, chan) -> publishMsg chan "poop" "scotty" $ newMsg { msgBody = LBS.pack msg }
scotty 8000 $ do
get "/" $ do
html "<form method=\"POST\"><input name=\"haha\"/><input type=\"submit\"/></form>"
post "/" $ do
haha <- param "haha"
liftIO $ send haha
text $ TL.pack haha
amqpPool new size = createPool create destroy 1 3600 size
where
create = do
conn <- new
chan <- openChannel conn -- throws up when server's not yet available
return (conn, chan)
destroy (conn, _) = closeConnection conn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment