-
Notifications
You must be signed in to change notification settings - Fork 0
/
messaging.rb
46 lines (35 loc) · 1.1 KB
/
messaging.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require 'torquebox-messaging'
# create a reference to a queue. will create the queue in HornetQ as
# well if it doesn't already exist
queue = TorqueBox::Messaging.queue('some-queue')
# attach a listener to the queue that will be called when a message
# comes in
queue.listen do |m|
puts "Listener received #{m}"
end
# send ten messages to the queue
10.times do |n|
queue.publish({message: n})
end
# default encoding is marshal
puts TorqueBox::Messaging.default_encoding
# show json, edn also supported (if gems are loaded)
# you can override the default
# TorqueBox::Messaging.default_encoding = :json
# request/respond
sync_queue = TorqueBox::Messaging.queue('sync-queue')
# attach a responder - a listener who's return value is sent to the
# requester
sync_queue.respond do |m|
puts "Responder received #{m}"
sleep(2)
m.upcase
end
# send requests - each will block waiting for the response
%w{foo bar ham biscuit}.map do |m|
response = sync_queue.request(m)
puts "Response is #{response}"
response
end
# can connect to a remote HornetQ
# can talk to apps in other languages (clojure, java, etc)