Skip to content

Commit 031e2a4

Browse files
Port 2nd tutorial to Haskell (initial version)
It has a problem with the consumer not printing any output until there are messages in the queue by the time it is started.
1 parent d33ae6d commit 031e2a4

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed

haskell/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,23 @@ Code examples are executed via `runhaskell`:
1818

1919
[Tutorial two: Work Queues](http://www.rabbitmq.com/tutorial-two-python.html):
2020

21-
runhaskell new_task.hs
21+
runhaskell newTask.hs
2222
runhaskell worker.hs
2323

2424
[Tutorial three: Publish/Subscribe](http://www.rabbitmq.com/tutorial-three-python.html)
2525

26-
runhaskell receive_logs.hs
27-
runhaskell emit_log.hs
26+
runhaskell receiveLogs.hs
27+
runhaskell emitLog.hs
2828

2929
[Tutorial four: Routing](http://www.rabbitmq.com/tutorial-four-python.html)
3030

31-
runhaskell receive_logs_direct.hs
32-
runhaskell emit_log_direct.hs
31+
runhaskell receiveLogsDirect.hs
32+
runhaskell emitLogDirect.hs
3333

3434
[Tutorial five: Topics](http://www.rabbitmq.com/tutorial-five-python.html)
3535

36-
runhaskell receive_logs_topic.hs
37-
runhaskell emit_log_topic.hs
36+
runhaskell receiveLogsTopic.hs
37+
runhaskell emitLogTopic.hs
3838

3939
[Tutorial six: RPC](http://www.rabbitmq.com/tutorial-six-python.html)
4040

haskell/newTask.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{-# OPTIONS -XOverloadedStrings #-}
2+
3+
import Network.AMQP
4+
import qualified Data.ByteString.Lazy.Char8 as BL
5+
6+
main :: IO ()
7+
main = do
8+
conn <- openConnection "127.0.0.1" "/" "guest" "guest"
9+
ch <- openChannel conn
10+
11+
declareQueue ch newQueue {queueName = "task_queue",
12+
queueAutoDelete = False,
13+
queueDurable = True}
14+
15+
publishMsg ch "" "task_queue"
16+
(newMsg {msgBody = (BL.pack "a.b.c.d.e"),
17+
msgDeliveryMode = Just Persistent})
18+
19+
putStrLn " [x] Sent 'a.b.c.d.e'"
20+
closeConnection conn

haskell/worker.hs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{-# OPTIONS -XOverloadedStrings #-}
2+
3+
import Network.AMQP
4+
import qualified Data.ByteString.Lazy.Char8 as BL
5+
6+
import Control.Concurrent (threadDelay)
7+
8+
main :: IO ()
9+
main = do
10+
conn <- openConnection "127.0.0.1" "/" "guest" "guest"
11+
ch <- openChannel conn
12+
13+
declareQueue ch newQueue {queueName = "task_queue",
14+
queueAutoDelete = False,
15+
queueDurable = True}
16+
17+
qos ch 0 1
18+
19+
putStrLn " [*] Waiting for messages. to Exit press CTRL+C"
20+
consumeMsgs ch "task_queue" NoAck deliveryHandler
21+
22+
-- waits for keypresses
23+
getLine
24+
closeConnection conn
25+
26+
deliveryHandler :: (Message, Envelope) -> IO ()
27+
deliveryHandler (msg, metadata) = do
28+
putStrLn $ " [x] Received " ++ body
29+
-- threadDelay (1000 * n)
30+
putStrLn $ " [x] Done"
31+
ackEnv metadata
32+
where
33+
body = (BL.unpack $ msgBody msg)
34+
n = countDots body
35+
36+
37+
38+
countDots :: [Char] -> Int
39+
countDots s = length $ filter (\c -> c == '.') s

0 commit comments

Comments
 (0)