1 -- generic amqp consumer
2 import Control.Concurrent
3 import qualified Control.Exception as X
5 import qualified Data.Text as T
7 import Data.Version (showVersion)
9 import Network.AMQP.Utils.Connection
10 import Network.AMQP.Utils.Helpers
11 import Network.AMQP.Utils.Options
12 import Paths_amqp_utils (version)
13 import System.Environment
19 args <- getArgs >>= parseargs 'k'
20 let addiArgs = reverse $ additionalArgs args
21 printparam "client version" ["amqp-utils", showVersion version]
22 (conn, chan) <- connect args
23 addChannelExceptionHandler chan (X.throwTo tid)
25 printparam "prefetch" $ preFetch args
26 qos chan 0 (preFetch args) False
27 -- attach to given queue? or build exclusive queue and bind it?
30 (tempQueue chan (tmpQName args) (bindings args) (currentExchange args))
32 (fmap T.pack (qName args))
33 printparam "queue name" queue
34 printparam "shown body chars" $ anRiss args
35 printparam "temp dir" $ tempDir args
36 printparam "callback" $ fileProcess args
37 printparam "callback args" $ addiArgs
38 printparam "cleanup temp file" $
39 maybe Nothing (\_ -> Just (cleanupTmpFile args)) (fileProcess args)
40 -- subscribe to the queue
48 (myCallback args addiArgs tid)
49 printparam "consumer tag" ctag
50 printparam "send acks" $ ack args
51 printparam "requeue if rejected" $
53 then Just (show (requeuenack args))
55 hr "entering main loop"
57 (forever $ threadDelay 5000000)
58 (\e -> printparam "exception" (e :: X.SomeException))
60 hr "connection closed"
62 -- | exclusive temp queue
63 tempQueue :: Channel -> String -> [(String, String)] -> String -> IO T.Text
64 tempQueue chan tmpqname bindlist x = do
68 newQueue {queueExclusive = True, queueName = T.pack tmpqname}
71 bindQueue chan q (T.pack xchange) (T.pack bkey) >>
72 printparam "binding" [xchange, bkey])
78 -- | process received message
79 myCallback :: Args -> [String] -> ThreadId -> (Message, Envelope) -> IO ()
80 myCallback a addi tid m@(_, envi) = do
81 let numstring = show $ envDeliveryTag envi
82 hr $ "BEGIN " ++ numstring
84 (callbackoptions, callbackenv) <-
86 (printmsg Nothing m (anRiss a) now)
87 (\x -> X.throwTo tid (x :: X.SomeException) >> return ([], []))
88 either (\e -> printparam "ERROR" (e :: X.SomeException) >> reje envi a) return =<<
99 hr $ "END " ++ numstring