Skip to content
Tim Harper edited this page Feb 24, 2016 · 6 revisions

Is there something you'd like to do that's not included here? Open an issue.

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.

Connecting without using a config file

RabbitControl can accept a ConnectionParams object, directly: http://spingo-oss.s3.amazonaws.com/docs/op-rabbit/current/index.html#com.spingo.op_rabbit.RabbitControl

import com.rabbitmq.client.{Address, ConnectionFactory}

val connectionParams = ConnectionParams(
  hosts = List(new Address("127.0.0.1", ConnectionFactory.DEFAULT_AMQP_PORT)),
  username = "admin",
  password = "very-secret"
)

actorSystem.actorOf(Props { new RabbitControl(connectionParams) })
Clone this wiki locally