2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
|
2013-02-20 21:26:52 +01:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2012-01-19 14:38:44 +00:00
|
|
|
package akka.camel.internal
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
import akka.camel.internal.component.{ ActorComponent, DurationTypeConverter }
|
2012-01-19 14:38:44 +00:00
|
|
|
import org.apache.camel.impl.DefaultCamelContext
|
|
|
|
|
import scala.Predef._
|
|
|
|
|
import akka.event.Logging
|
2019-03-11 10:38:24 +01:00
|
|
|
import akka.camel.{ Camel, CamelSettings }
|
2012-09-03 12:08:46 +02:00
|
|
|
import akka.camel.internal.ActivationProtocol._
|
2012-07-22 15:33:18 +02:00
|
|
|
import scala.util.control.NonFatal
|
2012-09-21 14:50:06 +02:00
|
|
|
import scala.concurrent.duration._
|
2012-09-03 12:08:46 +02:00
|
|
|
import org.apache.camel.ProducerTemplate
|
2019-03-11 10:38:24 +01:00
|
|
|
import scala.concurrent.{ ExecutionContext, Future }
|
2012-09-03 12:08:46 +02:00
|
|
|
import akka.util.Timeout
|
|
|
|
|
import akka.pattern.ask
|
2019-03-11 10:38:24 +01:00
|
|
|
import akka.actor.{ ActorRef, ExtendedActorSystem, Props }
|
2012-01-19 14:38:44 +00:00
|
|
|
|
|
|
|
|
/**
|
2013-06-27 16:45:47 +02:00
|
|
|
* INTERNAL API
|
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.
|
|
|
|
|
*/
|
2012-09-26 17:12:30 +02:00
|
|
|
private[camel] class DefaultCamel(val system: ExtendedActorSystem) extends Camel {
|
2012-09-03 12:08:46 +02:00
|
|
|
val supervisor = system.actorOf(Props[CamelSupervisor], "camel-supervisor")
|
2014-11-17 17:17:52 -06:00
|
|
|
private[camel] implicit val log = Logging(system, getClass.getName)
|
2012-01-19 14:38:44 +00:00
|
|
|
|
2012-09-03 12:08:46 +02:00
|
|
|
lazy val context: DefaultCamelContext = {
|
2014-02-14 16:14:04 +01:00
|
|
|
val ctx = settings.ContextProvider.getContext(system)
|
2012-09-27 12:11:45 +02:00
|
|
|
if (!settings.JmxStatistics) ctx.disableJMX()
|
2012-06-26 00:47:06 +02:00
|
|
|
ctx.setName(system.name)
|
2012-09-27 12:11:45 +02:00
|
|
|
ctx.setStreamCaching(settings.StreamingCache)
|
2012-07-18 08:06:07 +02:00
|
|
|
ctx.addComponent("akka", new ActorComponent(this, system))
|
2012-09-14 10:08:40 +02:00
|
|
|
ctx.getTypeConverterRegistry.addTypeConverter(classOf[FiniteDuration], classOf[String], DurationTypeConverter)
|
2012-01-19 14:38:44 +00:00
|
|
|
ctx
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-26 17:12:30 +02:00
|
|
|
val settings = new CamelSettings(system.settings.config, system.dynamicAccess)
|
2012-07-22 13:27:24 +02:00
|
|
|
|
2012-05-23 15:17:49 +02:00
|
|
|
lazy val template: ProducerTemplate = context.createProducerTemplate()
|
2012-01-19 14:38:44 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Starts camel and underlying camel context and template.
|
|
|
|
|
* Only the creator of Camel should start and stop it.
|
2015-05-15 16:53:24 +02:00
|
|
|
* @see akka.camel.internal.DefaultCamel#shutdown
|
2012-01-19 14:38:44 +00:00
|
|
|
*/
|
2012-05-23 15:17:49 +02:00
|
|
|
def start(): this.type = {
|
2012-01-19 14:38:44 +00:00
|
|
|
context.start()
|
2019-03-11 10:38:24 +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.
|
|
|
|
|
*
|
2015-05-15 16:53:24 +02:00
|
|
|
* @see akka.camel.internal.DefaultCamel#start
|
2012-01-19 14:38:44 +00:00
|
|
|
*/
|
2012-05-23 15:17:49 +02:00
|
|
|
def shutdown(): Unit = {
|
2019-03-11 10:38:24 +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-03-01 17:32:10 +01:00
|
|
|
}
|
2012-01-19 14:38:44 +00:00
|
|
|
log.debug("Stopped CamelContext[{}] for ActorSystem[{}]", context.getName, system.name)
|
|
|
|
|
}
|
2012-09-03 12:08:46 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Produces a Future with the specified endpoint that will be completed when the endpoint has been activated,
|
|
|
|
|
* or if it times out, which will happen after the specified Timeout.
|
|
|
|
|
*
|
|
|
|
|
* @param endpoint the endpoint to be activated
|
|
|
|
|
* @param timeout the timeout for the Future
|
|
|
|
|
*/
|
|
|
|
|
def activationFutureFor(endpoint: ActorRef)(implicit timeout: Timeout, executor: ExecutionContext): Future[ActorRef] =
|
2019-03-11 10:38:24 +01:00
|
|
|
(supervisor
|
|
|
|
|
.ask(AwaitActivation(endpoint))(timeout))
|
|
|
|
|
.map[ActorRef]({
|
|
|
|
|
case EndpointActivated(`endpoint`) => endpoint
|
|
|
|
|
case EndpointFailedToActivate(`endpoint`, cause) => throw cause
|
|
|
|
|
})
|
2012-09-03 12:08:46 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Produces a Future which will be completed when the given endpoint has been deactivated or
|
|
|
|
|
* or if it times out, which will happen after the specified Timeout.
|
|
|
|
|
*
|
|
|
|
|
* @param endpoint the endpoint to be deactivated
|
|
|
|
|
* @param timeout the timeout of the Future
|
|
|
|
|
*/
|
2019-03-13 10:56:20 +01:00
|
|
|
def deactivationFutureFor(
|
|
|
|
|
endpoint: ActorRef)(implicit timeout: Timeout, executor: ExecutionContext): Future[ActorRef] =
|
2019-03-11 10:38:24 +01:00
|
|
|
(supervisor
|
|
|
|
|
.ask(AwaitDeActivation(endpoint))(timeout))
|
|
|
|
|
.map[ActorRef]({
|
|
|
|
|
case EndpointDeActivated(`endpoint`) => endpoint
|
|
|
|
|
case EndpointFailedToDeActivate(`endpoint`, cause) => throw cause
|
|
|
|
|
})
|
2012-09-21 14:50:06 +02:00
|
|
|
}
|