rabbitmq-tutorials/haskell/worker.hs
Erik Stevenson 3b6a5e908c Update haskell examples.
- Add support for running examples easily with `stack`.
- Fix calls made to `threadDelay` that were using milliseconds intead of
  nanoseconds.
- amqp-0.9 added a Bool parameter to the `qos` that was causing worker.hs
  to not compile.
- Removed almost all use of String in favor of Lazy ByteString.
2016-12-16 13:39:18 -05:00

45 lines
1.1 KiB
Haskell

#!/usr/bin/env stack
{- stack --install-ghc
runghc
--package amqp
--package bytestring
-}
{-# LANGUAGE OverloadedStrings #-}
import Network.AMQP
import Control.Concurrent (threadDelay)
import qualified Data.ByteString.Lazy.Char8 as BL
import Data.Monoid ((<>))
main :: IO ()
main = do
conn <- openConnection "127.0.0.1" "/" "guest" "guest"
ch <- openChannel conn
declareQueue ch newQueue {queueName = "task_queue",
queueAutoDelete = False,
queueDurable = True}
qos ch 0 1 False
BL.putStrLn " [*] Waiting for messages. To exit press CTRL+C"
consumeMsgs ch "task_queue" Ack deliveryHandler
-- waits for keypresses
getLine
closeConnection conn
deliveryHandler :: (Message, Envelope) -> IO ()
deliveryHandler (msg, metadata) = do
BL.putStrLn $ " [x] Received " <> body
threadDelay (1000000 * n)
BL.putStrLn " [x] Done"
ackEnv metadata
where
body = msgBody msg
n = countDots body
countDots :: BL.ByteString -> Int
countDots = fromIntegral . BL.count '.'