0

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.

threejeez
  • 2,314
  • 6
  • 30
  • 51

1 Answers1

1

I believe that is expected. Each Akka Camel actor will have their own Camel Context which will be independent from others. This means that for each new actor you create, you will be creating a new camel context with a new RabbitMQ endpoint that will hold a new RabbitMQ connection and a new Channel.

If in your scenario the queue and exchange does not change but just the routing key, why don't you just have one Akka Camel actor consuming from the queue and another actor managing the bindings. Every time there is a new routing key that needs to be consumed, this actor will create a rabbitmq connection, channel and call queueBind() with the new routing key. Also, you can have the same actor unbinding the undesired routing keys.

hveiga
  • 6,725
  • 7
  • 54
  • 78
  • It sounds like you're suggesting I create a new connection for each new routing key. Is that what you're saying? Each time a new routing key is requested it will need to be added to the list of routing keys that are already bound to rabbitmq connection/exchange/queue. Do I do that by simply creating another binding for the already existing queue? – threejeez May 15 '15 at 18:50
  • I just added to my original question exactly what I'm trying to do. I hope this helps illuminate the problem better. Thanks!! – threejeez May 15 '15 at 19:00