I'm using akka-camel to subscribe to a rabbitmq exchange. There will be several of these actors created... one per requested routingKey
. The exchange and queue doesn't change. Each time a new routingKey
is requested I create a new actor and, instead of a new channel being created, a brand new connection is being created, which is undesirable. I don't quite understand why a new connection is being created each time the Consumer actor is being created.
Here's the actor code:
class CommandConsumer(routingKey: String)
extends Consumer with ActorLogging {
override def endpointUri = s"rabbitmq://localhost/hub_commands?exchangeType=topic&queue=test&autoDelete=false&routingKey=$routingKey"
override def receive: Receive = {
case msg: CamelMessage => {
log.debug(s"received {}", msg.bodyAs[String])
sender ! msg.bodyAs[String]
}
}
}
I'm creating the actor like this:
context.actorOf(CommandConsumer.props("my.routing.key", sender))
UPDATE
Here's exactly what I need to accomplish:
I'm writing a TCP/IP server that, when a client connection is accepted, needs to receive messages from other components in the back-end architecture. To do this, I'd like to use RabbitMQ. After a successful connection to my server, the client will send an id, which will be used as part of a routing key (e.g. command.<id>
). The RabbitMQ connection and queue is created when the first client connects and the routing key would be something like command.first_id
. When the next client connects I would like to add command.second_d
routing key to the list of routing keys that are already accepted, without creating a new connection to RabbitMQ.