With virtual hosts (vhosts), it is possible to have many different, independent virtual brokers within one single RabbitMQ instance. In this way, it is possible to use the same broker on the parts of many different applications without worrying about name clashes. This is the same approach used by web servers with virtual hosts.
You can find the simple Java example in the Chapter03/Recipe01
directory, which is identical to the example in the first recipe of the book, except for the usage of the vhost.
To exercise this recipe, you just need to issue some commands at the Linux command prompt, that is, RabbitMQ Command Prompt (sbindir) in the Windows Start menu.
To create a new vhost, perform the following steps:
rabbitmqctl list_vhosts
book_orders
by issuing the command:rabbitmqctl add_vhost book_orders
rabbitmqctl list_exchanges -p book_orders
rabbitmqctl list_permissions rabbitmqctl list_permissions -p book_orders
guest
, to access the book_orders
vhost:rabbitmqctl set_permissions guest .* .* .* -p book_orders
factory.setVirtualHost("book_orders");
Once installed, RabbitMQ has just the default vhost /
defined, as we can easily check with the command in step 1.
We then create a new vhost by issuing the command rabbitmqctl add_vhost
(step 2). After that, we must issue all the commands related to this new vhost by specifying it with the –p
option. If this is omitted, the commands are applied to the default vhost.
The new vhost you have added cannot be used yet. A quick check via listing permissions (step 4) will show that the new vhost has no authorization to perform any action. Then we give the predefined user, guest
, all the permissions in the context of the book_orders
vhost, as we will see in the next recipe (step 5).
At this point, it is enough to specify the vhost to the connection factory (step 6) in order to let a RabbitMQ client connect to it instead of the default vhost.