Skip to content
Tim Harper edited this page Jan 10, 2016 · 6 revisions

Connecting to multiple VHosts

When instantiating the RabbitControl actor, it is possible to provide a specific connection com.typesafe.config.Config block (thus customizing the location from which the actor pulls its configuration).

If you needed to connect to multiple vhosts in an application, you could achieve it via the following:

In your application's application.conf:

op-rabbit {
  topic-exchange-name = "op-rabbit-testeroni"
  vhost1 {
    hosts = ["127.0.0.1"]
    username = "guest"
    password = "guest"
    connection-timeout = 1s
    virtual-host = "vhost1"
    port = 5672
  }

  vhost2 {
    hosts = ["127.0.0.1"]
    username = "guest"
    password = "guest"
    connection-timeout = 1s
    virtual-host = "vhost2"
    port = 5672
  }
}

Then, instantiate the RabbitControl actors as follows:

val config = com.typesafe.config.ConfigFactory.load()
val rabbitVHost1 = actorSystem.actorOf(
  new RabbitControl(
    ConnectionParams.fromConfig(
      config.getConfig("op-rabbit.vhost1"))))

val rabbitVHost2 = actorSystem.actorOf(
  new RabbitControl(
    ConnectionParams.fromConfig(
      config.getConfig("op-rabbit.vhost2"))))

When you wish to have a consumer subscribe on vhost1, provide it the rabbitVHost1 control actor; for vhost2 provide rabbitVhost2 similarly.

Clone this wiki locally