2012-01-19 14:38:44 +00:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.camel
|
|
|
|
|
|
2012-06-29 13:33:20 +02:00
|
|
|
import akka.camel.internal._
|
|
|
|
|
import akka.util.Timeout
|
2012-07-04 15:25:30 +02:00
|
|
|
import scala.concurrent.Future
|
2012-01-19 14:38:44 +00:00
|
|
|
import java.util.concurrent.TimeoutException
|
|
|
|
|
import akka.actor.{ ActorSystem, Props, ActorRef }
|
|
|
|
|
import akka.pattern._
|
2012-06-29 13:33:20 +02:00
|
|
|
import scala.concurrent.util.Duration
|
2012-01-19 14:38:44 +00:00
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
/**
|
|
|
|
|
* Activation trait that can be used to wait on activation or de-activation of Camel endpoints.
|
|
|
|
|
* The Camel endpoints are activated asynchronously. This trait can signal when an endpoint is activated or de-activated.
|
|
|
|
|
*/
|
2012-01-19 14:38:44 +00:00
|
|
|
trait Activation {
|
2012-06-29 16:06:26 +02:00
|
|
|
import scala.concurrent.Await
|
2012-01-19 14:38:44 +00:00
|
|
|
|
2012-05-23 15:17:49 +02:00
|
|
|
def system: ActorSystem //FIXME Why is this here, what's it needed for and who should use it?
|
2012-03-01 17:32:10 +01:00
|
|
|
|
2012-05-23 15:17:49 +02:00
|
|
|
private val activationTracker = system.actorOf(Props[ActivationTracker], "camelActivationTracker") //FIXME Why is this also top level?
|
2012-01-19 14:38:44 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Awaits for endpoint to be activated. It blocks until the endpoint is registered in camel context or timeout expires.
|
2012-03-01 17:32:10 +01:00
|
|
|
* @param endpoint the endpoint to wait for to be activated
|
|
|
|
|
* @param timeout the timeout for the wait
|
2012-01-19 14:38:44 +00:00
|
|
|
* @throws akka.camel.ActivationTimeoutException if endpoint is not activated within timeout.
|
2012-03-18 10:46:08 +01:00
|
|
|
* @return the activated ActorRef
|
2012-01-19 14:38:44 +00:00
|
|
|
*/
|
2012-05-23 15:17:49 +02:00
|
|
|
def awaitActivation(endpoint: ActorRef, timeout: Duration): ActorRef =
|
|
|
|
|
try Await.result(activationFutureFor(endpoint, timeout), timeout) catch {
|
2012-01-19 14:38:44 +00:00
|
|
|
case e: TimeoutException ⇒ throw new ActivationTimeoutException(endpoint, timeout)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Awaits for endpoint to be de-activated. It is blocking until endpoint is unregistered in camel context or timeout expires.
|
2012-03-01 17:32:10 +01:00
|
|
|
* @param endpoint the endpoint to wait for to be de-activated
|
|
|
|
|
* @param timeout the timeout for the wait
|
2012-01-19 14:38:44 +00:00
|
|
|
* @throws akka.camel.DeActivationTimeoutException if endpoint is not de-activated within timeout.
|
|
|
|
|
*/
|
2012-05-23 15:17:49 +02:00
|
|
|
def awaitDeactivation(endpoint: ActorRef, timeout: Duration): Unit =
|
|
|
|
|
try Await.result(deactivationFutureFor(endpoint, timeout), timeout) catch {
|
2012-01-19 14:38:44 +00:00
|
|
|
case e: TimeoutException ⇒ throw new DeActivationTimeoutException(endpoint, timeout)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-03-01 17:32:10 +01:00
|
|
|
* Similar to `awaitActivation` but returns a future instead.
|
|
|
|
|
* @param endpoint the endpoint to be activated
|
|
|
|
|
* @param timeout the timeout for the Future
|
2012-01-19 14:38:44 +00:00
|
|
|
*/
|
2012-05-23 15:17:49 +02:00
|
|
|
def activationFutureFor(endpoint: ActorRef, timeout: Duration): Future[ActorRef] =
|
2012-01-19 14:38:44 +00:00
|
|
|
(activationTracker.ask(AwaitActivation(endpoint))(Timeout(timeout))).map[ActorRef] {
|
|
|
|
|
case EndpointActivated(_) ⇒ endpoint
|
|
|
|
|
case EndpointFailedToActivate(_, cause) ⇒ throw cause
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-03-01 17:32:10 +01:00
|
|
|
* Similar to awaitDeactivation but returns a future instead.
|
|
|
|
|
* @param endpoint the endpoint to be deactivated
|
|
|
|
|
* @param timeout the timeout of the Future
|
2012-01-19 14:38:44 +00:00
|
|
|
*/
|
2012-05-23 15:17:49 +02:00
|
|
|
def deactivationFutureFor(endpoint: ActorRef, timeout: Duration): Future[Unit] =
|
2012-01-19 14:38:44 +00:00
|
|
|
(activationTracker.ask(AwaitDeActivation(endpoint))(Timeout(timeout))).map[Unit] {
|
2012-03-01 17:32:10 +01:00
|
|
|
case EndpointDeActivated(_) ⇒ ()
|
2012-01-19 14:38:44 +00:00
|
|
|
case EndpointFailedToDeActivate(_, cause) ⇒ throw cause
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
/**
|
2012-03-18 10:46:08 +01:00
|
|
|
* An exception for when a timeout has occurred during deactivation of an endpoint.
|
2012-03-01 17:32:10 +01:00
|
|
|
* @param endpoint the endpoint that could not be de-activated in time
|
|
|
|
|
* @param timeout the timeout
|
|
|
|
|
*/
|
2012-01-19 14:38:44 +00:00
|
|
|
class DeActivationTimeoutException(endpoint: ActorRef, timeout: Duration) extends TimeoutException {
|
2012-05-23 15:17:49 +02:00
|
|
|
override def getMessage: String = "Timed out after %s, while waiting for de-activation of %s" format (timeout, endpoint.path)
|
2012-01-19 14:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-01 17:32:10 +01:00
|
|
|
/**
|
2012-03-18 10:46:08 +01:00
|
|
|
* An exception for when a timeout has occurred during the activation of an endpoint.
|
2012-03-01 17:32:10 +01:00
|
|
|
* @param endpoint the endpoint that could not be activated in time
|
|
|
|
|
* @param timeout the timeout
|
|
|
|
|
*/
|
2012-01-19 14:38:44 +00:00
|
|
|
class ActivationTimeoutException(endpoint: ActorRef, timeout: Duration) extends TimeoutException {
|
2012-05-23 15:17:49 +02:00
|
|
|
override def getMessage: String = "Timed out after %s, while waiting for activation of %s" format (timeout, endpoint.path)
|
2012-01-19 14:38:44 +00:00
|
|
|
}
|