Skip to content

Commit

Permalink
Merge pull request akka#2014 from trobert/master
Browse files Browse the repository at this point in the history
Add the ability to use an external camel context
  • Loading branch information
patriknw committed Mar 6, 2014
2 parents 05578f5 + c71987f commit dfef14a
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 7 deletions.
5 changes: 5 additions & 0 deletions akka-camel/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

akka {
camel {
# FQCN of the ContextProvider to be used to create or locate a CamelContext
# it must implement akka.camel.ContextProvider and have a no-arg constructor
# the built-in default create a fresh DefaultCamelContext
context-provider = akka.camel.DefaultContextProvider

# Whether JMX should be enabled or disabled for the Camel Context
jmx = off
# enable/disable streaming cache on the Camel Context
Expand Down
15 changes: 12 additions & 3 deletions akka-camel/src/main/scala/akka/camel/Camel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import org.apache.camel.ProducerTemplate
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.model.RouteDefinition
import com.typesafe.config.Config
import scala.concurrent.duration.{ Duration, FiniteDuration }
import java.util.concurrent.TimeUnit._
import scala.concurrent.duration.FiniteDuration
import scala.collection.immutable

/**
* Camel trait encapsulates the underlying camel machinery.
Expand Down Expand Up @@ -105,8 +105,17 @@ class CamelSettings private[camel] (config: Config, dynamicAccess: DynamicAccess

(s: String, r: RouteDefinition) conversions.get(s).fold(r)(r.convertBodyTo)
}

/**
* Configured setting, determine the class used to load/retrive the instance of the Camel Context
*/
final val ContextProvider: ContextProvider = {
val fqcn = config.getString("akka.camel.context-provider")
dynamicAccess.createInstanceFor[ContextProvider](fqcn, immutable.Seq.empty).recover {
case e throw new ConfigurationException("Could not find/load Context Provider class [" + fqcn + "]", e)
}.get
}
}

/**
* This class can be used to get hold of an instance of the Camel class bound to the actor system.
* <p>For example:
Expand Down
27 changes: 27 additions & 0 deletions akka-camel/src/main/scala/akka/camel/ContextProvider.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.camel

import akka.actor.ExtendedActorSystem
import org.apache.camel.impl.DefaultCamelContext

/**
* Implement this interface in order to inject a specific CamelContext in a system
* An instance of this class must be instantiable using a no-arg constructor.
*/
trait ContextProvider {
/**
* Retrieve or create a Camel Context for the given actor system
* Called once per actor system
*/
def getContext(system: ExtendedActorSystem): DefaultCamelContext
}

/**
* Default implementation of [[akka.camel.ContextProvider]]
* Provides a new DefaultCamelContext per actor system
*/
final class DefaultContextProvider extends ContextProvider {
override def getContext(system: ExtendedActorSystem) = new DefaultCamelContext
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import org.apache.camel.ProducerTemplate
import scala.concurrent.{ Future, ExecutionContext }
import akka.util.Timeout
import akka.pattern.ask
import java.io.InputStream
import org.apache.camel.model.RouteDefinition
import akka.actor.{ ExtendedActorSystem, ActorRef, Props, ActorSystem }
import akka.actor.{ ExtendedActorSystem, ActorRef, Props }

/**
* INTERNAL API
Expand All @@ -33,7 +31,7 @@ private[camel] class DefaultCamel(val system: ExtendedActorSystem) extends Camel
private[camel] implicit val log = Logging(system, "Camel")

lazy val context: DefaultCamelContext = {
val ctx = new DefaultCamelContext
val ctx = settings.ContextProvider.getContext(system)
if (!settings.JmxStatistics) ctx.disableJMX()
ctx.setName(system.name)
ctx.setStreamCaching(settings.StreamingCache)
Expand Down
4 changes: 4 additions & 0 deletions akka-camel/src/test/scala/akka/camel/CamelConfigSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class CamelConfigSpec extends WordSpec with Matchers {
conversions.getString("file") should be("java.io.InputStream")
conversions.entrySet.size should be(1)
}

"have correct Context Provider" in {
settings.ContextProvider.isInstanceOf[DefaultContextProvider] should be(true)
}
}
}

6 changes: 6 additions & 0 deletions akka-docs/rst/java/camel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ Below you can see how you can get access to these Apache Camel objects.

One ``CamelExtension`` is only loaded once for every one ``ActorSystem``, which makes it safe to call the ``CamelExtension`` at any point in your code to get to the
Apache Camel objects associated with it. There is one `CamelContext`_ and one `ProducerTemplate`_ for every one ``ActorSystem`` that uses a ``CamelExtension``.
By Default, a new `CamelContext`_ is created when the ``CamelExtension`` starts. If you want to inject your own context instead,
you can implement the `ContextProvider`_ interface and add the FQCN of your implementation in the config, as the value of the "akka.camel.context-provider".
This interface define a single method ``getContext()`` used to load the `CamelContext`_.

.. _ContextProvider: @github@/akka-camel/src/main/scala/akka/camel/ContextProvider.scala

Below an example on how to add the ActiveMQ component to the `CamelContext`_, which is required when you would like to use the ActiveMQ component.

.. includecode:: code/docs/camel/CamelExtensionTest.java#CamelExtensionAddComponent
Expand Down
6 changes: 6 additions & 0 deletions akka-docs/rst/scala/camel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ Below you can see how you can get access to these Apache Camel objects.

One ``CamelExtension`` is only loaded once for every one ``ActorSystem``, which makes it safe to call the ``CamelExtension`` at any point in your code to get to the
Apache Camel objects associated with it. There is one `CamelContext`_ and one `ProducerTemplate`_ for every one ``ActorSystem`` that uses a ``CamelExtension``.
By Default, a new `CamelContext`_ is created when the ``CamelExtension`` starts. If you want to inject your own context instead,
you can extend the `ContextProvider`_ trait and add the FQCN of your implementation in the config, as the value of the "akka.camel.context-provider".
This interface define a single method ``getContext`` used to load the `CamelContext`_.

.. _ContextProvider: @github@/akka-camel/src/main/scala/akka/camel/ContextProvider.scala

Below an example on how to add the ActiveMQ component to the `CamelContext`_, which is required when you would like to use the ActiveMQ component.

.. includecode:: code/docs/camel/Introduction.scala#CamelExtensionAddComponent
Expand Down

0 comments on commit dfef14a

Please sign in to comment.