Camel & RabbitMQ: Custom ConnectionFactory

@drse / updated June 26, 2015


as of Camel+RabbitMQ v2.15.1:

By default each new camel rabbitmq route (rabbitmq://localhost/) will open up a new connection to the server.

If you are using the context to send messages or dynamically create routes, this can eat up connections quickly and become a performance drain.

The simplest solution is to provide a custom ConnectionFactory used by camel-rabbitmq when creating a connection via the connectionFactory uri attribute.

First, you'll need to subclass your own bean. Make sure to set the host, port, vhost, username, and password. When using connectionFactory, rabbitmq route does not pick up these values from the query URI. Also, override getHost and getPort as these are always called.

Failure to override cred methods will lead to strange connection errors! (that can often be confusing to troubleshoot in production since locally, the default connection prefs and creds may work, ie. localhost:5672)

Ofcourse, cache the connection as well:

import com.rabbitmq.client.ConnectionFactory
import com.rabbitmq.client.Connection
import com.rabbitmq.client.Address

class CachedConnectionFactory extends ConnectionFactory {
  Connection cachedConnection

  protected Map config = [
    host: 'localhost',
    port: '5672',
    virtualHost: '/',
    username: 'guest',
    password: 'guest'

  CachedConnectionFactory() {

  def getConfig() {
    [host: "localhost", port: "1234",
    virtualHost:'/', username:'guest', password:'guest']

   * See:
  Connection createConnection(Connection newConnection) {
    cachedConnection = newConnection

  Connection newConnection() {
    cachedConnection ?: createConnection(super.newConnection())
  } // newConnection

  Connection newConnection(Address[] addrs) {
    cachedConnection ?: createConnection(super.newConnection(addrs))

  Connection newConnection(java.util.concurrent.ExecutorService executor) {
    cachedConnection ?: createConnection(super.newConnection(executor))

  Connection newConnection(java.util.concurrent.ExecutorService executor, Address[] addrs) {
    cachedConnection ?: createConnection(super.newConnection(executor, addrs))

  boolean isAutomaticRecoveryEnabled() {

  boolean isTopologyRecoveryEnabled() {

  long getNetworkRecoveryInterval() {

  int getConnectionTimeout() {

  Map getClientProperties() {

  // called always
  String getHost() { ?: super.getHost()

  // called always
  int getPort() {
    this.config.port ?: super.getPort()

  String getVirtualHost() {
    this.config.virtualHost ?: super.getVirtualHost()

  String getUsername() {
    this.config.username ?: super.getUsername()

  String getPassword() {
    this.config.password ?: super.getPassword()

  // called always
  boolean isSSL() {
} // CachedConnectionFactory

Now, you'll want to use it in the route. Note: you are passing in a bean name, not an instance. You'll either need to access the camelContext's spring context or provide a registry of some sort:

def connectionFactory = new CachedConnectionFactory()
def camelContext = new DefaultCamelContext()
def registry = new SimpleRegistry()
def beanName = "cachedConnectionFactory"
registry.put(beanName, connectionFactory)
camelContext.addRoutes(new RouteBuilder() {
  def void configure() {

Note: you'll probably want use the CachedConnectionFactory in a static context.