2012-01-19 14:38:44 +00:00
|
|
|
package akka.camel.internal
|
|
|
|
|
|
|
|
|
|
import akka.actor.ActorSystem
|
|
|
|
|
import component.{ DurationTypeConverter, ActorComponent }
|
|
|
|
|
import org.apache.camel.CamelContext
|
|
|
|
|
import org.apache.camel.impl.DefaultCamelContext
|
|
|
|
|
import scala.Predef._
|
|
|
|
|
import akka.event.Logging
|
|
|
|
|
import akka.camel.Camel
|
2012-03-01 17:32:10 +01:00
|
|
|
import akka.util.{ NonFatal, Duration }
|
2012-01-19 14:38:44 +00:00
|
|
|
|
|
|
|
|
/**
|
2012-03-01 17:32:10 +01:00
|
|
|
* For internal use only.
|
2012-01-19 14:38:44 +00:00
|
|
|
* Creates an instance of the Camel subsystem.
|
|
|
|
|
*
|
|
|
|
|
* @param system is used to create internal actors needed by camel instance.
|
|
|
|
|
* Camel doesn't maintain the lifecycle of this actor system. The actor system has to be shut down by the user.
|
|
|
|
|
* In the typical scenario, when camel is used with akka extension, it is natural that camel reuses the actor system it extends.
|
|
|
|
|
* Also by not creating extra internal actor system we are conserving resources.
|
|
|
|
|
*/
|
|
|
|
|
private[camel] class DefaultCamel(val system: ActorSystem) extends Camel {
|
2012-03-01 17:32:10 +01:00
|
|
|
/**
|
|
|
|
|
* For internal use only.
|
|
|
|
|
*/
|
2012-01-19 14:38:44 +00:00
|
|
|
private[camel] implicit val log = Logging(system, "Camel")
|
|
|
|
|
|
|
|
|
|
lazy val context: CamelContext = {
|
|
|
|
|
val ctx = new DefaultCamelContext
|
|
|
|
|
ctx.setName(system.name);
|
|
|
|
|
ctx.setStreamCaching(true)
|
|
|
|
|
ctx.addComponent("actor", new ActorComponent(this))
|
|
|
|
|
ctx.getTypeConverterRegistry.addTypeConverter(classOf[Duration], classOf[String], DurationTypeConverter)
|
|
|
|
|
ctx
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lazy val template = context.createProducerTemplate()
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Starts camel and underlying camel context and template.
|
|
|
|
|
* Only the creator of Camel should start and stop it.
|
|
|
|
|
* @see akka.camel.DefaultCamel#stop()
|
|
|
|
|
*/
|
|
|
|
|
def start = {
|
|
|
|
|
context.start()
|
2012-03-01 17:32:10 +01:00
|
|
|
try template.start() catch { case NonFatal(e) ⇒ context.stop(); throw e }
|
2012-01-19 14:38:44 +00:00
|
|
|
log.debug("Started CamelContext[{}] for ActorSystem[{}]", context.getName, system.name)
|
|
|
|
|
this
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stops camel and underlying camel context and template.
|
|
|
|
|
* Only the creator of Camel should shut it down.
|
|
|
|
|
* There is no need to stop Camel instance, which you get from the CamelExtension, as its lifecycle is bound to the actor system.
|
|
|
|
|
*
|
|
|
|
|
* @see akka.camel.DefaultCamel#start()
|
|
|
|
|
*/
|
|
|
|
|
def shutdown() {
|
2012-03-01 17:32:10 +01:00
|
|
|
try context.stop() finally {
|
|
|
|
|
try { template.stop() } catch { case NonFatal(e) ⇒ log.debug("Swallowing non-fatal exception [{}] on stopping Camel producer template", e) }
|
|
|
|
|
}
|
2012-01-19 14:38:44 +00:00
|
|
|
log.debug("Stopped CamelContext[{}] for ActorSystem[{}]", context.getName, system.name)
|
|
|
|
|
}
|
|
|
|
|
}
|