Description
This is a simple example to exercise our RabbitMQ setup. Previously we looked at the different types of producers. We also looked at how to install and configure a rabbitmq server. If you missed that, you can find it here.
Prepare our Environment
Install Ruby Version Manager(RVM)
[root@0a11a42bdeb9 /]# yum -y install which curl
...
[root@0a11a42bdeb9 /]# curl -sSL https://get.rvm.io | bash
Downloading https://github.com/rvm/rvm/archive/master.tar.gz
Creating group 'rvm'
Installing RVM to /usr/local/rvm/
cat: /usr/local/rvm/config/alias: No such file or directory
Installation of RVM in /usr/local/rvm/ is almost complete:
* First you need to add all users that will be using rvm to 'rvm' group,
and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.
* To start using RVM you need to run `source /etc/profile.d/rvm.sh`
in all your open shell windows, in rare cases you need to reopen all shell windows.
[root@0a11a42bdeb9 /]# source /etc/profile.d/rvm.sh
[root@0a11a42bdeb9 /]#
Configure RVM and get bunny
rvm build 2.4.0
rvm use 2.4.0
gem install bunny
Honestly, the version here doesnt matter too much. We arent doing anything terribly complicated. But its probably a good idea to just use the same version to avoid problems. This will build and select ruby 2.4.0. We then install the bunny gem, which we use to interact with rabbitmq.
note: If you get an error about rvm not found, revisit the “source” command I did above. We then install a gem called bunny, which is the library we need for interacting with rabbitmq.
Get the code and follow along.
yum -y install git
git clone https://github.com/jayninja/ruby-rabbitmq.git
cd ruby-rabbitmq
Or you can follow along visually by visiting github.
Producer
As we talked about previously, producers create messages and put them in a queue based on various rules(exchange type). So we need to connect to some host, and setup a direct exchange. For our actual message, we will generate some random numbers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
require 'bunny'
connection = Bunny.new
connection.start
channel = connection.create_channel
queue = channel.queue('testqueue')
size = rand(1..100)
while ( size != 0 ) do
channel.default_exchange.publish(rand.to_s, routing_key: queue.name)
size -= 1
puts "message #{size} sent"
end
channel.close
connection.close
If your rabbit instance is running locally, you can use the above. If its running on a different host, change line 3 to connection = Bunny.new(hostname: 'some.host.that.runs.rabbit.com')
.
As you can see, we are using a direct exchange. In other words, we are specifying directly what queue our message goes in. We are generating a random number, and putting it in the queue to be picked up by a consumer. We do this some random number of times under 100.
Consumer
Consumers on the other hand pull messages out of the queue, and do some action based on the message. In this case, we just print it out, because we dont have a real task.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
require 'bunny'
connection = Bunny.new
connection.start
channel = connection.create_channel
queue = channel.queue('testqueue')
begin
puts ' [*] Waiting for messages. To exit press CTRL+C'
queue.subscribe(block: true) do |_delivery_info, _properties, body|
puts " [x] Received #{body}"
end
rescue Interrupt => _
connection.close
exit(0)
end
Notice a lot of the connection data is basically the same. The meat an potatoes is after line 6. This script will retrieve messages from the queue ‘testqueue’, and will tell us what the message said in its body.
Conclusion
I didn’t think going line by line explaining it would help anyone, so I didn’t do it. Instead, you can grab the combined version from github, and run it for fun!