Rebased and squashed all changes to akka camel so far from Piotr and Ray
This commit is contained in:
parent
43febd0b65
commit
4d6511c5c6
69 changed files with 3099 additions and 2758 deletions
|
|
@ -1 +0,0 @@
|
|||
class=akka.camel.component.ActorComponent
|
||||
73
akka-camel/src/main/scala/akka/camel/Activation.scala
Normal file
73
akka-camel/src/main/scala/akka/camel/Activation.scala
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import internal._
|
||||
import akka.util.{ Timeout, Duration }
|
||||
import akka.dispatch.Future
|
||||
import java.util.concurrent.TimeoutException
|
||||
import akka.actor.{ ActorSystem, Props, ActorRef }
|
||||
import akka.pattern._
|
||||
|
||||
trait Activation {
|
||||
import akka.dispatch.Await
|
||||
|
||||
def system: ActorSystem
|
||||
private[camel] val activationTracker = system.actorOf(Props[ActivationTracker])
|
||||
|
||||
/**
|
||||
* Awaits for endpoint to be activated. It blocks until the endpoint is registered in camel context or timeout expires.
|
||||
*
|
||||
* @throws akka.camel.ActivationTimeoutException if endpoint is not activated within timeout.
|
||||
*/
|
||||
def awaitActivation(endpoint: ActorRef, timeout: Duration): ActorRef = {
|
||||
try {
|
||||
Await.result(activationFutureFor(endpoint, timeout), timeout)
|
||||
} catch {
|
||||
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.
|
||||
*
|
||||
* @throws akka.camel.DeActivationTimeoutException if endpoint is not de-activated within timeout.
|
||||
*/
|
||||
def awaitDeactivation(endpoint: ActorRef, timeout: Duration) {
|
||||
try {
|
||||
Await.result(deactivationFutureFor(endpoint, timeout), timeout)
|
||||
} catch {
|
||||
case e: TimeoutException ⇒ throw new DeActivationTimeoutException(endpoint, timeout)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Similar to `awaitActivation` but returns future instead.
|
||||
*/
|
||||
def activationFutureFor(endpoint: ActorRef, timeout: Duration): Future[ActorRef] = {
|
||||
(activationTracker.ask(AwaitActivation(endpoint))(Timeout(timeout))).map[ActorRef] {
|
||||
case EndpointActivated(_) ⇒ endpoint
|
||||
case EndpointFailedToActivate(_, cause) ⇒ throw cause
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Similar to awaitDeactivation but returns future instead.
|
||||
*/
|
||||
def deactivationFutureFor(endpoint: ActorRef, timeout: Duration): Future[Unit] = {
|
||||
(activationTracker.ask(AwaitDeActivation(endpoint))(Timeout(timeout))).map[Unit] {
|
||||
case EndpointDeActivated(_) ⇒ {}
|
||||
case EndpointFailedToDeActivate(_, cause) ⇒ throw cause
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class DeActivationTimeoutException(endpoint: ActorRef, timeout: Duration) extends TimeoutException {
|
||||
override def getMessage = "Timed out after %s, while waiting for de-activation of %s" format (timeout, endpoint.path)
|
||||
}
|
||||
|
||||
class ActivationTimeoutException(endpoint: ActorRef, timeout: Duration) extends TimeoutException {
|
||||
override def getMessage = "Timed out after %s, while waiting for activation of %s" format (timeout, endpoint.path)
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import internal.component.ActorEndpointPath
|
||||
import akka.actor.ActorRef
|
||||
import org.apache.camel.model.ProcessorDefinition
|
||||
|
||||
class ActorRouteDefinition(definition: ProcessorDefinition[_]) {
|
||||
def to(actorRef: ActorRef) = definition.to(ActorEndpointPath(actorRef).toCamelPath())
|
||||
def to(actorRef: ActorRef, consumerConfig: ConsumerConfig) = definition.to(ActorEndpointPath(actorRef).toCamelPath(consumerConfig))
|
||||
}
|
||||
|
||||
84
akka-camel/src/main/scala/akka/camel/Camel.scala
Normal file
84
akka-camel/src/main/scala/akka/camel/Camel.scala
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import internal._
|
||||
import akka.actor._
|
||||
import org.apache.camel.{ ProducerTemplate, CamelContext }
|
||||
|
||||
//TODO complete this doc
|
||||
/**
|
||||
* Camel trait encapsulates the underlying camel machinery.
|
||||
*
|
||||
*/
|
||||
trait Camel extends ConsumerRegistry with ProducerRegistry with Extension with Activation {
|
||||
/**
|
||||
* Underlying camel context.
|
||||
*
|
||||
* It can be used to configure camel manually, i.e. when the user wants to add new routes or endpoints,
|
||||
* i.e. <pre>camel.context.addRoutes(...)</pre>
|
||||
*
|
||||
* @see [[org.apache.camel.CamelContext]]
|
||||
*/
|
||||
def context: CamelContext
|
||||
|
||||
/**
|
||||
* Producer template.
|
||||
* @see [[org.apache.camel.ProducerTemplate]]
|
||||
*/
|
||||
def template: ProducerTemplate
|
||||
|
||||
/**
|
||||
* Associated `ActorSystem`.
|
||||
*
|
||||
* <p>It can be used to start producers, consumers or any other actors which need to interact with camel,
|
||||
* for example:
|
||||
* {{{
|
||||
* val system = ActorSystem("test")
|
||||
* system.actorOf(Props[SysOutConsumer])
|
||||
*
|
||||
* class SysOutConsumer extends Consumer {
|
||||
* def endpointUri = "file://data/input/CamelConsumer"
|
||||
*
|
||||
* protected def receive = {
|
||||
* case msg: Message ⇒ {
|
||||
* printf("Received '%s'\\n", msg.bodyAs[String])
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* }}}
|
||||
* '''Note:''' This actor system is responsible for stopping the underlying camel instance.
|
||||
*
|
||||
* @see [[akka.camel.CamelExtension]]
|
||||
*/
|
||||
def system: ActorSystem
|
||||
}
|
||||
|
||||
/**
|
||||
* This class can be used to get hold of an instance of the Camel class bound to the actor system.
|
||||
* <p>For example:
|
||||
* {{{
|
||||
* val system = ActorSystem("some system")
|
||||
* val camel = CamelExtension(system)
|
||||
* camel.context.addRoutes(...)
|
||||
* }}}
|
||||
*
|
||||
* @see akka.actor.ExtensionId
|
||||
* @see akka.actor.ExtensionIdProvider
|
||||
*
|
||||
*/
|
||||
object CamelExtension extends ExtensionId[Camel] with ExtensionIdProvider {
|
||||
|
||||
/**
|
||||
* Creates a new instance of Camel and makes sure it gets stopped when the actor system is shutdown.
|
||||
*/
|
||||
def createExtension(system: ExtendedActorSystem) = {
|
||||
val camel = new DefaultCamel(system).start;
|
||||
system.registerOnTermination(camel.shutdown())
|
||||
camel
|
||||
}
|
||||
|
||||
def lookup() = CamelExtension
|
||||
}
|
||||
|
|
@ -1,186 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2010 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import org.apache.camel.{ ProducerTemplate, CamelContext }
|
||||
import org.apache.camel.impl.DefaultCamelContext
|
||||
|
||||
import akka.event.EventHandler
|
||||
import akka.japi.{ Option ⇒ JOption }
|
||||
|
||||
import TypedCamelAccess._
|
||||
|
||||
/**
|
||||
* Manages the lifecycle of a CamelContext. Allowed transitions are
|
||||
* init -> start -> stop -> init -> ... etc.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
trait CamelContextLifecycle {
|
||||
// TODO: enforce correct state transitions
|
||||
// valid: init -> start -> stop -> init ...
|
||||
|
||||
private var _context: Option[CamelContext] = None
|
||||
private var _template: Option[ProducerTemplate] = None
|
||||
|
||||
private var _initialized = false
|
||||
private var _started = false
|
||||
|
||||
/**
|
||||
* Returns <code>Some(CamelContext)</code> (containing the current CamelContext)
|
||||
* if <code>CamelContextLifecycle</code> has been initialized, otherwise <code>None</code>.
|
||||
*/
|
||||
def context: Option[CamelContext] = _context
|
||||
|
||||
/**
|
||||
* Returns <code>Some(ProducerTemplate)</code> (containing the current ProducerTemplate)
|
||||
* if <code>CamelContextLifecycle</code> has been initialized, otherwise <code>None</code>.
|
||||
*/
|
||||
def template: Option[ProducerTemplate] = _template
|
||||
|
||||
/**
|
||||
* Returns <code>Some(CamelContext)</code> (containing the current CamelContext)
|
||||
* if <code>CamelContextLifecycle</code> has been initialized, otherwise <code>None</code>.
|
||||
* <p>
|
||||
* Java API.
|
||||
*/
|
||||
def getContext: JOption[CamelContext] = context
|
||||
|
||||
/**
|
||||
* Returns <code>Some(ProducerTemplate)</code> (containing the current ProducerTemplate)
|
||||
* if <code>CamelContextLifecycle</code> has been initialized, otherwise <code>None</code>.
|
||||
* <p>
|
||||
* Java API.
|
||||
*/
|
||||
def getTemplate: JOption[ProducerTemplate] = template
|
||||
|
||||
/**
|
||||
* Returns the current <code>CamelContext</code> if this <code>CamelContextLifecycle</code>
|
||||
* has been initialized, otherwise throws an <code>IllegalStateException</code>.
|
||||
*/
|
||||
def mandatoryContext =
|
||||
if (context.isDefined) context.get
|
||||
else throw new IllegalStateException("no current CamelContext")
|
||||
|
||||
/**
|
||||
* Returns the current <code>ProducerTemplate</code> if this <code>CamelContextLifecycle</code>
|
||||
* has been initialized, otherwise throws an <code>IllegalStateException</code>.
|
||||
*/
|
||||
def mandatoryTemplate =
|
||||
if (template.isDefined) template.get
|
||||
else throw new IllegalStateException("no current ProducerTemplate")
|
||||
|
||||
/**
|
||||
* Returns the current <code>CamelContext</code> if this <code>CamelContextLifecycle</code>
|
||||
* has been initialized, otherwise throws an <code>IllegalStateException</code>.
|
||||
* <p>
|
||||
* Java API.
|
||||
*/
|
||||
def getMandatoryContext = mandatoryContext
|
||||
|
||||
/**
|
||||
* Returns the current <code>ProducerTemplate</code> if this <code>CamelContextLifecycle</code>
|
||||
* has been initialized, otherwise throws an <code>IllegalStateException</code>.
|
||||
* <p>
|
||||
* Java API.
|
||||
*/
|
||||
def getMandatoryTemplate = mandatoryTemplate
|
||||
|
||||
def initialized = _initialized
|
||||
def started = _started
|
||||
|
||||
/**
|
||||
* Starts the CamelContext and an associated ProducerTemplate.
|
||||
*/
|
||||
def start = {
|
||||
for {
|
||||
c ← context
|
||||
t ← template
|
||||
} {
|
||||
c.start()
|
||||
t.start()
|
||||
_started = true
|
||||
EventHandler.info(this, "Camel context started")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the CamelContext and the associated ProducerTemplate.
|
||||
*/
|
||||
def stop = {
|
||||
for {
|
||||
t ← template
|
||||
c ← context
|
||||
} {
|
||||
t.stop
|
||||
c.stop
|
||||
_started = false
|
||||
_initialized = false
|
||||
EventHandler.info(this, "Camel context stopped")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes this instance a new DefaultCamelContext.
|
||||
*/
|
||||
def init(): Unit = init(new DefaultCamelContext)
|
||||
|
||||
/**
|
||||
* Initializes this instance with the given CamelContext. For the passed <code>context</code>
|
||||
* stream-caching is enabled. If applications want to disable stream-caching they can do so
|
||||
* after this method returned and prior to calling start.
|
||||
*/
|
||||
def init(context: CamelContext) {
|
||||
context.setStreamCaching(true)
|
||||
|
||||
for (tc ← TypedCamelModule.typedCamelObject) tc.onCamelContextInit(context)
|
||||
|
||||
this._context = Some(context)
|
||||
this._template = Some(context.createProducerTemplate)
|
||||
|
||||
_initialized = true
|
||||
EventHandler.info(this, "Camel context initialized")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Manages a global CamelContext and an associated ProducerTemplate.
|
||||
*/
|
||||
object CamelContextManager extends CamelContextLifecycle {
|
||||
|
||||
// -----------------------------------------------------
|
||||
// The inherited getters aren't statically accessible
|
||||
// from Java. Therefore, they are redefined here.
|
||||
// TODO: investigate if this is a Scala bug.
|
||||
// -----------------------------------------------------
|
||||
|
||||
/**
|
||||
* see CamelContextLifecycle.getContext
|
||||
* <p>
|
||||
* Java API.
|
||||
*/
|
||||
override def getContext: JOption[CamelContext] = super.getContext
|
||||
|
||||
/**
|
||||
* see CamelContextLifecycle.getTemplate
|
||||
* <p>
|
||||
* Java API.
|
||||
*/
|
||||
override def getTemplate: JOption[ProducerTemplate] = super.getTemplate
|
||||
|
||||
/**
|
||||
* see CamelContextLifecycle.getMandatoryContext
|
||||
* <p>
|
||||
* Java API.
|
||||
*/
|
||||
override def getMandatoryContext = super.getMandatoryContext
|
||||
|
||||
/**
|
||||
* see CamelContextLifecycle.getMandatoryTemplate
|
||||
* <p>
|
||||
* Java API.
|
||||
*/
|
||||
override def getMandatoryTemplate = super.getMandatoryTemplate
|
||||
}
|
||||
|
|
@ -1,268 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2010 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
package akka.camel
|
||||
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
import org.apache.camel.CamelContext
|
||||
|
||||
import akka.actor.{ newUuid, Props, LocalActorRef, Actor }
|
||||
import akka.config.Config._
|
||||
import akka.japi.{ SideEffect, Option ⇒ JOption }
|
||||
import akka.util.Bootable
|
||||
|
||||
import TypedCamelAccess._
|
||||
import akka.dispatch.Await
|
||||
|
||||
/**
|
||||
* Publishes consumer actors at their Camel endpoints. Consumer actors are published asynchronously when
|
||||
* they are started and un-published asynchronously when they are stopped. The CamelService is notified
|
||||
* about actor life cycle by registering listeners at Actor.registry.
|
||||
* <p>
|
||||
* If the akka-camel-typed jar is on the classpath, it is automatically detected by the CamelService. The
|
||||
* optional akka-camel-typed jar provides support for typed consumer actors.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
trait CamelService extends Bootable {
|
||||
private[camel] val activationTracker = new LocalActorRef(Props[ActivationTracker], Props.randomName, true)
|
||||
private[camel] val consumerPublisher = new LocalActorRef(Props(new ConsumerPublisher(activationTracker)), Props.randomName, true)
|
||||
private[camel] val publishRequestor = new LocalActorRef(Props(new ConsumerPublishRequestor), Props.randomName, true)
|
||||
|
||||
private val serviceEnabled = config.getList("akka.enabled-modules").exists(_ == "camel")
|
||||
|
||||
/**
|
||||
* Starts this CamelService if the <code>akka.enabled-modules</code> list contains <code>"camel"</code>.
|
||||
*/
|
||||
abstract override def onLoad = {
|
||||
super.onLoad
|
||||
if (serviceEnabled) start
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops this CamelService if the <code>akka.enabled-modules</code> list contains <code>"camel"</code>.
|
||||
*/
|
||||
abstract override def onUnload = {
|
||||
if (serviceEnabled) stop
|
||||
super.onUnload
|
||||
}
|
||||
|
||||
@deprecated("use start() instead", "1.1")
|
||||
def load = start
|
||||
|
||||
@deprecated("use stop() instead", "1.1")
|
||||
def unload = stop
|
||||
|
||||
/**
|
||||
* Starts this CamelService.
|
||||
*/
|
||||
def start: CamelService = {
|
||||
// Only init and start if not already done by system
|
||||
if (!CamelContextManager.initialized) CamelContextManager.init
|
||||
if (!CamelContextManager.started) CamelContextManager.start
|
||||
|
||||
registerPublishRequestor
|
||||
|
||||
// send registration events for all (un)typed actors that have been registered in the past.
|
||||
for (event ← PublishRequestor.pastActorRegisteredEvents) publishRequestor ! event
|
||||
|
||||
// init publishRequestor so that buffered and future events are delivered to consumerPublisher
|
||||
publishRequestor ! InitPublishRequestor(consumerPublisher)
|
||||
|
||||
for (tc ← TypedCamelModule.typedCamelObject) tc.onCamelServiceStart(this)
|
||||
|
||||
// Register this instance as current CamelService and return it
|
||||
CamelServiceManager.register(this)
|
||||
CamelServiceManager.mandatoryService
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops this CamelService.
|
||||
*/
|
||||
def stop = {
|
||||
// Unregister this instance as current CamelService
|
||||
CamelServiceManager.unregister(this)
|
||||
|
||||
for (tc ← TypedCamelModule.typedCamelObject) tc.onCamelServiceStop(this)
|
||||
|
||||
// Remove related listeners from registry
|
||||
unregisterPublishRequestor
|
||||
|
||||
// Stop related services
|
||||
consumerPublisher.stop
|
||||
activationTracker.stop
|
||||
CamelContextManager.stop
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for an expected number (<code>count</code>) of consumer actor endpoints to be activated
|
||||
* during execution of <code>f</code>. The wait-timeout is by default 10 seconds. Other timeout
|
||||
* values can be set via the <code>timeout</code> and <code>timeUnit</code> parameters.
|
||||
*/
|
||||
def awaitEndpointActivation(count: Int, timeout: Long = 10, timeUnit: TimeUnit = TimeUnit.SECONDS)(f: ⇒ Unit): Boolean = {
|
||||
val activation = expectEndpointActivationCount(count)
|
||||
f; activation.await(timeout, timeUnit)
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for an expected number (<code>count</code>) of consumer actor endpoints to be de-activated
|
||||
* during execution of <code>f</code>. The wait-timeout is by default 10 seconds. Other timeout
|
||||
* values can be set via the <code>timeout</code> and <code>timeUnit</code>
|
||||
* parameters.
|
||||
*/
|
||||
def awaitEndpointDeactivation(count: Int, timeout: Long = 10, timeUnit: TimeUnit = TimeUnit.SECONDS)(f: ⇒ Unit): Boolean = {
|
||||
val activation = expectEndpointDeactivationCount(count)
|
||||
f; activation.await(timeout, timeUnit)
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for an expected number (<code>count</code>) of consumer actor endpoints to be activated
|
||||
* during execution of <code>p</code>. The wait timeout is 10 seconds.
|
||||
* <p>
|
||||
* Java API
|
||||
*/
|
||||
def awaitEndpointActivation(count: Int, p: SideEffect): Boolean = {
|
||||
awaitEndpointActivation(count, 10, TimeUnit.SECONDS, p)
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for an expected number (<code>count</code>) of consumer actor endpoints to be activated
|
||||
* during execution of <code>p</code>. Timeout values can be set via the
|
||||
* <code>timeout</code> and <code>timeUnit</code> parameters.
|
||||
* <p>
|
||||
* Java API
|
||||
*/
|
||||
def awaitEndpointActivation(count: Int, timeout: Long, timeUnit: TimeUnit, p: SideEffect): Boolean = {
|
||||
awaitEndpointActivation(count, timeout, timeUnit) { p.apply }
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for an expected number (<code>count</code>) of consumer actor endpoints to be de-activated
|
||||
* during execution of <code>p</code>. The wait timeout is 10 seconds.
|
||||
* <p>
|
||||
* Java API
|
||||
*/
|
||||
def awaitEndpointDeactivation(count: Int, p: SideEffect): Boolean = {
|
||||
awaitEndpointDeactivation(count, 10, TimeUnit.SECONDS, p)
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for an expected number (<code>count</code>) of consumer actor endpoints to be de-activated
|
||||
* during execution of <code>p</code>. Timeout values can be set via the
|
||||
* <code>timeout</code> and <code>timeUnit</code> parameters.
|
||||
* <p>
|
||||
* Java API
|
||||
*/
|
||||
def awaitEndpointDeactivation(count: Int, timeout: Long, timeUnit: TimeUnit, p: SideEffect): Boolean = {
|
||||
awaitEndpointDeactivation(count, timeout, timeUnit) { p.apply }
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an expectation on the number of upcoming endpoint activations and returns
|
||||
* a CountDownLatch that can be used to wait for the activations to occur. Endpoint
|
||||
* activations that occurred in the past are not considered.
|
||||
*/
|
||||
private def expectEndpointActivationCount(count: Int): CountDownLatch =
|
||||
Await.result((activationTracker ? SetExpectedActivationCount(count)).mapTo[CountDownLatch], 3 seconds)
|
||||
|
||||
/**
|
||||
* Sets an expectation on the number of upcoming endpoint de-activations and returns
|
||||
* a CountDownLatch that can be used to wait for the de-activations to occur. Endpoint
|
||||
* de-activations that occurred in the past are not considered.
|
||||
*/
|
||||
private def expectEndpointDeactivationCount(count: Int): CountDownLatch =
|
||||
Await.result((activationTracker ? SetExpectedDeactivationCount(count)).mapTo[CountDownLatch], 3 seconds)
|
||||
|
||||
private[camel] def registerPublishRequestor: Unit =
|
||||
Actor.registry.addListener(publishRequestor)
|
||||
|
||||
private[camel] def unregisterPublishRequestor: Unit =
|
||||
Actor.registry.removeListener(publishRequestor)
|
||||
}
|
||||
|
||||
/**
|
||||
* Manages a CamelService (the 'current' CamelService).
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
object CamelServiceManager {
|
||||
|
||||
/**
|
||||
* The current CamelService which is defined when a CamelService has been started.
|
||||
*/
|
||||
private var _current: Option[CamelService] = None
|
||||
|
||||
/**
|
||||
* Starts a new CamelService, makes it the current CamelService and returns it.
|
||||
*
|
||||
* @see CamelService#start
|
||||
* @see CamelService#onLoad
|
||||
*/
|
||||
def startCamelService = CamelServiceFactory.createCamelService.start
|
||||
|
||||
/**
|
||||
* Stops the current CamelService (if defined).
|
||||
*
|
||||
* @see CamelService#stop
|
||||
* @see CamelService#onUnload
|
||||
*/
|
||||
def stopCamelService = for (s ← service) s.stop
|
||||
|
||||
/**
|
||||
* Returns <code>Some(CamelService)</code> if this <code>CamelService</code>
|
||||
* has been started, <code>None</code> otherwise.
|
||||
*/
|
||||
def service = _current
|
||||
|
||||
/**
|
||||
* Returns the current <code>CamelService</code> if <code>CamelService</code>
|
||||
* has been started, otherwise throws an <code>IllegalStateException</code>.
|
||||
* <p>
|
||||
* Java API
|
||||
*/
|
||||
def getService: JOption[CamelService] = CamelServiceManager.service
|
||||
|
||||
/**
|
||||
* Returns <code>Some(CamelService)</code> (containing the current CamelService)
|
||||
* if this <code>CamelService</code>has been started, <code>None</code> otherwise.
|
||||
*/
|
||||
def mandatoryService =
|
||||
if (_current.isDefined) _current.get
|
||||
else throw new IllegalStateException("co current CamelService")
|
||||
|
||||
/**
|
||||
* Returns <code>Some(CamelService)</code> (containing the current CamelService)
|
||||
* if this <code>CamelService</code>has been started, <code>None</code> otherwise.
|
||||
* <p>
|
||||
* Java API
|
||||
*/
|
||||
def getMandatoryService = mandatoryService
|
||||
|
||||
private[camel] def register(service: CamelService) =
|
||||
if (_current.isDefined) throw new IllegalStateException("current CamelService already registered")
|
||||
else _current = Some(service)
|
||||
|
||||
private[camel] def unregister(service: CamelService) =
|
||||
if (_current == Some(service)) _current = None
|
||||
else throw new IllegalStateException("only current CamelService can be unregistered")
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
object CamelServiceFactory {
|
||||
/**
|
||||
* Creates a new CamelService instance.
|
||||
*/
|
||||
def createCamelService: CamelService = new CamelService {}
|
||||
|
||||
/**
|
||||
* Creates a new CamelService instance and initializes it with the given CamelContext.
|
||||
*/
|
||||
def createCamelService(camelContext: CamelContext): CamelService = {
|
||||
CamelContextManager.init(camelContext)
|
||||
createCamelService
|
||||
}
|
||||
}
|
||||
|
|
@ -1,144 +1,67 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2010 Typesafe Inc. <http://www.typesafe.com>
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import internal.component.DurationTypeConverter
|
||||
import org.apache.camel.model.{ RouteDefinition, ProcessorDefinition }
|
||||
|
||||
import akka.actor._
|
||||
import akka.util.Duration
|
||||
import akka.util.duration._
|
||||
|
||||
/**
|
||||
* Mixed in by Actor implementations that consume message from Camel endpoints.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
trait Consumer { this: Actor ⇒
|
||||
import RouteDefinitionHandler._
|
||||
trait Consumer extends Actor with ConsumerConfig {
|
||||
|
||||
/**
|
||||
* The default route definition handler is the identity function
|
||||
*/
|
||||
private[camel] var routeDefinitionHandler: RouteDefinitionHandler = identity
|
||||
|
||||
/**
|
||||
* Returns the Camel endpoint URI to consume messages from.
|
||||
*/
|
||||
def endpointUri: String
|
||||
protected[this] implicit lazy val camel = CamelExtension(context.system)
|
||||
|
||||
camel.registerConsumer(endpointUri, this, activationTimeout)
|
||||
}
|
||||
|
||||
trait ConsumerConfig {
|
||||
//TODO: Explain the parameters better with some examples!
|
||||
|
||||
/**
|
||||
* Determines whether two-way communications between an endpoint and this consumer actor
|
||||
* should be done in blocking or non-blocking mode (default is non-blocking). This method
|
||||
* doesn't have any effect on one-way communications (they'll never block).
|
||||
* How long should the actor wait for activation before it fails.
|
||||
*/
|
||||
def blocking = false
|
||||
def activationTimeout: Duration = 10 seconds
|
||||
|
||||
/**
|
||||
* When endpoint is out-capable (can produce responses) replyTimeout is the maximum time
|
||||
* the endpoint can take to send the response before the message exchange fails. It defaults to 1 minute.
|
||||
* This setting is used for out-capable, in-only, manually acknowledged communication.
|
||||
* When the blocking is set to Blocking replyTimeout is ignored.
|
||||
*/
|
||||
def replyTimeout: Duration = 1 minute
|
||||
|
||||
/**
|
||||
* Determines whether one-way communications between an endpoint and this consumer actor
|
||||
* should be auto-acknowledged or system-acknowledged.
|
||||
* should be auto-acknowledged or application-acknowledged.
|
||||
* This flag has only effect when exchange is in-only.
|
||||
*/
|
||||
def autoack = true
|
||||
def autoack: Boolean = true
|
||||
|
||||
/**
|
||||
* Sets the route definition handler for creating a custom route to this consumer instance.
|
||||
* The route definition handler for creating a custom route to this consumer instance.
|
||||
*/
|
||||
def onRouteDefinition(h: RouteDefinition ⇒ ProcessorDefinition[_]): Unit = onRouteDefinition(from(h))
|
||||
//TODO: write a test confirming onRouteDefinition gets called
|
||||
def onRouteDefinition(rd: RouteDefinition): ProcessorDefinition[_] = rd
|
||||
|
||||
/**
|
||||
* Sets the route definition handler for creating a custom route to this consumer instance.
|
||||
* <p>
|
||||
* Java API.
|
||||
*/
|
||||
def onRouteDefinition(h: RouteDefinitionHandler): Unit = routeDefinitionHandler = h
|
||||
private[camel] def toCamelParameters: String = "autoack=%s&replyTimeout=%s" format (autoack, DurationTypeConverter.toString(replyTimeout))
|
||||
}
|
||||
|
||||
/**
|
||||
* Java-friendly Consumer.
|
||||
*
|
||||
* Subclass this abstract class to create an MDB-style untyped consumer actor. This
|
||||
* class is meant to be used from Java.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
abstract class UntypedConsumerActor extends UntypedActor with Consumer {
|
||||
final override def endpointUri = getEndpointUri
|
||||
final override def blocking = isBlocking
|
||||
final override def autoack = isAutoack
|
||||
|
||||
/**
|
||||
* Returns the Camel endpoint URI to consume messages from.
|
||||
*/
|
||||
def getEndpointUri(): String
|
||||
|
||||
/**
|
||||
* Determines whether two-way communications between an endpoint and this consumer actor
|
||||
* should be done in blocking or non-blocking mode (default is non-blocking). This method
|
||||
* doesn't have any effect on one-way communications (they'll never block).
|
||||
*/
|
||||
def isBlocking() = super.blocking
|
||||
|
||||
/**
|
||||
* Determines whether one-way communications between an endpoint and this consumer actor
|
||||
* should be auto-acknowledged or system-acknowledged.
|
||||
*/
|
||||
def isAutoack() = super.autoack
|
||||
trait ManualAckConsumer extends Consumer {
|
||||
override def autoack = false
|
||||
}
|
||||
|
||||
/**
|
||||
* A callback handler for route definitions to consumer actors.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
trait RouteDefinitionHandler {
|
||||
def onRouteDefinition(rd: RouteDefinition): ProcessorDefinition[_]
|
||||
}
|
||||
|
||||
/**
|
||||
* The identity route definition handler.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*
|
||||
*/
|
||||
class RouteDefinitionIdentity extends RouteDefinitionHandler {
|
||||
def onRouteDefinition(rd: RouteDefinition) = rd
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
object RouteDefinitionHandler {
|
||||
/**
|
||||
* Returns the identity route definition handler
|
||||
*/
|
||||
val identity = new RouteDefinitionIdentity
|
||||
|
||||
/**
|
||||
* Created a route definition handler from the given function.
|
||||
*/
|
||||
def from(f: RouteDefinition ⇒ ProcessorDefinition[_]) = new RouteDefinitionHandler {
|
||||
def onRouteDefinition(rd: RouteDefinition) = f(rd)
|
||||
trait ErrorPassing { self: Actor ⇒
|
||||
final override def preRestart(reason: Throwable, message: Option[Any]) {
|
||||
sender ! Failure(reason)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
private[camel] object Consumer {
|
||||
/**
|
||||
* Applies a function <code>f</code> to <code>actorRef</code> if <code>actorRef</code>
|
||||
* references a consumer actor. A valid reference to a consumer actor is a local actor
|
||||
* reference with a target actor that implements the <code>Consumer</code> trait. The
|
||||
* target <code>Consumer</code> instance is passed as argument to <code>f</code>. This
|
||||
* method returns <code>None</code> if <code>actorRef</code> is not a valid reference
|
||||
* to a consumer actor, <code>Some</code> contained the return value of <code>f</code>
|
||||
* otherwise.
|
||||
*/
|
||||
def withConsumer[T](actorRef: ActorRef)(f: Consumer ⇒ T): Option[T] = actorRef match {
|
||||
case l: LocalActorRef ⇒
|
||||
l.underlyingActorInstance match {
|
||||
case c: Consumer ⇒ Some(f(c))
|
||||
case _ ⇒ None
|
||||
}
|
||||
case _ ⇒ None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,221 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2010 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
package akka.camel
|
||||
|
||||
import java.io.InputStream
|
||||
import java.util.concurrent.CountDownLatch
|
||||
|
||||
import org.apache.camel.builder.RouteBuilder
|
||||
import org.apache.camel.model.RouteDefinition
|
||||
|
||||
import akka.actor._
|
||||
import akka.event.EventHandler
|
||||
|
||||
/**
|
||||
* Concrete publish requestor that requests publication of consumer actors on <code>ActorRegistered</code>
|
||||
* events and unpublication of consumer actors on <code>ActorUnregistered</code> events.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
private[camel] class ConsumerPublishRequestor extends PublishRequestor {
|
||||
def receiveActorRegistryEvent = {
|
||||
case ActorRegistered(_, actor) ⇒ for (event ← ConsumerActorRegistered.eventFor(actor)) deliverCurrentEvent(event)
|
||||
case ActorUnregistered(_, actor) ⇒ for (event ← ConsumerActorUnregistered.eventFor(actor)) deliverCurrentEvent(event)
|
||||
case _ ⇒ ()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Publishes consumer actors on <code>ConsumerActorRegistered</code> events and unpublishes
|
||||
* consumer actors on <code>ConsumerActorUnregistered</code> events. Publications are tracked
|
||||
* by sending an <code>activationTracker</code> an <code>EndpointActivated</code> event,
|
||||
* unpublications are tracked by sending an <code>EndpointActivated</code> event.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
private[camel] class ConsumerPublisher(activationTracker: ActorRef) extends Actor {
|
||||
import ConsumerPublisher._
|
||||
|
||||
def receive = {
|
||||
case r: ConsumerActorRegistered ⇒ {
|
||||
handleConsumerActorRegistered(r)
|
||||
activationTracker ! EndpointActivated
|
||||
}
|
||||
case u: ConsumerActorUnregistered ⇒ {
|
||||
handleConsumerActorUnregistered(u)
|
||||
activationTracker ! EndpointDeactivated
|
||||
}
|
||||
case _ ⇒ { /* ignore */ }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
private[camel] object ConsumerPublisher {
|
||||
/**
|
||||
* Creates a route to the registered consumer actor.
|
||||
*/
|
||||
def handleConsumerActorRegistered(event: ConsumerActorRegistered) {
|
||||
CamelContextManager.mandatoryContext.addRoutes(new ConsumerActorRouteBuilder(event))
|
||||
EventHandler notifyListeners EventHandler.Info(this, "published actor %s at endpoint %s" format (event.actorRef, event.endpointUri))
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the route to the already un-registered consumer actor.
|
||||
*/
|
||||
def handleConsumerActorUnregistered(event: ConsumerActorUnregistered) {
|
||||
CamelContextManager.mandatoryContext.stopRoute(event.uuid)
|
||||
EventHandler notifyListeners EventHandler.Info(this, "unpublished actor %s from endpoint %s" format (event.actorRef, event.endpointUri))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract builder of a route to a target which can be either an actor or an typed actor method.
|
||||
*
|
||||
* @param endpointUri endpoint URI of the consumer actor or typed actor method.
|
||||
* @param id unique route identifier.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
private[camel] abstract class ConsumerRouteBuilder(endpointUri: String, id: String) extends RouteBuilder {
|
||||
// TODO: make conversions configurable
|
||||
private val bodyConversions = Map(
|
||||
"file" -> classOf[InputStream])
|
||||
|
||||
def configure = {
|
||||
val schema = endpointUri take endpointUri.indexOf(":") // e.g. "http" from "http://whatever/..."
|
||||
val cnvopt = bodyConversions.get(schema)
|
||||
|
||||
onRouteDefinition(startRouteDefinition(cnvopt)).to(targetUri)
|
||||
}
|
||||
|
||||
protected def routeDefinitionHandler: RouteDefinitionHandler
|
||||
protected def targetUri: String
|
||||
|
||||
private def onRouteDefinition(rd: RouteDefinition) = routeDefinitionHandler.onRouteDefinition(rd)
|
||||
private def startRouteDefinition(bodyConversion: Option[Class[_]]): RouteDefinition = bodyConversion match {
|
||||
case Some(clazz) ⇒ from(endpointUri).routeId(id).convertBodyTo(clazz)
|
||||
case None ⇒ from(endpointUri).routeId(id)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder of a route to a consumer actor.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
private[camel] class ConsumerActorRouteBuilder(event: ConsumerActorRegistered) extends ConsumerRouteBuilder(event.endpointUri, event.uuid) {
|
||||
protected def routeDefinitionHandler: RouteDefinitionHandler = event.routeDefinitionHandler
|
||||
protected def targetUri = "actor:uuid:%s?blocking=%s&autoack=%s" format (event.uuid, event.blocking, event.autoack)
|
||||
}
|
||||
|
||||
/**
|
||||
* Tracks <code>EndpointActivated</code> and <code>EndpointDectivated</code> events. Used to wait for a
|
||||
* certain number of endpoints activations and de-activations to occur.
|
||||
*
|
||||
* @see SetExpectedActivationCount
|
||||
* @see SetExpectedDeactivationCount
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
private[camel] class ActivationTracker extends Actor {
|
||||
private var activationLatch = new CountDownLatch(0)
|
||||
private var deactivationLatch = new CountDownLatch(0)
|
||||
|
||||
def receive = {
|
||||
case SetExpectedActivationCount(num) ⇒ {
|
||||
activationLatch = new CountDownLatch(num)
|
||||
sender ! activationLatch
|
||||
}
|
||||
case SetExpectedDeactivationCount(num) ⇒ {
|
||||
deactivationLatch = new CountDownLatch(num)
|
||||
sender ! deactivationLatch
|
||||
}
|
||||
case EndpointActivated ⇒ activationLatch.countDown
|
||||
case EndpointDeactivated ⇒ deactivationLatch.countDown
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Command message that sets the number of expected endpoint activations on <code>ActivationTracker</code>.
|
||||
*/
|
||||
private[camel] case class SetExpectedActivationCount(num: Int)
|
||||
|
||||
/**
|
||||
* Command message that sets the number of expected endpoint de-activations on <code>ActivationTracker</code>.
|
||||
*/
|
||||
private[camel] case class SetExpectedDeactivationCount(num: Int)
|
||||
|
||||
/**
|
||||
* Event message indicating that a single endpoint has been activated.
|
||||
*/
|
||||
private[camel] case class EndpointActivated()
|
||||
|
||||
/**
|
||||
* Event message indicating that a single endpoint has been de-activated.
|
||||
*/
|
||||
private[camel] case class EndpointDeactivated()
|
||||
|
||||
/**
|
||||
* A consumer (un)registration event.
|
||||
*/
|
||||
private[camel] trait ConsumerEvent {
|
||||
val uuid: String
|
||||
}
|
||||
|
||||
/**
|
||||
* A consumer actor (un)registration event.
|
||||
*/
|
||||
private[camel] trait ConsumerActorEvent extends ConsumerEvent {
|
||||
val actorRef: ActorRef
|
||||
val actor: Consumer
|
||||
|
||||
val uuid = actorRef.uuid.toString
|
||||
val endpointUri = actor.endpointUri
|
||||
val blocking = actor.blocking
|
||||
val autoack = actor.autoack
|
||||
val routeDefinitionHandler = actor.routeDefinitionHandler
|
||||
}
|
||||
|
||||
/**
|
||||
* Event indicating that a consumer actor has been registered at the actor registry.
|
||||
*/
|
||||
private[camel] case class ConsumerActorRegistered(actorRef: ActorRef, actor: Consumer) extends ConsumerActorEvent
|
||||
|
||||
/**
|
||||
* Event indicating that a consumer actor has been unregistered from the actor registry.
|
||||
*/
|
||||
private[camel] case class ConsumerActorUnregistered(actorRef: ActorRef, actor: Consumer) extends ConsumerActorEvent
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
private[camel] object ConsumerActorRegistered {
|
||||
/**
|
||||
* Creates an ConsumerActorRegistered event message for a consumer actor or None if
|
||||
* <code>actorRef</code> is not a consumer actor.
|
||||
*/
|
||||
def eventFor(actorRef: ActorRef): Option[ConsumerActorRegistered] = {
|
||||
Consumer.withConsumer[ConsumerActorRegistered](actorRef) { actor ⇒
|
||||
ConsumerActorRegistered(actorRef, actor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
private[camel] object ConsumerActorUnregistered {
|
||||
/**
|
||||
* Creates an ConsumerActorUnregistered event message for a consumer actor or None if
|
||||
* <code>actorRef</code> is not a consumer actor.
|
||||
*/
|
||||
def eventFor(actorRef: ActorRef): Option[ConsumerActorUnregistered] = {
|
||||
Consumer.withConsumer[ConsumerActorUnregistered](actorRef) { actor ⇒
|
||||
ConsumerActorUnregistered(actorRef, actor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,63 +1,31 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2010 Typesafe Inc. <http://www.typesafe.com>
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import java.util.{ Map ⇒ JMap, Set ⇒ JSet }
|
||||
|
||||
import scala.collection.JavaConversions._
|
||||
|
||||
import org.apache.camel.{ Exchange, Message ⇒ CamelMessage }
|
||||
import org.apache.camel.util.ExchangeHelper
|
||||
|
||||
import akka.japi.{ Function ⇒ JFunction }
|
||||
import org.apache.camel.{ CamelContext, Message ⇒ CamelMessage }
|
||||
|
||||
/**
|
||||
* An immutable representation of a Camel message.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
case class Message(val body: Any, val headers: Map[String, Any] = Map.empty) {
|
||||
case class Message(body: Any, headers: Map[String, Any]) {
|
||||
|
||||
/**
|
||||
* Creates a Message with given body and empty headers map.
|
||||
*/
|
||||
def this(body: Any) = this(body, Map.empty[String, Any])
|
||||
def this(body: Any, headers: JMap[String, Any]) = this(body, headers.toMap) //for Java
|
||||
|
||||
/**
|
||||
* Creates a Message with given body and headers map. A copy of the headers map is made.
|
||||
* <p>
|
||||
* Java API
|
||||
*/
|
||||
def this(body: Any, headers: JMap[String, Any]) = this(body, headers.toMap)
|
||||
|
||||
/**
|
||||
* Returns the body of the message converted to the type <code>T</code>. Conversion is done
|
||||
* using Camel's type converter. The type converter is obtained from the CamelContext managed
|
||||
* by CamelContextManager. Applications have to ensure proper initialization of
|
||||
* CamelContextManager.
|
||||
*
|
||||
* @see CamelContextManager.
|
||||
*/
|
||||
def bodyAs[T](implicit m: Manifest[T]): T = getBodyAs(m.erasure.asInstanceOf[Class[T]])
|
||||
|
||||
/**
|
||||
* Returns the body of the message converted to the type as given by the <code>clazz</code>
|
||||
* parameter. Conversion is done using Camel's type converter. The type converter is obtained
|
||||
* from the CamelContext managed by CamelContextManager. Applications have to ensure proper
|
||||
* initialization of CamelContextManager.
|
||||
* <p>
|
||||
* Java API
|
||||
*
|
||||
* @see CamelContextManager.
|
||||
*/
|
||||
def getBodyAs[T](clazz: Class[T]): T =
|
||||
CamelContextManager.mandatoryContext.getTypeConverter.mandatoryConvertTo[T](clazz, body)
|
||||
override def toString = "Message(%s, %s)" format (body, headers)
|
||||
|
||||
/**
|
||||
* Returns those headers from this message whose name is contained in <code>names</code>.
|
||||
*/
|
||||
def headers(names: Set[String]): Map[String, Any] = headers.filter(names contains _._1)
|
||||
def headers(names: Set[String]): Map[String, Any] = headers.filterKeys(names contains _)
|
||||
|
||||
/**
|
||||
* Returns those headers from this message whose name is contained in <code>names</code>.
|
||||
|
|
@ -66,7 +34,7 @@ case class Message(val body: Any, val headers: Map[String, Any] = Map.empty) {
|
|||
* <p>
|
||||
* Java API
|
||||
*/
|
||||
def getHeaders(names: JSet[String]): JMap[String, Any] = headers.filter(names contains _._1)
|
||||
def getHeaders(names: JSet[String]): JMap[String, Any] = headers(names.toSet)
|
||||
|
||||
/**
|
||||
* Returns all headers from this message. The returned headers map is backed up by this
|
||||
|
|
@ -81,7 +49,7 @@ case class Message(val body: Any, val headers: Map[String, Any] = Map.empty) {
|
|||
* Returns the header with given <code>name</code>. Throws <code>NoSuchElementException</code>
|
||||
* if the header doesn't exist.
|
||||
*/
|
||||
def header(name: String): Any = headers(name)
|
||||
def header(name: String): Option[Any] = headers.get(name)
|
||||
|
||||
/**
|
||||
* Returns the header with given <code>name</code>. Throws <code>NoSuchElementException</code>
|
||||
|
|
@ -89,69 +57,41 @@ case class Message(val body: Any, val headers: Map[String, Any] = Map.empty) {
|
|||
* <p>
|
||||
* Java API
|
||||
*/
|
||||
def getHeader(name: String): Any = header(name)
|
||||
|
||||
/**
|
||||
* Returns the header with given <code>name</code> converted to type <code>T</code>. Throws
|
||||
* <code>NoSuchElementException</code> if the header doesn't exist.
|
||||
*/
|
||||
def headerAs[T](name: String)(implicit m: Manifest[T]): T =
|
||||
getHeaderAs(name, m.erasure.asInstanceOf[Class[T]])
|
||||
|
||||
/**
|
||||
* Returns the header with given <code>name</code> converted to type as given by the <code>clazz</code>
|
||||
* parameter. Throws <code>NoSuchElementException</code> if the header doesn't exist.
|
||||
* <p>
|
||||
* Java API
|
||||
*/
|
||||
def getHeaderAs[T](name: String, clazz: Class[T]): T =
|
||||
CamelContextManager.mandatoryContext.getTypeConverter.mandatoryConvertTo[T](clazz, header(name))
|
||||
def getHeader(name: String): Any = headers(name)
|
||||
|
||||
/**
|
||||
* Creates a Message with a transformed body using a <code>transformer</code> function.
|
||||
*/
|
||||
def transformBody[A](transformer: A ⇒ Any): Message = setBody(transformer(body.asInstanceOf[A]))
|
||||
def mapBody[A, B](transformer: A ⇒ B): Message = withBody(transformer(body.asInstanceOf[A]))
|
||||
|
||||
/**
|
||||
* Creates a Message with a transformed body using a <code>transformer</code> function.
|
||||
* <p>
|
||||
* Java API
|
||||
*/
|
||||
def transformBody[A](transformer: JFunction[A, Any]): Message = setBody(transformer(body.asInstanceOf[A]))
|
||||
|
||||
/**
|
||||
* Creates a Message with current <code>body</code> converted to type <code>T</code>.
|
||||
*/
|
||||
def setBodyAs[T](implicit m: Manifest[T]): Message = setBodyAs(m.erasure.asInstanceOf[Class[T]])
|
||||
|
||||
/**
|
||||
* Creates a Message with current <code>body</code> converted to type <code>clazz</code>.
|
||||
* <p>
|
||||
* Java API
|
||||
*/
|
||||
def setBodyAs[T](clazz: Class[T]): Message = setBody(getBodyAs(clazz))
|
||||
def mapBody[A, B](transformer: JFunction[A, B]): Message = withBody(transformer(body.asInstanceOf[A]))
|
||||
|
||||
/**
|
||||
* Creates a Message with a given <code>body</code>.
|
||||
*/
|
||||
def setBody(body: Any) = new Message(body, this.headers)
|
||||
def withBody(body: Any) = Message(body, this.headers)
|
||||
|
||||
/**
|
||||
* Creates a new Message with given <code>headers</code>.
|
||||
*/
|
||||
def setHeaders(headers: Map[String, Any]): Message = copy(this.body, headers)
|
||||
def withHeaders[A](headers: Map[String, A]): Message = copy(this.body, headers)
|
||||
|
||||
/**
|
||||
* Creates a new Message with given <code>headers</code>. A copy of the headers map is made.
|
||||
* <p>
|
||||
* Java API
|
||||
*/
|
||||
def setHeaders(headers: JMap[String, Any]): Message = setHeaders(headers.toMap)
|
||||
def withHeaders[A](headers: JMap[String, A]): Message = withHeaders(headers.toMap)
|
||||
|
||||
/**
|
||||
* Creates a new Message with given <code>headers</code> added to the current headers.
|
||||
*/
|
||||
def addHeaders(headers: Map[String, Any]): Message = copy(this.body, this.headers ++ headers)
|
||||
def plusHeaders[A](headers: Map[String, A]): Message = copy(this.body, this.headers ++ headers)
|
||||
|
||||
/**
|
||||
* Creates a new Message with given <code>headers</code> added to the current headers.
|
||||
|
|
@ -159,12 +99,12 @@ case class Message(val body: Any, val headers: Map[String, Any] = Map.empty) {
|
|||
* <p>
|
||||
* Java API
|
||||
*/
|
||||
def addHeaders(headers: JMap[String, Any]): Message = addHeaders(headers.toMap)
|
||||
def plusHeaders[A](headers: JMap[String, A]): Message = plusHeaders(headers.toMap)
|
||||
|
||||
/**
|
||||
* Creates a new Message with the given <code>header</code> added to the current headers.
|
||||
*/
|
||||
def addHeader(header: (String, Any)): Message = copy(this.body, this.headers + header)
|
||||
def plusHeader(header: (String, Any)): Message = copy(this.body, this.headers + header)
|
||||
|
||||
/**
|
||||
* Creates a new Message with the given header, represented by <code>name</code> and
|
||||
|
|
@ -172,13 +112,71 @@ case class Message(val body: Any, val headers: Map[String, Any] = Map.empty) {
|
|||
* <p>
|
||||
* Java API
|
||||
*/
|
||||
def addHeader(name: String, value: Any): Message = addHeader((name, value))
|
||||
def plusHeader(name: String, value: Any): Message = plusHeader((name, value))
|
||||
|
||||
/**
|
||||
* Creates a new Message where the header with given <code>headerName</code> is removed from
|
||||
* the existing headers.
|
||||
*/
|
||||
def removeHeader(headerName: String) = copy(this.body, this.headers - headerName)
|
||||
def withoutHeader(headerName: String) = copy(this.body, this.headers - headerName)
|
||||
|
||||
def copyContentTo(to: CamelMessage) = {
|
||||
to.setBody(this.body)
|
||||
for ((name, value) ← this.headers) to.getHeaders.put(name, value.asInstanceOf[AnyRef])
|
||||
}
|
||||
}
|
||||
|
||||
class RichMessage(message: Message, camelContext: CamelContext) {
|
||||
/**
|
||||
* Returns the body of the message converted to the type <code>T</code>. Conversion is done
|
||||
* using Camel's type converter. The type converter is obtained from the CamelContext managed
|
||||
* by CamelContextManager. Applications have to ensure proper initialization of
|
||||
* CamelContextManager.
|
||||
*
|
||||
* @see CamelContextManager.
|
||||
*/
|
||||
|
||||
def bodyAs[T](implicit m: Manifest[T]): T = getBodyAs(m.erasure.asInstanceOf[Class[T]])
|
||||
|
||||
/**
|
||||
* Returns the body of the message converted to the type as given by the <code>clazz</code>
|
||||
* parameter. Conversion is done using Camel's type converter. The type converter is obtained
|
||||
* from the CamelContext managed by CamelContextManager. Applications have to ensure proper
|
||||
* initialization of CamelContextManager.
|
||||
* <p>
|
||||
* Java API
|
||||
*
|
||||
* @see CamelContextManager.
|
||||
*/
|
||||
def getBodyAs[T](clazz: Class[T]): T =
|
||||
camelContext.getTypeConverter.mandatoryConvertTo[T](clazz, message.body)
|
||||
|
||||
/**
|
||||
* Creates a Message with current <code>body</code> converted to type <code>T</code>.
|
||||
*/
|
||||
def withBodyAs[T](implicit m: Manifest[T]): Message = withBodyAs(m.erasure.asInstanceOf[Class[T]])
|
||||
|
||||
/**
|
||||
* Creates a Message with current <code>body</code> converted to type <code>clazz</code>.
|
||||
* <p>
|
||||
* Java API
|
||||
*/
|
||||
def withBodyAs[T](clazz: Class[T]): Message = message.withBody(getBodyAs(clazz))
|
||||
|
||||
/**
|
||||
* Returns the header with given <code>name</code> converted to type <code>T</code>. Throws
|
||||
* <code>NoSuchElementException</code> if the header doesn't exist.
|
||||
*/
|
||||
def headerAs[T](name: String)(implicit m: Manifest[T]): Option[T] = message.header(name).map(camelContext.getTypeConverter.mandatoryConvertTo[T](m.erasure.asInstanceOf[Class[T]], _))
|
||||
|
||||
/**
|
||||
* Returns the header with given <code>name</code> converted to type as given by the <code>clazz</code>
|
||||
* parameter. Throws <code>NoSuchElementException</code> if the header doesn't exist.
|
||||
* <p>
|
||||
* Java API
|
||||
*/
|
||||
def getHeaderAs[T](name: String, clazz: Class[T]) = headerAs[T](name)(Manifest.classType(clazz)).get
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -203,12 +201,26 @@ object Message {
|
|||
*/
|
||||
def canonicalize(msg: Any) = msg match {
|
||||
case mobj: Message ⇒ mobj
|
||||
case body ⇒ new Message(body)
|
||||
case body ⇒ Message(body, Map.empty)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Message object from the Camel message.
|
||||
*/
|
||||
def from(camelMessage: CamelMessage): Message = from(camelMessage, Map.empty)
|
||||
|
||||
/**
|
||||
* Creates a new Message object from the Camel message.
|
||||
*
|
||||
* @param headers additional headers to set on the created Message in addition to those
|
||||
* in the Camel message.
|
||||
*/
|
||||
def from(camelMessage: CamelMessage, headers: Map[String, Any]): Message = Message(camelMessage.getBody, headers ++ camelMessage.getHeaders)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Positive acknowledgement message (used for system-acknowledged message receipts).
|
||||
* Positive acknowledgement message (used for application-acknowledged message receipts).
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
|
|
@ -253,133 +265,4 @@ case class Failure(val cause: Throwable, val headers: Map[String, Any] = Map.emp
|
|||
* Java API
|
||||
*/
|
||||
def getHeaders: JMap[String, Any] = headers
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapter for converting an org.apache.camel.Exchange to and from Message and Failure objects.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
class CamelExchangeAdapter(exchange: Exchange) {
|
||||
import CamelMessageConversion.toMessageAdapter
|
||||
|
||||
/**
|
||||
* Sets Exchange.getIn from the given Message object.
|
||||
*/
|
||||
def fromRequestMessage(msg: Message): Exchange = { requestMessage.fromMessage(msg); exchange }
|
||||
|
||||
/**
|
||||
* Depending on the exchange pattern, sets Exchange.getIn or Exchange.getOut from the given
|
||||
* Message object. If the exchange is out-capable then the Exchange.getOut is set, otherwise
|
||||
* Exchange.getIn.
|
||||
*/
|
||||
def fromResponseMessage(msg: Message): Exchange = { responseMessage.fromMessage(msg); exchange }
|
||||
|
||||
/**
|
||||
* Sets Exchange.getException from the given Failure message. Headers of the Failure message
|
||||
* are ignored.
|
||||
*/
|
||||
def fromFailureMessage(msg: Failure): Exchange = { exchange.setException(msg.cause); exchange }
|
||||
|
||||
/**
|
||||
* Creates a Message object from Exchange.getIn.
|
||||
*/
|
||||
def toRequestMessage: Message = toRequestMessage(Map.empty)
|
||||
|
||||
/**
|
||||
* Depending on the exchange pattern, creates a Message object from Exchange.getIn or Exchange.getOut.
|
||||
* If the exchange is out-capable then the Exchange.getOut is set, otherwise Exchange.getIn.
|
||||
*/
|
||||
def toResponseMessage: Message = toResponseMessage(Map.empty)
|
||||
|
||||
/**
|
||||
* Creates a Failure object from the adapted Exchange.
|
||||
*
|
||||
* @see Failure
|
||||
*/
|
||||
def toFailureMessage: Failure = toFailureMessage(Map.empty)
|
||||
|
||||
/**
|
||||
* Creates a Message object from Exchange.getIn.
|
||||
*
|
||||
* @param headers additional headers to set on the created Message in addition to those
|
||||
* in the Camel message.
|
||||
*/
|
||||
def toRequestMessage(headers: Map[String, Any]): Message = requestMessage.toMessage(headers)
|
||||
|
||||
/**
|
||||
* Depending on the exchange pattern, creates a Message object from Exchange.getIn or Exchange.getOut.
|
||||
* If the exchange is out-capable then the Exchange.getOut is set, otherwise Exchange.getIn.
|
||||
*
|
||||
* @param headers additional headers to set on the created Message in addition to those
|
||||
* in the Camel message.
|
||||
*/
|
||||
def toResponseMessage(headers: Map[String, Any]): Message = responseMessage.toMessage(headers)
|
||||
|
||||
/**
|
||||
* Creates a Failure object from the adapted Exchange.
|
||||
*
|
||||
* @param headers additional headers to set on the created Message in addition to those
|
||||
* in the Camel message.
|
||||
*
|
||||
* @see Failure
|
||||
*/
|
||||
def toFailureMessage(headers: Map[String, Any]): Failure =
|
||||
Failure(exchange.getException, headers ++ responseMessage.toMessage.headers)
|
||||
|
||||
private def requestMessage = exchange.getIn
|
||||
|
||||
private def responseMessage = ExchangeHelper.getResultMessage(exchange)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapter for converting an org.apache.camel.Message to and from Message objects.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
class CamelMessageAdapter(val cm: CamelMessage) {
|
||||
/**
|
||||
* Set the adapted Camel message from the given Message object.
|
||||
*/
|
||||
def fromMessage(m: Message): CamelMessage = {
|
||||
cm.setBody(m.body)
|
||||
for (h ← m.headers) cm.getHeaders.put(h._1, h._2.asInstanceOf[AnyRef])
|
||||
cm
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Message object from the adapted Camel message.
|
||||
*/
|
||||
def toMessage: Message = toMessage(Map.empty)
|
||||
|
||||
/**
|
||||
* Creates a new Message object from the adapted Camel message.
|
||||
*
|
||||
* @param headers additional headers to set on the created Message in addition to those
|
||||
* in the Camel message.
|
||||
*/
|
||||
def toMessage(headers: Map[String, Any]): Message = Message(cm.getBody, cmHeaders(headers, cm))
|
||||
|
||||
private def cmHeaders(headers: Map[String, Any], cm: CamelMessage) = headers ++ cm.getHeaders
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines conversion methods to CamelExchangeAdapter and CamelMessageAdapter.
|
||||
* Imported by applications that implicitly want to use conversion methods of
|
||||
* CamelExchangeAdapter and CamelMessageAdapter.
|
||||
*/
|
||||
object CamelMessageConversion {
|
||||
|
||||
/**
|
||||
* Creates an CamelExchangeAdapter for the given Camel exchange.
|
||||
*/
|
||||
implicit def toExchangeAdapter(ce: Exchange): CamelExchangeAdapter =
|
||||
new CamelExchangeAdapter(ce)
|
||||
|
||||
/**
|
||||
* Creates an CamelMessageAdapter for the given Camel message.
|
||||
*/
|
||||
implicit def toMessageAdapter(cm: CamelMessage): CamelMessageAdapter =
|
||||
new CamelMessageAdapter(cm)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,16 +1,12 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2010 Typesafe Inc. <http://www.typesafe.com>
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import CamelMessageConversion.toExchangeAdapter
|
||||
|
||||
import org.apache.camel._
|
||||
import org.apache.camel.processor.SendProcessor
|
||||
|
||||
import akka.actor.{ Actor, ActorRef, UntypedActor }
|
||||
import akka.dispatch.ActorPromise
|
||||
import akka.actor.Actor
|
||||
import internal.CamelExchangeAdapter
|
||||
import org.apache.camel.{ Exchange, ExchangePattern, AsyncCallback }
|
||||
|
||||
/**
|
||||
* Support trait for producing messages to Camel endpoints.
|
||||
|
|
@ -18,23 +14,15 @@ import akka.dispatch.ActorPromise
|
|||
* @author Martin Krasser
|
||||
*/
|
||||
trait ProducerSupport { this: Actor ⇒
|
||||
protected[this] implicit lazy val camel = CamelExtension(context.system)
|
||||
|
||||
protected[this] lazy val (endpoint, processor) = camel.registerProducer(self, endpointUri)
|
||||
|
||||
/**
|
||||
* Message headers to copy by default from request message to response-message.
|
||||
*/
|
||||
private val headersToCopyDefault = Set(Message.MessageExchangeId)
|
||||
|
||||
/**
|
||||
* <code>Endpoint</code> object resolved from the current CamelContext with
|
||||
* <code>endpointUri</code>.
|
||||
*/
|
||||
private lazy val endpoint = CamelContextManager.mandatoryContext.getEndpoint(endpointUri)
|
||||
|
||||
/**
|
||||
* <code>SendProcessor</code> for producing messages to <code>endpoint</code>.
|
||||
*/
|
||||
private lazy val processor = createSendProcessor
|
||||
|
||||
/**
|
||||
* If set to false (default), this producer expects a response message from the Camel endpoint.
|
||||
* If set to true, this producer initiates an in-only message exchange with the Camel endpoint
|
||||
|
|
@ -50,32 +38,10 @@ trait ProducerSupport { this: Actor ⇒
|
|||
/**
|
||||
* Returns the names of message headers to copy from a request message to a response message.
|
||||
* By default only the Message.MessageExchangeId is copied. Applications may override this to
|
||||
* define an system-specific set of message headers to copy.
|
||||
* define an application-specific set of message headers to copy.
|
||||
*/
|
||||
def headersToCopy: Set[String] = headersToCopyDefault
|
||||
|
||||
/**
|
||||
* Default implementation of <code>Actor.preRestart</code> for freeing resources needed
|
||||
* to actually send messages to <code>endpointUri</code>.
|
||||
*/
|
||||
override def preRestart(reason: Throwable, msg: Option[Any]) {
|
||||
try { preRestartProducer(reason) } finally { processor.stop }
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing by default. Can be overridden by concrete producers for implementing a
|
||||
* pre-restart callback handler.
|
||||
*/
|
||||
def preRestartProducer(reason: Throwable) {}
|
||||
|
||||
/**
|
||||
* Default implementation of <code>Actor.postStop</code> for freeing resources needed
|
||||
* to actually send messages to <code>endpointUri</code>.
|
||||
*/
|
||||
override def postStop {
|
||||
processor.stop
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates a message exchange of given <code>pattern</code> with the endpoint specified by
|
||||
* <code>endpointUri</code>. The in-message of the initiated exchange is the canonical form
|
||||
|
|
@ -83,44 +49,34 @@ trait ProducerSupport { this: Actor ⇒
|
|||
* as argument to <code>receiveAfterProduce</code>. If the response is received synchronously from
|
||||
* the endpoint then <code>receiveAfterProduce</code> is called synchronously as well. If the
|
||||
* response is received asynchronously, the <code>receiveAfterProduce</code> is called
|
||||
* asynchronously. This is done by wrapping the response, adding it to this producers
|
||||
* mailbox, unwrapping it and calling <code>receiveAfterProduce</code>. The original
|
||||
* sender and senderFuture are thereby preserved.
|
||||
* asynchronously. The original
|
||||
* sender and senderFuture are preserved.
|
||||
*
|
||||
* @see Message#canonicalize(Any)
|
||||
*
|
||||
* @param msg message to produce
|
||||
* @param pattern exchange pattern
|
||||
*/
|
||||
protected def produce(msg: Any, pattern: ExchangePattern) {
|
||||
protected def produce(msg: Any, pattern: ExchangePattern): Unit = {
|
||||
implicit def toExchangeAdapter(exchange: Exchange): CamelExchangeAdapter = new CamelExchangeAdapter(exchange)
|
||||
|
||||
val cmsg = Message.canonicalize(msg)
|
||||
val exchange = createExchange(pattern).fromRequestMessage(cmsg)
|
||||
val exchange = endpoint.createExchange(pattern)
|
||||
exchange.setRequest(cmsg)
|
||||
processor.process(exchange, new AsyncCallback {
|
||||
val producer = self
|
||||
// Need copies of sender reference here since the callback could be done
|
||||
// later by another thread.
|
||||
val replyChannel = sender
|
||||
val originalSender = sender
|
||||
|
||||
def done(doneSync: Boolean) {
|
||||
(doneSync, exchange.isFailed) match {
|
||||
case (true, true) ⇒ dispatchSync(exchange.toFailureMessage(cmsg.headers(headersToCopy)))
|
||||
case (true, false) ⇒ dispatchSync(exchange.toResponseMessage(cmsg.headers(headersToCopy)))
|
||||
case (false, true) ⇒ dispatchAsync(FailureResult(exchange.toFailureMessage(cmsg.headers(headersToCopy))))
|
||||
case (false, false) ⇒ dispatchAsync(MessageResult(exchange.toResponseMessage(cmsg.headers(headersToCopy))))
|
||||
}
|
||||
}
|
||||
|
||||
private def dispatchSync(result: Any) =
|
||||
receiveAfterProduce(result)
|
||||
|
||||
private def dispatchAsync(result: Any) = {
|
||||
replyChannel match {
|
||||
case _: ActorPromise ⇒
|
||||
producer.postMessageToMailboxAndCreateFutureResultWithTimeout(result, producer.timeout, replyChannel)
|
||||
case _ ⇒
|
||||
producer.postMessageToMailbox(result, replyChannel)
|
||||
def done(doneSync: Boolean): Unit = {
|
||||
if (exchange.isFailed) {
|
||||
dispatch(FailureResult(exchange.toFailureMessage(cmsg.headers(headersToCopy))))
|
||||
} else {
|
||||
dispatch(MessageResult(exchange.toResponseMessage(cmsg.headers(headersToCopy))))
|
||||
}
|
||||
}
|
||||
private def dispatch(result: Any) { producer.tell(result, originalSender) }
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -162,20 +118,6 @@ trait ProducerSupport { this: Actor ⇒
|
|||
case msg ⇒ if (!oneway) sender ! msg
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Exchange of given <code>pattern</code> from the endpoint specified by
|
||||
* <code>endpointUri</code>.
|
||||
*/
|
||||
private def createExchange(pattern: ExchangePattern): Exchange = endpoint.createExchange(pattern)
|
||||
|
||||
/**
|
||||
* Creates a new <code>SendProcessor</code> for <code>endpoint</code>.
|
||||
*/
|
||||
private def createSendProcessor = {
|
||||
val sendProcessor = new SendProcessor(endpoint)
|
||||
sendProcessor.start()
|
||||
sendProcessor
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -190,58 +132,6 @@ trait Producer extends ProducerSupport { this: Actor ⇒
|
|||
protected def receive = produce
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclass this abstract class to create an untyped producer actor. This class is meant to be used from Java.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
abstract class UntypedProducerActor extends UntypedActor with ProducerSupport {
|
||||
final override def endpointUri = getEndpointUri
|
||||
final override def oneway = isOneway
|
||||
|
||||
final override def receiveBeforeProduce = {
|
||||
case msg ⇒ onReceiveBeforeProduce(msg)
|
||||
}
|
||||
|
||||
final override def receiveAfterProduce = {
|
||||
case msg ⇒ onReceiveAfterProduce(msg)
|
||||
}
|
||||
|
||||
/**
|
||||
* Default implementation of UntypedActor.onReceive
|
||||
*/
|
||||
def onReceive(message: Any) = produce(message)
|
||||
|
||||
/**
|
||||
* Returns the Camel endpoint URI to produce messages to.
|
||||
*/
|
||||
def getEndpointUri(): String
|
||||
|
||||
/**
|
||||
* If set to false (default), this producer expects a response message from the Camel endpoint.
|
||||
* If set to true, this producer communicates with the Camel endpoint with an in-only message
|
||||
* exchange pattern (fire and forget).
|
||||
*/
|
||||
def isOneway() = super.oneway
|
||||
|
||||
/**
|
||||
* Called before the message is sent to the endpoint specified by <code>getEndpointUri</code>. The original
|
||||
* message is passed as argument. By default, this method simply returns the argument but may be overridden
|
||||
* by subclasses.
|
||||
*/
|
||||
@throws(classOf[Exception])
|
||||
def onReceiveBeforeProduce(message: Any): Any = super.receiveBeforeProduce(message)
|
||||
|
||||
/**
|
||||
* Called after a response was received from the endpoint specified by <code>endpointUri</code>. The
|
||||
* response is passed as argument. By default, this method sends the response back to the original sender
|
||||
* if <code>oneway</code> is <code>false</code>. If <code>oneway</code> is <code>true</code>, nothing is
|
||||
* done. This method may be overridden by subclasses (e.g. to forward responses to another actor).
|
||||
*/
|
||||
@throws(classOf[Exception])
|
||||
def onReceiveAfterProduce(message: Any): Unit = super.receiveAfterProduce(message)
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -1,64 +0,0 @@
|
|||
package akka.camel
|
||||
|
||||
import akka.actor._
|
||||
|
||||
/**
|
||||
* Base class for concrete (un)publish requestors. Subclasses are responsible for requesting
|
||||
* (un)publication of consumer actors by calling <code>deliverCurrentEvent</code>.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
private[camel] abstract class PublishRequestor extends Actor {
|
||||
private val events = collection.mutable.Set[ConsumerEvent]()
|
||||
private var publisher: Option[ActorRef] = None
|
||||
|
||||
def receiveActorRegistryEvent: Receive
|
||||
|
||||
/**
|
||||
* Accepts
|
||||
* <ul>
|
||||
* <li><code>InitPublishRequestor</code> messages to configure a publisher for this requestor.</li>
|
||||
* <li><code>ActorRegistryEvent</code> messages to be handled <code>receiveActorRegistryEvent</code>
|
||||
* implementators</li>.
|
||||
* </ul>
|
||||
* Other messages are simply ignored. Calls to <code>deliverCurrentEvent</code> prior to setting a
|
||||
* publisher are buffered. They will be sent after a publisher has been set.
|
||||
*/
|
||||
def receive = {
|
||||
case InitPublishRequestor(pub) ⇒ {
|
||||
publisher = Some(pub)
|
||||
deliverBufferedEvents
|
||||
}
|
||||
case e: ActorRegistryEvent ⇒ receiveActorRegistryEvent(e)
|
||||
case _ ⇒ { /* ignore */ }
|
||||
}
|
||||
|
||||
/**
|
||||
* Deliver the given <code>event</code> to <code>publisher</code> or buffer the event if
|
||||
* <code>publisher</code> is not defined yet.
|
||||
*/
|
||||
protected def deliverCurrentEvent(event: ConsumerEvent) {
|
||||
publisher match {
|
||||
case Some(pub) ⇒ pub ! event
|
||||
case None ⇒ events += event
|
||||
}
|
||||
}
|
||||
|
||||
private def deliverBufferedEvents {
|
||||
for (event ← events) deliverCurrentEvent(event)
|
||||
events.clear
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
private[camel] object PublishRequestor {
|
||||
def pastActorRegisteredEvents = for (actor ← Actor.registry.local.actors) yield ActorRegistered(actor.address, actor)
|
||||
}
|
||||
|
||||
/**
|
||||
* Command message to initialize a PublishRequestor to use <code>publisher</code>
|
||||
* for publishing consumer actors.
|
||||
*/
|
||||
private[camel] case class InitPublishRequestor(publisher: ActorRef)
|
||||
|
|
@ -1,36 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import org.apache.camel.CamelContext
|
||||
import akka.event.EventHandler
|
||||
|
||||
import akka.util.ReflectiveAccess.getObjectFor
|
||||
|
||||
/**
|
||||
* Module for reflective access to akka-camel-typed.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
private[camel] object TypedCamelAccess {
|
||||
val loader = getClass.getClassLoader
|
||||
|
||||
object TypedCamelModule {
|
||||
|
||||
type TypedCamelObject = {
|
||||
def onCamelContextInit(context: CamelContext): Unit
|
||||
def onCamelServiceStart(service: CamelService): Unit
|
||||
def onCamelServiceStop(service: CamelService): Unit
|
||||
}
|
||||
|
||||
val typedCamelObject: Option[TypedCamelObject] =
|
||||
getObjectFor("akka.camel.TypedCamel$", loader) match {
|
||||
case Right(value) ⇒ Some(value)
|
||||
case Left(exception) ⇒
|
||||
EventHandler.debug(this, exception.toString)
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,302 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2010 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel.component
|
||||
|
||||
import java.net.InetSocketAddress
|
||||
import java.util.{ Map ⇒ JMap }
|
||||
import java.util.concurrent.TimeoutException
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
|
||||
import org.apache.camel._
|
||||
import org.apache.camel.impl.{ DefaultProducer, DefaultEndpoint, DefaultComponent }
|
||||
|
||||
import akka.actor._
|
||||
import akka.camel.{ Ack, Failure, Message }
|
||||
import akka.camel.CamelMessageConversion.toExchangeAdapter
|
||||
import scala.reflect.BeanProperty
|
||||
import akka.dispatch._
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
object ActorComponent {
|
||||
/**
|
||||
* Name of the message header containing the actor id or uuid.
|
||||
*/
|
||||
val ActorIdentifier = "CamelActorIdentifier"
|
||||
}
|
||||
|
||||
/**
|
||||
* Camel component for sending messages to and receiving replies from (untyped) actors.
|
||||
*
|
||||
* @see akka.camel.component.ActorEndpoint
|
||||
* @see akka.camel.component.ActorProducer
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
class ActorComponent extends DefaultComponent {
|
||||
def createEndpoint(uri: String, remaining: String, parameters: JMap[String, Object]): ActorEndpoint = {
|
||||
val (idType, idValue) = parsePath(remaining)
|
||||
new ActorEndpoint(uri, this, idType, idValue)
|
||||
}
|
||||
|
||||
private def parsePath(remaining: String): Tuple2[String, Option[String]] = remaining match {
|
||||
case null | "" ⇒ throw new IllegalArgumentException("invalid path: [%s] - should be <actorid> or id:<actorid> or uuid:<actoruuid>" format remaining)
|
||||
case id if id startsWith "id:" ⇒ ("id", parseIdentifier(id substring 3))
|
||||
case uuid if uuid startsWith "uuid:" ⇒ ("uuid", parseIdentifier(uuid substring 5))
|
||||
case id ⇒ ("id", parseIdentifier(id))
|
||||
}
|
||||
|
||||
private def parseIdentifier(identifier: String): Option[String] =
|
||||
if (identifier.length > 0) Some(identifier) else None
|
||||
}
|
||||
|
||||
/**
|
||||
* Camel endpoint for sending messages to and receiving replies from (untyped) actors. Actors
|
||||
* are referenced using <code>actor</code> endpoint URIs of the following format:
|
||||
* <code>actor:<actor-id></code>,
|
||||
* <code>actor:id:[<actor-id>]</code> and
|
||||
* <code>actor:uuid:[<actor-uuid>]</code>,
|
||||
* where <code><actor-id></code> refers to <code>ActorRef.id</code> and <code><actor-uuid></code>
|
||||
* refers to the String-representation od <code>ActorRef.uuid</code>. In URIs that contain
|
||||
* <code>id:</code> or <code>uuid:</code>, an actor identifier (id or uuid) is optional. In this
|
||||
* case, the in-message of an exchange produced to this endpoint must contain a message header
|
||||
* with name <code>CamelActorIdentifier</code> and a value that is the target actor's identifier.
|
||||
* If the URI contains an actor identifier, a message with a <code>CamelActorIdentifier</code>
|
||||
* header overrides the identifier in the endpoint URI.
|
||||
*
|
||||
* @see akka.camel.component.ActorComponent
|
||||
* @see akka.camel.component.ActorProducer
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
class ActorEndpoint(uri: String,
|
||||
comp: ActorComponent,
|
||||
val idType: String,
|
||||
val idValue: Option[String]) extends DefaultEndpoint(uri, comp) {
|
||||
|
||||
/**
|
||||
* Whether to block caller thread during two-way message exchanges with (untyped) actors. This is
|
||||
* set via the <code>blocking=true|false</code> endpoint URI parameter. Default value is
|
||||
* <code>false</code>.
|
||||
*/
|
||||
@BeanProperty
|
||||
var blocking: Boolean = false
|
||||
|
||||
/**
|
||||
* Whether to auto-acknowledge one-way message exchanges with (untyped) actors. This is
|
||||
* set via the <code>blocking=true|false</code> endpoint URI parameter. Default value is
|
||||
* <code>true</code>. When set to <code>true</code> consumer actors need to additionally
|
||||
* call <code>Consumer.ack</code> within <code>Actor.receive</code>.
|
||||
*/
|
||||
@BeanProperty
|
||||
var autoack: Boolean = true
|
||||
|
||||
/**
|
||||
* @throws UnsupportedOperationException
|
||||
*/
|
||||
def createConsumer(processor: Processor): Consumer =
|
||||
throw new UnsupportedOperationException("actor consumer not supported yet")
|
||||
|
||||
/**
|
||||
* Creates a new ActorProducer instance initialized with this endpoint.
|
||||
*/
|
||||
def createProducer: ActorProducer = new ActorProducer(this)
|
||||
|
||||
/**
|
||||
* Returns true.
|
||||
*/
|
||||
def isSingleton: Boolean = true
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the in-message of an exchange to an (untyped) actor, identified by an
|
||||
* actor endpoint URI or by a <code>CamelActorIdentifier</code> message header.
|
||||
* <ul>
|
||||
* <li>If the exchange pattern is out-capable and <code>blocking</code> is set to
|
||||
* <code>true</code> then the producer waits for a reply, using the !! operator.</li>
|
||||
* <li>If the exchange pattern is out-capable and <code>blocking</code> is set to
|
||||
* <code>false</code> then the producer sends the message using the ! operator, together
|
||||
* with a callback handler. The callback handler is an <code>ActorRef</code> that can be
|
||||
* used by the receiving actor to asynchronously reply to the route that is sending the
|
||||
* message.</li>
|
||||
* <li>If the exchange pattern is in-only then the producer sends the message using the
|
||||
* ! operator.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @see akka.camel.component.ActorComponent
|
||||
* @see akka.camel.component.ActorEndpoint
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
class ActorProducer(val ep: ActorEndpoint) extends DefaultProducer(ep) with AsyncProcessor {
|
||||
import ActorProducer._
|
||||
|
||||
private lazy val uuid = uuidFrom(ep.idValue.getOrElse(throw new ActorIdentifierNotSetException))
|
||||
|
||||
def process(exchange: Exchange) =
|
||||
if (exchange.getPattern.isOutCapable) sendSync(exchange) else sendAsync(exchange)
|
||||
|
||||
def process(exchange: Exchange, callback: AsyncCallback): Boolean = {
|
||||
(exchange.getPattern.isOutCapable, ep.blocking, ep.autoack) match {
|
||||
case (true, true, _) ⇒ {
|
||||
sendSync(exchange)
|
||||
callback.done(true)
|
||||
true
|
||||
}
|
||||
case (true, false, _) ⇒ {
|
||||
sendAsync(exchange, Some(AsyncCallbackAdapter(exchange, callback)))
|
||||
false
|
||||
}
|
||||
case (false, false, true) ⇒ {
|
||||
sendAsync(exchange)
|
||||
callback.done(true)
|
||||
true
|
||||
}
|
||||
case (false, false, false) ⇒ {
|
||||
sendAsync(exchange, Some(AsyncCallbackAdapter(exchange, callback)))
|
||||
false
|
||||
}
|
||||
case (false, true, false) ⇒ {
|
||||
sendSync(exchange)
|
||||
callback.done(true)
|
||||
true
|
||||
}
|
||||
case (false, true, true) ⇒ {
|
||||
throw new IllegalStateException("cannot have blocking=true and autoack=true for in-only message exchanges")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private def sendSync(exchange: Exchange) = {
|
||||
val actor = target(exchange)
|
||||
val result: Any = try { Some(Await.result((actor ? requestFor(exchange), 5 seconds)) } catch { case e ⇒ Some(Failure(e)) }
|
||||
|
||||
result match {
|
||||
case Some(Ack) ⇒ { /* no response message to set */ }
|
||||
case Some(msg: Failure) ⇒ exchange.fromFailureMessage(msg)
|
||||
case Some(msg) ⇒ exchange.fromResponseMessage(Message.canonicalize(msg))
|
||||
case None ⇒ throw new TimeoutException("timeout (%d ms) while waiting response from %s"
|
||||
format (actor.timeout, ep.getEndpointUri))
|
||||
}
|
||||
}
|
||||
|
||||
private def sendAsync(exchange: Exchange, sender: Option[ActorRef] = None) =
|
||||
target(exchange).!(requestFor(exchange))(sender)
|
||||
|
||||
private def target(exchange: Exchange) =
|
||||
targetOption(exchange) getOrElse (throw new ActorNotRegisteredException(ep.getEndpointUri))
|
||||
|
||||
private def targetOption(exchange: Exchange): Option[ActorRef] = ep.idType match {
|
||||
case "id" ⇒ targetById(targetId(exchange))
|
||||
case "uuid" ⇒ targetByUuid(targetUuid(exchange))
|
||||
}
|
||||
|
||||
private def targetId(exchange: Exchange) = exchange.getIn.getHeader(ActorComponent.ActorIdentifier) match {
|
||||
case id: String ⇒ id
|
||||
case null ⇒ ep.idValue.getOrElse(throw new ActorIdentifierNotSetException)
|
||||
}
|
||||
|
||||
private def targetUuid(exchange: Exchange) = exchange.getIn.getHeader(ActorComponent.ActorIdentifier) match {
|
||||
case uuid: Uuid ⇒ uuid
|
||||
case uuid: String ⇒ uuidFrom(uuid)
|
||||
case null ⇒ uuid
|
||||
}
|
||||
|
||||
private def targetById(id: String) = Actor.registry.local.actorFor(id)
|
||||
private def targetByUuid(uuid: Uuid) = Actor.registry.local.actorFor(uuid)
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
private[camel] object ActorProducer {
|
||||
def requestFor(exchange: Exchange) =
|
||||
exchange.toRequestMessage(Map(Message.MessageExchangeId -> exchange.getExchangeId))
|
||||
}
|
||||
|
||||
/**
|
||||
* Thrown to indicate that an actor referenced by an endpoint URI cannot be
|
||||
* found in the Actor.registry.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
class ActorNotRegisteredException(uri: String) extends RuntimeException {
|
||||
override def getMessage = "'%s' not registered" format uri
|
||||
}
|
||||
|
||||
/**
|
||||
* Thrown to indicate that no actor identifier has been set.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
class ActorIdentifierNotSetException extends RuntimeException {
|
||||
override def getMessage = "actor identifier not set"
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
private[akka] object AsyncCallbackAdapter {
|
||||
/**
|
||||
* Creates and starts an <code>AsyncCallbackAdapter</code>.
|
||||
*
|
||||
* @param exchange message exchange to write results to.
|
||||
* @param callback callback object to generate completion notifications.
|
||||
*/
|
||||
def apply(exchange: Exchange, callback: AsyncCallback) =
|
||||
new AsyncCallbackAdapter(exchange, callback).start
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapts an <code>ActorRef</code> to a Camel <code>AsyncCallback</code>. Used by receiving actors to reply
|
||||
* asynchronously to Camel routes with <code>ActorRef.reply</code>.
|
||||
* <p>
|
||||
* <em>Please note</em> that this adapter can only be used locally at the moment which should not
|
||||
* be a problem is most situations since Camel endpoints are only activated for local actor references,
|
||||
* never for remote references.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
private[akka] class AsyncCallbackAdapter(exchange: Exchange, callback: AsyncCallback) extends ActorRef with ScalaActorRef {
|
||||
import akka.camel.Consumer._
|
||||
|
||||
val address = exchange.getExchangeId
|
||||
|
||||
@volatile
|
||||
private var running: Boolean = true
|
||||
|
||||
def isTerminated: Boolean = !running
|
||||
|
||||
def suspend(): Unit = ()
|
||||
|
||||
def resume(): Unit = ()
|
||||
|
||||
def stop() { running = false }
|
||||
|
||||
/**
|
||||
* Populates the initial <code>exchange</code> with the reply <code>message</code> and uses the
|
||||
* <code>callback</code> handler to notify Camel about the asynchronous completion of the message
|
||||
* exchange.
|
||||
*
|
||||
* @param message reply message
|
||||
* @param sender ignored
|
||||
*/
|
||||
protected[akka] def postMessageToMailbox(message: Any, sender: ActorRef) = if(running) {
|
||||
message match {
|
||||
case Ack ⇒ { /* no response message to set */ }
|
||||
case msg: Failure ⇒ exchange.fromFailureMessage(msg)
|
||||
case msg ⇒ exchange.fromResponseMessage(Message.canonicalize(msg))
|
||||
}
|
||||
callback.done(false)
|
||||
}
|
||||
|
||||
def ?(message: Any)(implicit timeout: Timeout): Future[Any] =
|
||||
Promise.failed(new UnsupportedOperationException("Ask/? is not supported for %s".format(getClass.getName)))
|
||||
def restart(reason: Throwable): Unit = unsupported
|
||||
|
||||
private def unsupported = throw new UnsupportedOperationException("Not supported for %s" format classOf[AsyncCallbackAdapter].getName)
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel.internal
|
||||
|
||||
import akka.actor._
|
||||
import collection.mutable.WeakHashMap
|
||||
|
||||
class ActivationTracker extends Actor with ActorLogging {
|
||||
val activations = new WeakHashMap[ActorRef, ActivationStateMachine]
|
||||
|
||||
class ActivationStateMachine {
|
||||
type State = PartialFunction[ActivationMessage, Unit]
|
||||
|
||||
var receive: State = notActivated()
|
||||
|
||||
def notActivated(): State = {
|
||||
var awaitingActivation = List[ActorRef]()
|
||||
var awaitingDeActivation = List[ActorRef]()
|
||||
|
||||
{
|
||||
case AwaitActivation(ref) ⇒ awaitingActivation ::= sender
|
||||
case AwaitDeActivation(ref) ⇒ awaitingDeActivation ::= sender
|
||||
|
||||
case msg @ EndpointActivated(ref) ⇒ {
|
||||
awaitingActivation.foreach(_ ! msg)
|
||||
receive = activated(awaitingDeActivation)
|
||||
}
|
||||
|
||||
case EndpointFailedToActivate(ref, cause) ⇒ {
|
||||
awaitingActivation.foreach(_ ! EndpointFailedToActivate(ref, cause))
|
||||
receive = failedToActivate(cause)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def activated(currentAwaitingDeActivation: List[ActorRef]): State = {
|
||||
var awaitingDeActivation = currentAwaitingDeActivation
|
||||
|
||||
{
|
||||
case AwaitActivation(ref) ⇒ sender ! EndpointActivated(ref)
|
||||
case AwaitDeActivation(ref) ⇒ awaitingDeActivation ::= sender
|
||||
case msg @ EndpointDeActivated(ref) ⇒ {
|
||||
awaitingDeActivation foreach (_ ! msg)
|
||||
receive = deactivated
|
||||
}
|
||||
case msg @ EndpointFailedToDeActivate(ref, cause) ⇒ {
|
||||
awaitingDeActivation foreach (_ ! msg)
|
||||
receive = failedToDeActivate(cause)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def deactivated: State = {
|
||||
case AwaitActivation(ref) ⇒ sender ! EndpointActivated(ref)
|
||||
case AwaitDeActivation(ref) ⇒ sender ! EndpointDeActivated(ref)
|
||||
}
|
||||
|
||||
def failedToActivate(cause: Throwable): State = {
|
||||
case AwaitActivation(ref) ⇒ sender ! EndpointFailedToActivate(ref, cause)
|
||||
case AwaitDeActivation(ref) ⇒ sender ! EndpointFailedToActivate(ref, cause)
|
||||
}
|
||||
|
||||
def failedToDeActivate(cause: Throwable): State = {
|
||||
case AwaitActivation(ref) ⇒ sender ! EndpointActivated(ref)
|
||||
case AwaitDeActivation(ref) ⇒ sender ! EndpointFailedToDeActivate(ref, cause)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override def preStart() {
|
||||
context.system.eventStream.subscribe(self, classOf[ActivationMessage])
|
||||
}
|
||||
|
||||
override def receive = {
|
||||
case msg @ ActivationMessage(ref) ⇒ {
|
||||
val state = activations.getOrElseUpdate(ref, new ActivationStateMachine)
|
||||
(state.receive orElse logStateWarning(ref))(msg)
|
||||
}
|
||||
}
|
||||
|
||||
private[this] def logStateWarning(actorRef: ActorRef): Receive = { case msg ⇒ log.warning("Message [{}] not expected in current state of actor [{}]", msg, actorRef) }
|
||||
}
|
||||
|
||||
case class AwaitActivation(ref: ActorRef) extends ActivationMessage(ref)
|
||||
|
||||
case class AwaitDeActivation(ref: ActorRef) extends ActivationMessage(ref)
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
package akka.camel.internal
|
||||
|
||||
import java.util.{ Map ⇒ JMap, Set ⇒ JSet }
|
||||
|
||||
import scala.collection.JavaConversions._
|
||||
|
||||
import org.apache.camel.util.ExchangeHelper
|
||||
|
||||
import akka.japi.{ Function ⇒ JFunction }
|
||||
import org.apache.camel.{ Exchange, Message ⇒ CamelMessage }
|
||||
import akka.camel.{ Failure, Message }
|
||||
|
||||
/**
|
||||
* Adapter for converting an org.apache.camel.Exchange to and from Message and Failure objects.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
//TODO: rething/rewrite this
|
||||
private[camel] class CamelExchangeAdapter(exchange: Exchange) {
|
||||
def getExchangeId = exchange.getExchangeId
|
||||
|
||||
def isOutCapable = exchange.getPattern.isOutCapable
|
||||
|
||||
/**
|
||||
* Sets Exchange.getIn from the given Message object.
|
||||
*/
|
||||
def setRequest(msg: Message) { msg.copyContentTo(request) }
|
||||
|
||||
/**
|
||||
* Depending on the exchange pattern, sets Exchange.getIn or Exchange.getOut from the given
|
||||
* Message object. If the exchange is out-capable then the Exchange.getOut is set, otherwise
|
||||
* Exchange.getIn.
|
||||
*/
|
||||
def setResponse(msg: Message) { msg.copyContentTo(response) }
|
||||
|
||||
/**
|
||||
* Sets Exchange.getException from the given Failure message. Headers of the Failure message
|
||||
* are ignored.
|
||||
*/
|
||||
def setFailure(msg: Failure) { exchange.setException(msg.cause) }
|
||||
|
||||
/**
|
||||
* Creates a Message object from Exchange.getIn.
|
||||
*/
|
||||
def toRequestMessage: Message = toRequestMessage(Map.empty)
|
||||
|
||||
/**
|
||||
* Depending on the exchange pattern, creates a Message object from Exchange.getIn or Exchange.getOut.
|
||||
* If the exchange is out-capable then the Exchange.getOut is set, otherwise Exchange.getIn.
|
||||
*/
|
||||
def toResponseMessage: Message = toResponseMessage(Map.empty)
|
||||
|
||||
/**
|
||||
* Creates a Failure object from the adapted Exchange.
|
||||
*
|
||||
* @see Failure
|
||||
*/
|
||||
def toFailureMessage: Failure = toFailureMessage(Map.empty)
|
||||
|
||||
/**
|
||||
* Creates a Message object from Exchange.getIn.
|
||||
*
|
||||
* @param headers additional headers to set on the created Message in addition to those
|
||||
* in the Camel message.
|
||||
*/
|
||||
def toRequestMessage(headers: Map[String, Any]): Message = Message.from(request, headers)
|
||||
|
||||
/**
|
||||
* Depending on the exchange pattern, creates a Message object from Exchange.getIn or Exchange.getOut.
|
||||
* If the exchange is out-capable then the Exchange.getOut is set, otherwise Exchange.getIn.
|
||||
*
|
||||
* @param headers additional headers to set on the created Message in addition to those
|
||||
* in the Camel message.
|
||||
*/
|
||||
def toResponseMessage(headers: Map[String, Any]): Message = Message.from(response, headers)
|
||||
|
||||
/**
|
||||
* Creates a Failure object from the adapted Exchange.
|
||||
*
|
||||
* @param headers additional headers to set on the created Message in addition to those
|
||||
* in the Camel message.
|
||||
*
|
||||
* @see Failure
|
||||
*/
|
||||
def toFailureMessage(headers: Map[String, Any]): Failure =
|
||||
Failure(exchange.getException, headers ++ response.getHeaders)
|
||||
|
||||
private def request = exchange.getIn
|
||||
|
||||
private def response: CamelMessage = ExchangeHelper.getResultMessage(exchange)
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,155 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel.internal
|
||||
|
||||
import akka.camel._
|
||||
import component.ActorEndpointPath
|
||||
import java.io.InputStream
|
||||
|
||||
import org.apache.camel.builder.RouteBuilder
|
||||
|
||||
import akka.actor._
|
||||
import collection.mutable
|
||||
import org.apache.camel.model.RouteDefinition
|
||||
import org.apache.camel.CamelContext
|
||||
import akka.util.Duration
|
||||
|
||||
/**
|
||||
* Manages consumer registration. Consumers call registerConsumer method to register themselves when they get created.
|
||||
* ActorEndpoint uses it to lookup an actor by its path.
|
||||
*/
|
||||
private[camel] trait ConsumerRegistry { this: Activation ⇒
|
||||
def system: ActorSystem
|
||||
def context: CamelContext
|
||||
|
||||
private[this] lazy val idempotentRegistry = system.actorOf(Props(new IdempotentCamelConsumerRegistry(context)))
|
||||
|
||||
private[camel] def registerConsumer(endpointUri: String, consumer: Consumer, activationTimeout: Duration) = {
|
||||
idempotentRegistry ! RegisterConsumer(endpointUri, consumer.self, consumer)
|
||||
awaitActivation(consumer.self, activationTimeout)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Guarantees idempotent registration of camel consumer endpoints.
|
||||
*
|
||||
* Once registered the consumer is watched and unregistered upon termination.
|
||||
* It also publishes events to the eventStream so interested parties could subscribe to them.
|
||||
* The main consumer of these events is currently the ActivationTracker.
|
||||
*/
|
||||
private[camel] class IdempotentCamelConsumerRegistry(camelContext: CamelContext) extends Actor {
|
||||
|
||||
case class UnregisterConsumer(actorRef: ActorRef)
|
||||
|
||||
val activated = new mutable.HashSet[ActorRef]
|
||||
|
||||
val registrator = context.actorOf(Props(new CamelConsumerRegistrator))
|
||||
|
||||
def receive = {
|
||||
case msg @ RegisterConsumer(_, consumer, _) ⇒ unless(isAlreadyActivated(consumer)) {
|
||||
activated.add(consumer)
|
||||
registrator ! msg
|
||||
}
|
||||
case msg @ EndpointActivated(consumer) ⇒ {
|
||||
context.watch(consumer)
|
||||
context.system.eventStream.publish(msg)
|
||||
}
|
||||
case msg @ EndpointFailedToActivate(consumer, _) ⇒ {
|
||||
activated.remove(consumer)
|
||||
context.system.eventStream.publish(msg)
|
||||
}
|
||||
case Terminated(ref) ⇒ {
|
||||
activated.remove(ref)
|
||||
registrator ! UnregisterConsumer(ref)
|
||||
}
|
||||
case msg @ EndpointDeActivated(ref) ⇒ { context.system.eventStream.publish(msg) }
|
||||
case msg @ EndpointFailedToDeActivate(ref, cause) ⇒ { context.system.eventStream.publish(msg) }
|
||||
}
|
||||
|
||||
def unless[A](condition: Boolean)(block: ⇒ A) = if (!condition) block
|
||||
def isAlreadyActivated(ref: ActorRef): Boolean = activated.contains(ref)
|
||||
|
||||
class CamelConsumerRegistrator extends Actor with ActorLogging {
|
||||
|
||||
def receive = {
|
||||
case RegisterConsumer(endpointUri, consumer, consumerConfig) ⇒ {
|
||||
camelContext.addRoutes(new ConsumerActorRouteBuilder(endpointUri, consumer, consumerConfig))
|
||||
context.sender ! EndpointActivated(consumer)
|
||||
log.debug("Published actor [{}] at endpoint [{}]", consumerConfig, endpointUri)
|
||||
}
|
||||
|
||||
case UnregisterConsumer(consumer) ⇒ {
|
||||
camelContext.stopRoute(consumer.path.toString)
|
||||
context.sender ! EndpointDeActivated(consumer)
|
||||
log.debug("Unpublished actor [{}] from endpoint [{}]", consumer, consumer.path)
|
||||
}
|
||||
}
|
||||
|
||||
override def preRestart(reason: Throwable, message: Option[Any]) {
|
||||
message match {
|
||||
case Some(RegisterConsumer(_, consumer, _)) ⇒ sender ! EndpointFailedToActivate(consumer, reason)
|
||||
case Some(UnregisterConsumer(consumer)) ⇒ sender ! EndpointFailedToDeActivate(consumer, reason)
|
||||
case _ ⇒
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private[camel] case class RegisterConsumer(endpointUri: String, actorRef: ActorRef, config: ConsumerConfig)
|
||||
|
||||
/**
|
||||
* Abstract builder of a route to a target which can be either an actor or an typed actor method.
|
||||
*
|
||||
* @param endpointUri endpoint URI of the consumer actor or typed actor method.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
private[camel] class ConsumerActorRouteBuilder(endpointUri: String, consumer: ActorRef, config: ConsumerConfig) extends RouteBuilder {
|
||||
|
||||
//TODO: what if actorpath contains parameters? Should we use url encoding? But this will look ugly...
|
||||
protected def targetActorUri = ActorEndpointPath(consumer).toCamelPath(config)
|
||||
|
||||
def configure() {
|
||||
val scheme = endpointUri take endpointUri.indexOf(":") // e.g. "http" from "http://whatever/..."
|
||||
|
||||
val route = from(endpointUri).routeId(consumer.path.toString)
|
||||
val converted = Conversions.apply(scheme, route)
|
||||
val userCustomized = applyUserRouteCustomization(converted)
|
||||
userCustomized.to(targetActorUri)
|
||||
}
|
||||
|
||||
def applyUserRouteCustomization(rd: RouteDefinition) = config.onRouteDefinition(rd)
|
||||
|
||||
object Conversions {
|
||||
// TODO: make conversions configurable
|
||||
private val bodyConversions = Map(
|
||||
"file" -> classOf[InputStream])
|
||||
|
||||
def apply(scheme: String, routeDefinition: RouteDefinition): RouteDefinition = bodyConversions.get(scheme) match {
|
||||
case Some(clazz) ⇒ routeDefinition.convertBodyTo(clazz)
|
||||
case None ⇒ routeDefinition
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Super class of all activation messages.
|
||||
*/
|
||||
private[camel] abstract class ActivationMessage(val actor: ActorRef)
|
||||
private[camel] object ActivationMessage {
|
||||
def unapply(msg: ActivationMessage): Option[ActorRef] = Some(msg.actor)
|
||||
}
|
||||
|
||||
/**
|
||||
* Event message indicating that a single endpoint has been activated.
|
||||
*/
|
||||
private[camel] case class EndpointActivated(actorRef: ActorRef) extends ActivationMessage(actorRef)
|
||||
|
||||
private[camel] case class EndpointFailedToActivate(actorRef: ActorRef, cause: Throwable) extends ActivationMessage(actorRef)
|
||||
|
||||
private[camel] case class EndpointDeActivated(actorRef: ActorRef) extends ActivationMessage(actorRef)
|
||||
|
||||
private[camel] case class EndpointFailedToDeActivate(actorRef: ActorRef, cause: Throwable) extends ActivationMessage(actorRef)
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
package akka.camel.internal
|
||||
|
||||
import akka.actor.ActorSystem
|
||||
import component.{ DurationTypeConverter, ActorComponent }
|
||||
import org.apache.camel.CamelContext
|
||||
import org.apache.camel.impl.DefaultCamelContext
|
||||
import akka.util.Duration
|
||||
import scala.Predef._
|
||||
import akka.event.Logging
|
||||
import akka.camel.Camel
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
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()
|
||||
Try(template.start()) otherwise context.stop()
|
||||
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() {
|
||||
try context.stop() finally Try.safe(template.stop())
|
||||
log.debug("Stopped CamelContext[{}] for ActorSystem[{}]", context.getName, system.name)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
package akka.camel.internal
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import org.apache.camel.processor.SendProcessor
|
||||
import akka.actor.{ Props, ActorRef, Terminated, Actor }
|
||||
import org.apache.camel.Endpoint
|
||||
import akka.camel.Camel
|
||||
|
||||
/**
|
||||
* Watches the end of life of <code>Producer</code>s.
|
||||
* Removes a <code>Producer</code> from the <code>ProducerRegistry</code> when it is <code>Terminated</code>,
|
||||
* which in turn stops the <code>SendProcessor</code>.
|
||||
*/
|
||||
private[camel] class ProducerWatcher(registry: ProducerRegistry) extends Actor {
|
||||
override def receive = {
|
||||
case RegisterProducer(actorRef) ⇒ {
|
||||
context.watch(actorRef)
|
||||
}
|
||||
case Terminated(actorRef) ⇒ {
|
||||
registry.unregisterProducer(actorRef)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private[camel] case class RegisterProducer(actorRef: ActorRef)
|
||||
|
||||
/**
|
||||
* Manages the Camel objects for <code>Producer</code>s.
|
||||
* Every <code>Producer</code> needs an <code>Endpoint</code> and a <code>SendProcessor</code>
|
||||
* to produce messages over an <code>Exchange</code>.
|
||||
*/
|
||||
private[camel] trait ProducerRegistry {
|
||||
this: Camel ⇒
|
||||
private val camelObjects = new ConcurrentHashMap[ActorRef, (Endpoint, SendProcessor)]()
|
||||
private val watcher = system.actorOf(Props(new ProducerWatcher(this)))
|
||||
|
||||
private def registerWatch(actorRef: ActorRef) {
|
||||
watcher ! RegisterProducer(actorRef)
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters <code>Endpoint</code> and <code>SendProcessor</code> and stops the SendProcessor
|
||||
*/
|
||||
private[camel] def unregisterProducer(actorRef: ActorRef): Unit = {
|
||||
// Terminated cannot be sent before the actor is created in the processing of system messages.
|
||||
Option(camelObjects.remove(actorRef)).foreach {
|
||||
case (_, processor) ⇒
|
||||
try {
|
||||
processor.stop()
|
||||
system.eventStream.publish(EndpointDeActivated(actorRef))
|
||||
} catch {
|
||||
case e ⇒ system.eventStream.publish(EndpointFailedToDeActivate(actorRef, e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates <code>Endpoint</code> and <code>SendProcessor</code> and associates the actorRef to these.
|
||||
* @param actorRef the actorRef of the <code>Producer</code> actor.
|
||||
* @param endpointUri the endpoint Uri of the producer
|
||||
* @return <code>Endpoint</code> and <code>SendProcessor</code> registered for the actorRef
|
||||
*/
|
||||
private[camel] def registerProducer(actorRef: ActorRef, endpointUri: String): (Endpoint, SendProcessor) = {
|
||||
try {
|
||||
val endpoint = context.getEndpoint(endpointUri)
|
||||
val processor = new SendProcessor(endpoint)
|
||||
|
||||
val prev = camelObjects.putIfAbsent(actorRef, (endpoint, processor))
|
||||
if (prev != null) {
|
||||
prev
|
||||
} else {
|
||||
processor.start()
|
||||
system.eventStream.publish(EndpointActivated(actorRef))
|
||||
registerWatch(actorRef)
|
||||
(endpoint, processor)
|
||||
}
|
||||
} catch {
|
||||
case e ⇒ {
|
||||
system.eventStream.publish(EndpointFailedToActivate(actorRef, e))
|
||||
// can't return null to the producer actor, so blow up actor in initialization.
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
53
akka-camel/src/main/scala/akka/camel/internal/Try.scala
Normal file
53
akka-camel/src/main/scala/akka/camel/internal/Try.scala
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package akka.camel.internal
|
||||
|
||||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
import akka.event.LoggingAdapter
|
||||
|
||||
private[camel] object Try {
|
||||
|
||||
/**
|
||||
* Tries to execute body.
|
||||
*
|
||||
* Example below tries to start template and if it's unsuccessful it stops context in a safe way, by logging exceptions and then it rethrows exception thrown by template.start()
|
||||
* <pre> Try(template.start()) otherwise context.stop() </pre>
|
||||
*
|
||||
* @param body block of code to execute.
|
||||
* @return Ok, if no exception is thrown by body.
|
||||
* @return Failed, if exception was thrown by body.
|
||||
*
|
||||
*/
|
||||
def apply(body: ⇒ Unit): Result =
|
||||
try {
|
||||
body; Ok
|
||||
} catch {
|
||||
case e ⇒ Failed(e)
|
||||
}
|
||||
|
||||
sealed trait Result {
|
||||
def otherwise(onError: ⇒ Unit)(implicit log: LoggingAdapter): Unit = ()
|
||||
}
|
||||
|
||||
private[this] case object Ok extends Result
|
||||
|
||||
private[this] case class Failed(e: Throwable) extends Result {
|
||||
override def otherwise(onError: ⇒ Unit)(implicit log: LoggingAdapter) = {
|
||||
safe(onError)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the block and logs the exception, if it's thrown by the block, and swallows the exception.
|
||||
*/
|
||||
@inline def safe(block: ⇒ Unit)(implicit log: LoggingAdapter) {
|
||||
try {
|
||||
block
|
||||
} catch {
|
||||
case e ⇒ log.warning("Safe operation failed. Swallowing exception [{}]", e)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,239 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel.internal.component
|
||||
|
||||
import java.util.{ Map ⇒ JMap }
|
||||
|
||||
import org.apache.camel._
|
||||
import org.apache.camel.impl.{ DefaultProducer, DefaultEndpoint, DefaultComponent }
|
||||
|
||||
import akka.actor._
|
||||
import akka.pattern._
|
||||
|
||||
import scala.reflect.BeanProperty
|
||||
import akka.util.{ Duration, Timeout }
|
||||
import akka.util.duration._
|
||||
import java.util.concurrent.{ TimeoutException, CountDownLatch }
|
||||
import akka.camel.{ ConsumerConfig, Camel, Ack, Failure ⇒ CamelFailure, Message }
|
||||
import akka.camel.internal.CamelExchangeAdapter
|
||||
|
||||
/**
|
||||
* Camel component for sending messages to and receiving replies from (untyped) actors.
|
||||
*
|
||||
* @see akka.camel.component.ActorEndpoint
|
||||
* @see akka.camel.component.ActorProducer
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
class ActorComponent(camel: Camel) extends DefaultComponent {
|
||||
def createEndpoint(uri: String, remaining: String, parameters: JMap[String, Object]): ActorEndpoint = {
|
||||
val path = ActorEndpointPath.fromCamelPath(remaining)
|
||||
new ActorEndpoint(uri, this, path, camel)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO fix the doc to be consistent with implementation
|
||||
* Camel endpoint for sending messages to and receiving replies from (untyped) actors. Actors
|
||||
* are referenced using <code>actor</code> endpoint URIs of the following format:
|
||||
* <code>actor:<actor-id></code>,
|
||||
* <code>actor:id:[<actor-id>]</code> and
|
||||
* <code>actor:uuid:[<actor-uuid>]</code>,
|
||||
* where <code><actor-id></code> refers to <code>ActorRef.id</code> and <code><actor-uuid></code>
|
||||
* refers to the String-representation od <code>ActorRef.uuid</code>. In URIs that contain
|
||||
* <code>id:</code> or <code>uuid:</code>, an actor identifier (id or uuid) is optional. In this
|
||||
* case, the in-message of an exchange produced to this endpoint must contain a message header
|
||||
* with name <code>CamelActorIdentifier</code> and a value that is the target actor's identifier.
|
||||
* If the URI contains an actor identifier, a message with a <code>CamelActorIdentifier</code>
|
||||
* header overrides the identifier in the endpoint URI.
|
||||
*
|
||||
* @see akka.camel.component.ActorComponent
|
||||
* @see akka.camel.component.ActorProducer
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
class ActorEndpoint(uri: String,
|
||||
comp: ActorComponent,
|
||||
val path: ActorEndpointPath,
|
||||
camel: Camel) extends DefaultEndpoint(uri, comp) with ActorEndpointConfig {
|
||||
|
||||
/**
|
||||
* @throws UnsupportedOperationException
|
||||
*/
|
||||
def createConsumer(processor: Processor): org.apache.camel.Consumer =
|
||||
throw new UnsupportedOperationException("actor consumer not supported yet")
|
||||
|
||||
/**
|
||||
* Creates a new ActorProducer instance initialized with this endpoint.
|
||||
*/
|
||||
def createProducer: ActorProducer = new ActorProducer(this, camel)
|
||||
|
||||
/**
|
||||
* Returns true.
|
||||
*/
|
||||
def isSingleton: Boolean = true
|
||||
}
|
||||
|
||||
trait ActorEndpointConfig {
|
||||
def path: ActorEndpointPath
|
||||
|
||||
@BeanProperty var replyTimeout: Duration = 1 minute
|
||||
|
||||
/**
|
||||
* TODO fix it
|
||||
* Whether to auto-acknowledge one-way message exchanges with (untyped) actors. This is
|
||||
* set via the <code>blocking=true|false</code> endpoint URI parameter. Default value is
|
||||
* <code>true</code>. When set to <code>true</code> consumer actors need to additionally
|
||||
* call <code>Consumer.ack</code> within <code>Actor.receive</code>.
|
||||
*/
|
||||
@BeanProperty var autoack: Boolean = true
|
||||
}
|
||||
|
||||
//FIXME: rewrite this doc
|
||||
/**
|
||||
* Sends the in-message of an exchange to an (untyped) actor, identified by an
|
||||
* actor endpoint URI or by a <code>CamelActorIdentifier</code> message header.
|
||||
* <ul>
|
||||
* <li>If the exchange pattern is out-capable and <code>blocking</code> is set to
|
||||
* <code>true</code> then the producer waits for a reply, using the !! operator.</li>
|
||||
* <li>If the exchange pattern is out-capable and <code>blocking</code> is set to
|
||||
* <code>false</code> then the producer sends the message using the ! operator, together
|
||||
* with a callback handler. The callback handler is an <code>ActorRef</code> that can be
|
||||
* used by the receiving actor to asynchronously reply to the route that is sending the
|
||||
* message.</li>
|
||||
* <li>If the exchange pattern is in-only then the producer sends the message using the
|
||||
* ! operator.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @see akka.camel.component.ActorComponent
|
||||
* @see akka.camel.component.ActorEndpoint
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
class ActorProducer(val ep: ActorEndpoint, camel: Camel) extends DefaultProducer(ep) with AsyncProcessor {
|
||||
def process(exchange: Exchange) { new ConsumerAsyncProcessor(ep, camel).process(new CamelExchangeAdapter(exchange)) }
|
||||
def process(exchange: Exchange, callback: AsyncCallback) = new ConsumerAsyncProcessor(ep, camel).process(new CamelExchangeAdapter(exchange), callback)
|
||||
}
|
||||
|
||||
class ConsumerAsyncProcessor(config: ActorEndpointConfig, camel: Camel) {
|
||||
|
||||
def process(exchange: CamelExchangeAdapter) {
|
||||
val isDone = new CountDownLatch(1)
|
||||
process(exchange, new AsyncCallback { def done(doneSync: Boolean) { isDone.countDown() } })
|
||||
isDone.await() // this should never wait forever as the process(exchange, callback) method guarantees that.
|
||||
}
|
||||
|
||||
def process(exchange: CamelExchangeAdapter, callback: AsyncCallback): Boolean = {
|
||||
|
||||
// this notify methods are just a syntax sugar
|
||||
def notifyDoneSynchronously[A](a: A = null) = callback.done(true)
|
||||
def notifyDoneAsynchronously[A](a: A = null) = callback.done(false)
|
||||
|
||||
def message = messageFor(exchange)
|
||||
|
||||
if (exchange.isOutCapable) { //InOut
|
||||
sendAsync(message, onComplete = forwardResponseTo(exchange) andThen notifyDoneAsynchronously)
|
||||
} else { // inOnly
|
||||
if (config.autoack) { //autoAck
|
||||
fireAndForget(message, exchange)
|
||||
notifyDoneSynchronously()
|
||||
true // done sync
|
||||
} else { //manualAck
|
||||
sendAsync(message, onComplete = forwardAckTo(exchange) andThen notifyDoneAsynchronously)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private def sendAsync(message: Message, onComplete: PartialFunction[Either[Throwable, Any], Unit]): Boolean = {
|
||||
try {
|
||||
val actor = actorFor(config.path)
|
||||
val future = actor.ask(message)(new Timeout(config.replyTimeout))
|
||||
future.onComplete(onComplete)
|
||||
} catch {
|
||||
case e ⇒ onComplete(Left(e))
|
||||
}
|
||||
false // Done async
|
||||
}
|
||||
|
||||
private def fireAndForget(message: Message, exchange: CamelExchangeAdapter) {
|
||||
try {
|
||||
actorFor(config.path) ! message
|
||||
} catch {
|
||||
case e ⇒ exchange.setFailure(new CamelFailure(e))
|
||||
}
|
||||
}
|
||||
|
||||
private[this] def forwardResponseTo(exchange: CamelExchangeAdapter): PartialFunction[Either[Throwable, Any], Unit] = {
|
||||
case Right(failure: CamelFailure) ⇒ exchange.setFailure(failure);
|
||||
case Right(msg) ⇒ exchange.setResponse(Message.canonicalize(msg))
|
||||
case Left(e: TimeoutException) ⇒ exchange.setFailure(CamelFailure(new TimeoutException("Failed to get response from the actor [%s] within timeout [%s]. Check replyTimeout and blocking settings [%s]" format (config.path, config.replyTimeout, config))))
|
||||
case Left(throwable) ⇒ exchange.setFailure(CamelFailure(throwable))
|
||||
}
|
||||
|
||||
def forwardAckTo(exchange: CamelExchangeAdapter): PartialFunction[Either[Throwable, Any], Unit] = {
|
||||
case Right(Ack) ⇒ { /* no response message to set */ }
|
||||
case Right(failure: CamelFailure) ⇒ exchange.setFailure(failure)
|
||||
case Right(msg) ⇒ exchange.setFailure(CamelFailure(new IllegalArgumentException("Expected Ack or Failure message, but got: [%s] from actor [%s]" format (msg, config.path))))
|
||||
case Left(e: TimeoutException) ⇒ exchange.setFailure(CamelFailure(new TimeoutException("Failed to get Ack or Failure response from the actor [%s] within timeout [%s]. Check replyTimeout and blocking settings [%s]" format (config.path, config.replyTimeout, config))))
|
||||
case Left(throwable) ⇒ exchange.setFailure(CamelFailure(throwable))
|
||||
}
|
||||
|
||||
private[this] def actorFor(path: ActorEndpointPath): ActorRef =
|
||||
path.findActorIn(camel.system) getOrElse (throw new ActorNotRegisteredException(path.actorPath))
|
||||
|
||||
private[this] def messageFor(exchange: CamelExchangeAdapter) =
|
||||
exchange.toRequestMessage(Map(Message.MessageExchangeId -> exchange.getExchangeId))
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Thrown to indicate that an actor referenced by an endpoint URI cannot be
|
||||
* found in the Actor.registry.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
class ActorNotRegisteredException(uri: String) extends RuntimeException {
|
||||
override def getMessage = "Actor [%s] doesn't exist" format uri
|
||||
}
|
||||
|
||||
private[camel] object DurationTypeConverter extends CamelTypeConverter {
|
||||
def convertTo[T](`type`: Class[T], value: AnyRef) = {
|
||||
require(value.toString.endsWith(" nanos"))
|
||||
Duration.fromNanos(value.toString.dropRight(6).toLong).asInstanceOf[T]
|
||||
}
|
||||
|
||||
def toString(duration: Duration) = duration.toNanos + " nanos"
|
||||
}
|
||||
|
||||
private[camel] abstract class CamelTypeConverter extends TypeConverter {
|
||||
def convertTo[T](`type`: Class[T], exchange: Exchange, value: AnyRef) = convertTo(`type`, value)
|
||||
def mandatoryConvertTo[T](`type`: Class[T], value: AnyRef) = convertTo(`type`, value)
|
||||
def mandatoryConvertTo[T](`type`: Class[T], exchange: Exchange, value: AnyRef) = convertTo(`type`, value)
|
||||
}
|
||||
|
||||
private[camel] case class ActorEndpointPath private (actorPath: String) {
|
||||
require(actorPath != null)
|
||||
require(actorPath.length() > 0)
|
||||
def toCamelPath(config: ConsumerConfig = new ConsumerConfig {}): String = "actor://path:%s?%s" format (actorPath, config.toCamelParameters)
|
||||
|
||||
def findActorIn(system: ActorSystem): Option[ActorRef] = {
|
||||
val ref = system.actorFor(actorPath)
|
||||
if (ref.isTerminated) None else Some(ref)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private[camel] object ActorEndpointPath {
|
||||
def apply(actorRef: ActorRef) = new ActorEndpointPath(actorRef.path.toString)
|
||||
|
||||
/**
|
||||
* Expects path in a format: path:%s
|
||||
*/
|
||||
def fromCamelPath(camelPath: String) = camelPath match {
|
||||
case id if id startsWith "path:" ⇒ new ActorEndpointPath(id substring 5)
|
||||
case _ ⇒ throw new IllegalArgumentException("Invalid path: [%s] - should be path:<actorPath>" format camelPath)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel.javaapi
|
||||
|
||||
import akka.actor.UntypedActor
|
||||
import akka.camel._
|
||||
|
||||
/**
|
||||
* Java-friendly Consumer.
|
||||
*
|
||||
* @see UntypedConsumerActor
|
||||
* @see RemoteUntypedConsumerActor
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
trait UntypedConsumer extends Consumer { self: UntypedActor ⇒
|
||||
final def endpointUri = getEndpointUri
|
||||
|
||||
/**
|
||||
* Returns the Camel endpoint URI to consume messages from.
|
||||
*/
|
||||
def getEndpointUri(): String
|
||||
|
||||
def rich(message: Message): RichMessage = message
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclass this abstract class to create an MDB-style untyped consumer actor. This
|
||||
* class is meant to be used from Java.
|
||||
*/
|
||||
abstract class UntypedConsumerActor extends UntypedActor with UntypedConsumer
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel.javaapi
|
||||
|
||||
import akka.actor.UntypedActor
|
||||
import akka.camel._
|
||||
import org.apache.camel.{ CamelContext, ProducerTemplate }
|
||||
|
||||
/**
|
||||
* Subclass this abstract class to create an untyped producer actor. This class is meant to be used from Java.
|
||||
*
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
abstract class UntypedProducerActor extends UntypedActor with ProducerSupport {
|
||||
/**
|
||||
* Called before the message is sent to the endpoint specified by <code>getEndpointUri</code>. The original
|
||||
* message is passed as argument. By default, this method simply returns the argument but may be overridden
|
||||
* by subclasses.
|
||||
*/
|
||||
def onReceiveBeforeProduce(message: Any): Any = super.receiveBeforeProduce(message)
|
||||
|
||||
/**
|
||||
* Called after a response was received from the endpoint specified by <code>endpointUri</code>. The
|
||||
* response is passed as argument. By default, this method sends the response back to the original sender
|
||||
* if <code>oneway</code> is <code>false</code>. If <code>oneway</code> is <code>true</code>, nothing is
|
||||
* done. This method may be overridden by subclasses (e.g. to forward responses to another actor).
|
||||
*/
|
||||
def onReceiveAfterProduce(message: Any): Unit = super.receiveAfterProduce(message)
|
||||
|
||||
final override def receiveBeforeProduce = {
|
||||
case msg ⇒ onReceiveBeforeProduce(msg)
|
||||
}
|
||||
|
||||
final override def receiveAfterProduce = {
|
||||
case msg ⇒ onReceiveAfterProduce(msg)
|
||||
}
|
||||
|
||||
final override def endpointUri = getEndpointUri
|
||||
|
||||
final override def oneway = isOneway
|
||||
|
||||
/**
|
||||
* Default implementation of UntypedActor.onReceive
|
||||
*/
|
||||
def onReceive(message: Any) {
|
||||
produce(message)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Camel endpoint URI to produce messages to.
|
||||
*/
|
||||
def getEndpointUri(): String
|
||||
|
||||
/**
|
||||
* If set to false (default), this producer expects a response message from the Camel endpoint.
|
||||
* If set to true, this producer communicates with the Camel endpoint with an in-only message
|
||||
* exchange pattern (fire and forget).
|
||||
*/
|
||||
def isOneway() = super.oneway
|
||||
|
||||
/**
|
||||
* Creates a <code>RichMessage</code> out of a message. The <code>RichMessage</code> has convenience methods for accessing body and headers
|
||||
* @param message the message
|
||||
* @return the <code>RichMessage</code>
|
||||
*/
|
||||
def rich(message: Message): RichMessage = message
|
||||
|
||||
/**
|
||||
* Returns the <code>CamelContext</code>.
|
||||
*/
|
||||
def getCamelContext(): CamelContext = camel.context
|
||||
|
||||
/**
|
||||
* Returns the <code>ProducerTemplate</code>.
|
||||
*/
|
||||
def getProducerTemplate(): ProducerTemplate = camel.template
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camelexamples
|
||||
|
||||
import akka.camel._
|
||||
import akka.util.duration._
|
||||
import akka.actor.{ Actor, OneForOneStrategy }
|
||||
import akka.actor.SupervisorStrategy._
|
||||
|
||||
object ExamplesSupport {
|
||||
val retry3xWithin1s = OneForOneStrategy(maxNrOfRetries = 3, withinTimeRange = 1 second) {
|
||||
case _: Exception ⇒ Restart
|
||||
}
|
||||
}
|
||||
|
||||
class SysOutConsumer extends Consumer {
|
||||
override def activationTimeout = 10 seconds
|
||||
def endpointUri = "file://data/input/CamelConsumer"
|
||||
|
||||
protected def receive = {
|
||||
case msg: Message ⇒ {
|
||||
printf("Received '%s'\n", msg.bodyAs[String])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TroubleMaker extends Consumer {
|
||||
def endpointUri = "WRONG URI"
|
||||
|
||||
println("Trying to instantiate conumer with uri: " + endpointUri)
|
||||
protected def receive = { case _ ⇒ }
|
||||
}
|
||||
|
||||
class SysOutActor(implicit camel: Camel) extends Actor {
|
||||
protected def receive = {
|
||||
case msg: Message ⇒ {
|
||||
printf("Received '%s'\n", msg.bodyAs[String])
|
||||
}
|
||||
}
|
||||
}
|
||||
1
akka-camel/src/main/scala/akka/camelexamples/README.txt
Normal file
1
akka-camel/src/main/scala/akka/camelexamples/README.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
This package is outside of akka.camel because we don't want to use private[camel] features in examples.
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camelexamples
|
||||
|
||||
import java.io.FileWriter
|
||||
|
||||
object RichString {
|
||||
implicit def toRichString(s: String): RichString = new RichString(s)
|
||||
}
|
||||
|
||||
class RichString(s: String) {
|
||||
def saveAs(fileName: String) = write(fileName, s)
|
||||
def >>(fileName: String) = this.saveAs(fileName)
|
||||
def <<(content: String) = write(s, content)
|
||||
|
||||
private[this] def write(fileName: String, content: String) {
|
||||
val f = new FileWriter(fileName)
|
||||
f.write(content)
|
||||
f.close()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camelexamples
|
||||
|
||||
import akka.actor.{ Props, ActorSystem }
|
||||
import RichString._
|
||||
|
||||
object _1_SimpleConsumer extends App {
|
||||
val system = ActorSystem("test")
|
||||
|
||||
system.actorOf(Props[SysOutConsumer])
|
||||
|
||||
"data/input/CamelConsumer/file1.txt" << "test data " + math.random
|
||||
|
||||
Thread.sleep(2000)
|
||||
|
||||
system.shutdown()
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camelexamples
|
||||
|
||||
import akka.actor.{ PoisonPill, Terminated, Props, ActorSystem, Actor }
|
||||
import ExamplesSupport._
|
||||
import RichString._
|
||||
|
||||
object SupervisedConsumersExample extends App {
|
||||
|
||||
val system = ActorSystem("test1")
|
||||
|
||||
system.actorOf(Props(new Actor {
|
||||
context.watch(context.actorOf(Props[EndpointManager]))
|
||||
protected def receive = {
|
||||
case Terminated(ref) ⇒ system.shutdown()
|
||||
}
|
||||
}))
|
||||
|
||||
"data/input/CamelConsumer/file1.txt" << "test data " + math.random
|
||||
}
|
||||
|
||||
class EndpointManager extends Actor {
|
||||
import context._
|
||||
|
||||
override def supervisorStrategy() = retry3xWithin1s
|
||||
|
||||
watch(actorOf(Props[SysOutConsumer]))
|
||||
watch(actorOf(Props[TroubleMaker]))
|
||||
|
||||
protected def receive = {
|
||||
case Terminated(ref) ⇒ {
|
||||
printf("Hey! One of the endpoints has died: %s. I am doing sepuku...\n", ref)
|
||||
self ! PoisonPill
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camelexamples
|
||||
|
||||
import org.apache.camel.builder.RouteBuilder
|
||||
import akka.actor.{ Props, ActorSystem }
|
||||
import akka.camel._
|
||||
import RichString._
|
||||
|
||||
object _3_SimpleActorEndpoint extends App {
|
||||
|
||||
val system = ActorSystem("test")
|
||||
val camel = CamelExtension(system)
|
||||
|
||||
val actor = system.actorOf(Props[SysOutActor])
|
||||
|
||||
camel.context.addRoutes(new RouteBuilder() {
|
||||
def configure() {
|
||||
from("file://data/input/CamelConsumer").to(actor)
|
||||
}
|
||||
})
|
||||
|
||||
"data/input/CamelConsumer/file1.txt" << "test data " + math.random
|
||||
|
||||
Thread.sleep(3000)
|
||||
|
||||
system.shutdown()
|
||||
|
||||
}
|
||||
12
akka-camel/src/main/scala/akka/package.scala
Normal file
12
akka-camel/src/main/scala/akka/package.scala
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka
|
||||
|
||||
import org.apache.camel.model.ProcessorDefinition
|
||||
|
||||
package object camel {
|
||||
implicit def toActorRouteDefinition(definition: ProcessorDefinition[_]) = new ActorRouteDefinition(definition)
|
||||
implicit def messageToRichMessage(m: Message)(implicit camel: Camel): RichMessage = new RichMessage(m, camel.context)
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@ object CamelTestSupport {
|
|||
|
||||
trait TestActor extends Actor {
|
||||
def receive = {
|
||||
case msg ⇒ {
|
||||
case msg => {
|
||||
handler(msg)
|
||||
}
|
||||
}
|
||||
|
|
@ -28,51 +28,52 @@ object CamelTestSupport {
|
|||
}
|
||||
|
||||
class Tester3 extends TestActor with Noop {
|
||||
self.timeout = 1
|
||||
def handler = noop
|
||||
}
|
||||
|
||||
trait Countdown { this: Actor ⇒
|
||||
trait Countdown { this: Actor =>
|
||||
var latch: CountDownLatch = new CountDownLatch(0)
|
||||
def countdown: Handler = {
|
||||
case SetExpectedMessageCount(num) ⇒ {
|
||||
case SetExpectedMessageCount(num) => {
|
||||
latch = new CountDownLatch(num)
|
||||
sender ! latch
|
||||
self.reply(latch)
|
||||
}
|
||||
case msg ⇒ latch.countDown
|
||||
case msg => latch.countDown
|
||||
}
|
||||
}
|
||||
|
||||
trait Respond { this: Actor ⇒
|
||||
trait Respond { this: Actor =>
|
||||
def respond: Handler = {
|
||||
case msg: Message ⇒ sender ! response(msg)
|
||||
case msg: Message => self.reply(response(msg))
|
||||
}
|
||||
|
||||
def response(msg: Message): Any = "Hello %s" format msg.body
|
||||
}
|
||||
|
||||
trait Retain { this: Actor ⇒
|
||||
trait Retain { this: Actor =>
|
||||
val messages = Buffer[Any]()
|
||||
|
||||
def retain: Handler = {
|
||||
case GetRetainedMessage ⇒ sender ! messages.last
|
||||
case GetRetainedMessages(p) ⇒ sender ! messages.filter(p).toList
|
||||
case msg ⇒ {
|
||||
case GetRetainedMessage => self.reply(messages.last)
|
||||
case GetRetainedMessages(p) => self.reply(messages.toList.filter(p))
|
||||
case msg => {
|
||||
messages += msg
|
||||
msg
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
trait Noop { this: Actor ⇒
|
||||
trait Noop { this: Actor =>
|
||||
def noop: Handler = {
|
||||
case msg ⇒ msg
|
||||
case msg => msg
|
||||
}
|
||||
}
|
||||
|
||||
case class SetExpectedMessageCount(num: Int)
|
||||
case class GetRetainedMessage()
|
||||
case class GetRetainedMessages(p: Any ⇒ Boolean) {
|
||||
def this() = this(_ ⇒ true)
|
||||
case class GetRetainedMessages(p: Any => Boolean) {
|
||||
def this() = this(_ => true)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,22 +1,22 @@
|
|||
package akka.camel.component
|
||||
|
||||
import java.util.concurrent.{ TimeUnit, CountDownLatch }
|
||||
import java.util.concurrent.{TimeUnit, CountDownLatch}
|
||||
|
||||
import org.apache.camel.RuntimeCamelException
|
||||
import org.apache.camel.builder.RouteBuilder
|
||||
import org.apache.camel.component.mock.MockEndpoint
|
||||
import org.scalatest.{ BeforeAndAfterEach, BeforeAndAfterAll, FeatureSpec }
|
||||
import org.scalatest.{BeforeAndAfterEach, BeforeAndAfterAll, FeatureSpec}
|
||||
|
||||
import akka.actor.{ Actor, Props, Timeout }
|
||||
import akka.actor.Actor
|
||||
import akka.actor.Actor._
|
||||
import akka.camel.{ Failure, Message, CamelContextManager }
|
||||
import akka.camel.{Failure, Message, CamelContextManager}
|
||||
import akka.camel.CamelTestSupport._
|
||||
|
||||
class ActorComponentFeatureTest extends FeatureSpec with BeforeAndAfterAll with BeforeAndAfterEach {
|
||||
import ActorComponentFeatureTest._
|
||||
|
||||
override protected def beforeAll = {
|
||||
Actor.registry.local.shutdownAll
|
||||
Actor.registry.shutdownAll
|
||||
CamelContextManager.init
|
||||
CamelContextManager.mandatoryContext.addRoutes(new TestRoute)
|
||||
CamelContextManager.start
|
||||
|
|
@ -25,7 +25,7 @@ class ActorComponentFeatureTest extends FeatureSpec with BeforeAndAfterAll with
|
|||
override protected def afterAll = CamelContextManager.stop
|
||||
|
||||
override protected def afterEach = {
|
||||
Actor.registry.local.shutdownAll
|
||||
Actor.registry.shutdownAll
|
||||
mockEndpoint.reset
|
||||
}
|
||||
|
||||
|
|
@ -33,21 +33,21 @@ class ActorComponentFeatureTest extends FeatureSpec with BeforeAndAfterAll with
|
|||
import CamelContextManager.mandatoryTemplate
|
||||
|
||||
scenario("one-way communication") {
|
||||
val actor = actorOf(Props[Tester1]
|
||||
val latch = (actor ? SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
val actor = actorOf[Tester1].start
|
||||
val latch = (actor !! SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
mandatoryTemplate.sendBody("actor:uuid:%s" format actor.uuid, "Martin")
|
||||
assert(latch.await(5000, TimeUnit.MILLISECONDS))
|
||||
val reply = (actor ? GetRetainedMessage).get.asInstanceOf[Message]
|
||||
val reply = (actor !! GetRetainedMessage).get.asInstanceOf[Message]
|
||||
assert(reply.body === "Martin")
|
||||
}
|
||||
|
||||
scenario("two-way communication") {
|
||||
val actor = actorOf(Props[Tester2]
|
||||
val actor = actorOf[Tester2].start
|
||||
assert(mandatoryTemplate.requestBody("actor:uuid:%s" format actor.uuid, "Martin") === "Hello Martin")
|
||||
}
|
||||
|
||||
scenario("two-way communication with timeout") {
|
||||
val actor = actorOf(Props[Tester3].withTimeout(Timeout(1)))
|
||||
val actor = actorOf[Tester3].start
|
||||
intercept[RuntimeCamelException] {
|
||||
mandatoryTemplate.requestBody("actor:uuid:%s?blocking=true" format actor.uuid, "Martin")
|
||||
}
|
||||
|
|
@ -70,21 +70,21 @@ class ActorComponentFeatureTest extends FeatureSpec with BeforeAndAfterAll with
|
|||
import CamelContextManager.mandatoryTemplate
|
||||
|
||||
scenario("one-way communication") {
|
||||
val actor = actorOf(Props[Tester1]
|
||||
val latch = (actor ? SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
mandatoryTemplate.sendBody("actor:%s" format actor.address, "Martin")
|
||||
val actor = actorOf[Tester1].start
|
||||
val latch = (actor !! SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
mandatoryTemplate.sendBody("actor:%s" format actor.id, "Martin")
|
||||
assert(latch.await(5000, TimeUnit.MILLISECONDS))
|
||||
val reply = (actor ? GetRetainedMessage).get.asInstanceOf[Message]
|
||||
val reply = (actor !! GetRetainedMessage).get.asInstanceOf[Message]
|
||||
assert(reply.body === "Martin")
|
||||
}
|
||||
|
||||
scenario("two-way communication") {
|
||||
val actor = actorOf(Props[Tester2]
|
||||
assert(mandatoryTemplate.requestBody("actor:%s" format actor.address, "Martin") === "Hello Martin")
|
||||
val actor = actorOf[Tester2].start
|
||||
assert(mandatoryTemplate.requestBody("actor:%s" format actor.id, "Martin") === "Hello Martin")
|
||||
}
|
||||
|
||||
scenario("two-way communication via a custom route") {
|
||||
val actor = actorOf(Props[CustomIdActor]("custom-id")
|
||||
val actor = actorOf[CustomIdActor].start
|
||||
assert(mandatoryTemplate.requestBody("direct:custom-id-test-1", "Martin") === "Received Martin")
|
||||
assert(mandatoryTemplate.requestBody("direct:custom-id-test-2", "Martin") === "Received Martin")
|
||||
}
|
||||
|
|
@ -95,26 +95,27 @@ class ActorComponentFeatureTest extends FeatureSpec with BeforeAndAfterAll with
|
|||
|
||||
object ActorComponentFeatureTest {
|
||||
class CustomIdActor extends Actor {
|
||||
self.id = "custom-id"
|
||||
protected def receive = {
|
||||
case msg: Message ⇒ sender ! ("Received %s" format msg.body)
|
||||
case msg: Message => self.reply("Received %s" format msg.body)
|
||||
}
|
||||
}
|
||||
|
||||
class FailWithMessage extends Actor {
|
||||
protected def receive = {
|
||||
case msg: Message ⇒ sender ! Failure(new Exception("test"))
|
||||
case msg: Message => self.reply(Failure(new Exception("test")))
|
||||
}
|
||||
}
|
||||
|
||||
class FailWithException extends Actor {
|
||||
protected def receive = {
|
||||
case msg: Message ⇒ throw new Exception("test")
|
||||
case msg: Message => throw new Exception("test")
|
||||
}
|
||||
}
|
||||
|
||||
class TestRoute extends RouteBuilder {
|
||||
val failWithMessage = actorOf(Props[FailWithMessage]
|
||||
val failWithException = actorOf(Props[FailWithException]
|
||||
val failWithMessage = actorOf[FailWithMessage].start
|
||||
val failWithException = actorOf[FailWithException].start
|
||||
def configure {
|
||||
from("direct:custom-id-test-1").to("actor:custom-id")
|
||||
from("direct:custom-id-test-2").to("actor:id:custom-id")
|
||||
|
|
@ -2,60 +2,55 @@ package akka.camel.component
|
|||
|
||||
import ActorComponentTest._
|
||||
|
||||
import java.util.concurrent.{ CountDownLatch, TimeoutException, TimeUnit }
|
||||
import java.util.concurrent.{CountDownLatch, TimeoutException, TimeUnit}
|
||||
|
||||
import org.apache.camel.{ AsyncCallback, ExchangePattern }
|
||||
import org.apache.camel.{AsyncCallback, ExchangePattern}
|
||||
|
||||
import org.junit.{ After, Test }
|
||||
import org.junit.{After, Test}
|
||||
import org.scalatest.junit.JUnitSuite
|
||||
import org.scalatest.BeforeAndAfterAll
|
||||
|
||||
import akka.actor.{ Props, Timeout }
|
||||
import akka.actor.Actor._
|
||||
import akka.camel.{ Failure, Message }
|
||||
import akka.camel.{Failure, Message}
|
||||
import akka.camel.CamelTestSupport._
|
||||
|
||||
class ActorProducerTest extends JUnitSuite with BeforeAndAfterAll {
|
||||
import ActorProducerTest._
|
||||
|
||||
@After
|
||||
def tearDown = registry.local.shutdownAll
|
||||
@After def tearDown = registry.shutdownAll
|
||||
|
||||
@Test
|
||||
def shouldSendMessageToActorWithSyncProcessor = {
|
||||
val actor = actorOf(Props[Tester1]
|
||||
val latch = (actor ? SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
@Test def shouldSendMessageToActorWithSyncProcessor = {
|
||||
val actor = actorOf[Tester1].start
|
||||
val latch = (actor !! SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
val endpoint = actorEndpoint("actor:uuid:%s" format actor.uuid)
|
||||
val exchange = endpoint.createExchange(ExchangePattern.InOnly)
|
||||
exchange.getIn.setBody("Martin")
|
||||
exchange.getIn.setHeader("k1", "v1")
|
||||
actorProducer(endpoint).process(exchange)
|
||||
assert(latch.await(5000, TimeUnit.MILLISECONDS))
|
||||
val reply = (actor ? GetRetainedMessage).get.asInstanceOf[Message]
|
||||
val reply = (actor !! GetRetainedMessage).get.asInstanceOf[Message]
|
||||
assert(reply.body === "Martin")
|
||||
assert(reply.headers === Map(Message.MessageExchangeId -> exchange.getExchangeId, "k1" -> "v1"))
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldSendMessageToActorWithAsyncProcessor = {
|
||||
val actor = actorOf(Props[Tester1]
|
||||
val latch = (actor ? SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
@Test def shouldSendMessageToActorWithAsyncProcessor = {
|
||||
val actor = actorOf[Tester1].start
|
||||
val latch = (actor !! SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
val endpoint = actorEndpoint("actor:uuid:%s" format actor.uuid)
|
||||
val exchange = endpoint.createExchange(ExchangePattern.InOnly)
|
||||
exchange.getIn.setBody("Martin")
|
||||
exchange.getIn.setHeader("k1", "v1")
|
||||
actorAsyncProducer(endpoint).process(exchange, expectSyncCompletion)
|
||||
assert(latch.await(5000, TimeUnit.MILLISECONDS))
|
||||
val reply = (actor ? GetRetainedMessage).get.asInstanceOf[Message]
|
||||
val reply = (actor !! GetRetainedMessage).get.asInstanceOf[Message]
|
||||
assert(reply.body === "Martin")
|
||||
assert(reply.headers === Map(Message.MessageExchangeId -> exchange.getExchangeId, "k1" -> "v1"))
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldSendMessageToActorAndReceiveResponseWithSyncProcessor = {
|
||||
val actor = actorOf(Props(new Tester2 {
|
||||
@Test def shouldSendMessageToActorAndReceiveResponseWithSyncProcessor = {
|
||||
val actor = actorOf(new Tester2 {
|
||||
override def response(msg: Message) = Message(super.response(msg), Map("k2" -> "v2"))
|
||||
})
|
||||
}).start
|
||||
val endpoint = actorEndpoint("actor:uuid:%s" format actor.uuid)
|
||||
val exchange = endpoint.createExchange(ExchangePattern.InOut)
|
||||
exchange.getIn.setBody("Martin")
|
||||
|
|
@ -65,11 +60,10 @@ class ActorProducerTest extends JUnitSuite with BeforeAndAfterAll {
|
|||
assert(exchange.getOut.getHeader("k2") === "v2")
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldSendMessageToActorAndReceiveResponseWithAsyncProcessor = {
|
||||
val actor = actorOf(Props(new Tester2 {
|
||||
@Test def shouldSendMessageToActorAndReceiveResponseWithAsyncProcessor = {
|
||||
val actor = actorOf(new Tester2 {
|
||||
override def response(msg: Message) = Message(super.response(msg), Map("k2" -> "v2"))
|
||||
})
|
||||
}).start
|
||||
val completion = expectAsyncCompletion
|
||||
val endpoint = actorEndpoint("actor:uuid:%s" format actor.uuid)
|
||||
val exchange = endpoint.createExchange(ExchangePattern.InOut)
|
||||
|
|
@ -81,11 +75,10 @@ class ActorProducerTest extends JUnitSuite with BeforeAndAfterAll {
|
|||
assert(exchange.getOut.getHeader("k2") === "v2")
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldSendMessageToActorAndReceiveFailureWithAsyncProcessor = {
|
||||
val actor = actorOf(Props(new Tester2 {
|
||||
@Test def shouldSendMessageToActorAndReceiveFailureWithAsyncProcessor = {
|
||||
val actor = actorOf(new Tester2 {
|
||||
override def response(msg: Message) = Failure(new Exception("testmsg"), Map("k3" -> "v3"))
|
||||
})
|
||||
}).start
|
||||
val completion = expectAsyncCompletion
|
||||
val endpoint = actorEndpoint("actor:uuid:%s" format actor.uuid)
|
||||
val exchange = endpoint.createExchange(ExchangePattern.InOut)
|
||||
|
|
@ -98,11 +91,10 @@ class ActorProducerTest extends JUnitSuite with BeforeAndAfterAll {
|
|||
assert(exchange.getOut.getHeader("k3") === null) // headers from failure message are currently ignored
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldSendMessageToActorAndReceiveAckWithAsyncProcessor = {
|
||||
val actor = actorOf(Props(new Tester2 {
|
||||
@Test def shouldSendMessageToActorAndReceiveAckWithAsyncProcessor = {
|
||||
val actor = actorOf(new Tester2 {
|
||||
override def response(msg: Message) = akka.camel.Ack
|
||||
})
|
||||
}).start
|
||||
val completion = expectAsyncCompletion
|
||||
val endpoint = actorEndpoint("actor:uuid:%s?autoack=false" format actor.uuid)
|
||||
val exchange = endpoint.createExchange(ExchangePattern.InOnly)
|
||||
|
|
@ -113,61 +105,62 @@ class ActorProducerTest extends JUnitSuite with BeforeAndAfterAll {
|
|||
assert(exchange.getOut.getBody === null)
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldDynamicallyRouteMessageToActorWithDefaultId = {
|
||||
val actor1 = actorOf(Props[Tester1]("x")
|
||||
val actor2 = actorOf(Props[Tester1]("y")
|
||||
actor1
|
||||
actor2
|
||||
val latch1 = (actor1 ? SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
val latch2 = (actor2 ? SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
val endpoint = actorEndpoint("actor:id:%s" format actor1.address)
|
||||
@Test def shouldDynamicallyRouteMessageToActorWithDefaultId = {
|
||||
val actor1 = actorOf[Tester1]
|
||||
val actor2 = actorOf[Tester1]
|
||||
actor1.id = "x"
|
||||
actor2.id = "y"
|
||||
actor1.start
|
||||
actor2.start
|
||||
val latch1 = (actor1 !! SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
val latch2 = (actor2 !! SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
val endpoint = actorEndpoint("actor:id:%s" format actor1.id)
|
||||
val exchange1 = endpoint.createExchange(ExchangePattern.InOnly)
|
||||
val exchange2 = endpoint.createExchange(ExchangePattern.InOnly)
|
||||
exchange1.getIn.setBody("Test1")
|
||||
exchange2.getIn.setBody("Test2")
|
||||
exchange2.getIn.setHeader(ActorComponent.ActorIdentifier, actor2.address)
|
||||
exchange2.getIn.setHeader(ActorComponent.ActorIdentifier, actor2.id)
|
||||
actorProducer(endpoint).process(exchange1)
|
||||
actorProducer(endpoint).process(exchange2)
|
||||
assert(latch1.await(5, TimeUnit.SECONDS))
|
||||
assert(latch2.await(5, TimeUnit.SECONDS))
|
||||
val reply1 = (actor1 ? GetRetainedMessage).get.asInstanceOf[Message]
|
||||
val reply2 = (actor2 ? GetRetainedMessage).get.asInstanceOf[Message]
|
||||
val reply1 = (actor1 !! GetRetainedMessage).get.asInstanceOf[Message]
|
||||
val reply2 = (actor2 !! GetRetainedMessage).get.asInstanceOf[Message]
|
||||
assert(reply1.body === "Test1")
|
||||
assert(reply2.body === "Test2")
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldDynamicallyRouteMessageToActorWithoutDefaultId = {
|
||||
val actor1 = actorOf(Props[Tester1]("x")
|
||||
val actor2 = actorOf(Props[Tester1]("y")
|
||||
actor1
|
||||
actor2
|
||||
val latch1 = (actor1 ? SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
val latch2 = (actor2 ? SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
@Test def shouldDynamicallyRouteMessageToActorWithoutDefaultId = {
|
||||
val actor1 = actorOf[Tester1]
|
||||
val actor2 = actorOf[Tester1]
|
||||
actor1.id = "x"
|
||||
actor2.id = "y"
|
||||
actor1.start
|
||||
actor2.start
|
||||
val latch1 = (actor1 !! SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
val latch2 = (actor2 !! SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
val endpoint = actorEndpoint("actor:id:")
|
||||
val exchange1 = endpoint.createExchange(ExchangePattern.InOnly)
|
||||
val exchange2 = endpoint.createExchange(ExchangePattern.InOnly)
|
||||
exchange1.getIn.setBody("Test1")
|
||||
exchange2.getIn.setBody("Test2")
|
||||
exchange1.getIn.setHeader(ActorComponent.ActorIdentifier, actor1.address)
|
||||
exchange2.getIn.setHeader(ActorComponent.ActorIdentifier, actor2.address)
|
||||
exchange1.getIn.setHeader(ActorComponent.ActorIdentifier, actor1.id)
|
||||
exchange2.getIn.setHeader(ActorComponent.ActorIdentifier, actor2.id)
|
||||
actorProducer(endpoint).process(exchange1)
|
||||
actorProducer(endpoint).process(exchange2)
|
||||
assert(latch1.await(5, TimeUnit.SECONDS))
|
||||
assert(latch2.await(5, TimeUnit.SECONDS))
|
||||
val reply1 = (actor1 ? GetRetainedMessage).get.asInstanceOf[Message]
|
||||
val reply2 = (actor2 ? GetRetainedMessage).get.asInstanceOf[Message]
|
||||
val reply1 = (actor1 !! GetRetainedMessage).get.asInstanceOf[Message]
|
||||
val reply2 = (actor2 !! GetRetainedMessage).get.asInstanceOf[Message]
|
||||
assert(reply1.body === "Test1")
|
||||
assert(reply2.body === "Test2")
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldDynamicallyRouteMessageToActorWithDefaultUuid = {
|
||||
val actor1 = actorOf(Props[Tester1]
|
||||
val actor2 = actorOf(Props[Tester1]
|
||||
val latch1 = (actor1 ? SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
val latch2 = (actor2 ? SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
@Test def shouldDynamicallyRouteMessageToActorWithDefaultUuid = {
|
||||
val actor1 = actorOf[Tester1].start
|
||||
val actor2 = actorOf[Tester1].start
|
||||
val latch1 = (actor1 !! SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
val latch2 = (actor2 !! SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
val endpoint = actorEndpoint("actor:uuid:%s" format actor1.uuid)
|
||||
val exchange1 = endpoint.createExchange(ExchangePattern.InOnly)
|
||||
val exchange2 = endpoint.createExchange(ExchangePattern.InOnly)
|
||||
|
|
@ -178,18 +171,17 @@ class ActorProducerTest extends JUnitSuite with BeforeAndAfterAll {
|
|||
actorProducer(endpoint).process(exchange2)
|
||||
assert(latch1.await(5, TimeUnit.SECONDS))
|
||||
assert(latch2.await(5, TimeUnit.SECONDS))
|
||||
val reply1 = (actor1 ? GetRetainedMessage).get.asInstanceOf[Message]
|
||||
val reply2 = (actor2 ? GetRetainedMessage).get.asInstanceOf[Message]
|
||||
val reply1 = (actor1 !! GetRetainedMessage).get.asInstanceOf[Message]
|
||||
val reply2 = (actor2 !! GetRetainedMessage).get.asInstanceOf[Message]
|
||||
assert(reply1.body === "Test1")
|
||||
assert(reply2.body === "Test2")
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldDynamicallyRouteMessageToActorWithoutDefaultUuid = {
|
||||
val actor1 = actorOf(Props[Tester1]
|
||||
val actor2 = actorOf(Props[Tester1]
|
||||
val latch1 = (actor1 ? SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
val latch2 = (actor2 ? SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
@Test def shouldDynamicallyRouteMessageToActorWithoutDefaultUuid = {
|
||||
val actor1 = actorOf[Tester1].start
|
||||
val actor2 = actorOf[Tester1].start
|
||||
val latch1 = (actor1 !! SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
val latch2 = (actor2 !! SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
val endpoint = actorEndpoint("actor:uuid:")
|
||||
val exchange1 = endpoint.createExchange(ExchangePattern.InOnly)
|
||||
val exchange2 = endpoint.createExchange(ExchangePattern.InOnly)
|
||||
|
|
@ -201,35 +193,32 @@ class ActorProducerTest extends JUnitSuite with BeforeAndAfterAll {
|
|||
actorProducer(endpoint).process(exchange2)
|
||||
assert(latch1.await(5, TimeUnit.SECONDS))
|
||||
assert(latch2.await(5, TimeUnit.SECONDS))
|
||||
val reply1 = (actor1 ? GetRetainedMessage).get.asInstanceOf[Message]
|
||||
val reply2 = (actor2 ? GetRetainedMessage).get.asInstanceOf[Message]
|
||||
val reply1 = (actor1 !! GetRetainedMessage).get.asInstanceOf[Message]
|
||||
val reply2 = (actor2 !! GetRetainedMessage).get.asInstanceOf[Message]
|
||||
assert(reply1.body === "Test1")
|
||||
assert(reply2.body === "Test2")
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldThrowExceptionWhenIdNotSet{
|
||||
val actor = actorOf(Props[Tester1]
|
||||
val latch = (actor ? SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
@Test def shouldThrowExceptionWhenIdNotSet: Unit = {
|
||||
val actor = actorOf[Tester1].start
|
||||
val latch = (actor !! SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
val endpoint = actorEndpoint("actor:id:")
|
||||
intercept[ActorIdentifierNotSetException] {
|
||||
actorProducer(endpoint).process(endpoint.createExchange(ExchangePattern.InOnly))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldThrowExceptionWhenUuidNotSet{
|
||||
val actor = actorOf(Props[Tester1]
|
||||
val latch = (actor ? SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
@Test def shouldThrowExceptionWhenUuidNotSet: Unit = {
|
||||
val actor = actorOf[Tester1].start
|
||||
val latch = (actor !! SetExpectedMessageCount(1)).as[CountDownLatch].get
|
||||
val endpoint = actorEndpoint("actor:uuid:")
|
||||
intercept[ActorIdentifierNotSetException] {
|
||||
actorProducer(endpoint).process(endpoint.createExchange(ExchangePattern.InOnly))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldSendMessageToActorAndTimeout() {
|
||||
val actor = actorOf(Props[Tester3].withTimeout(Timeout(1)))
|
||||
@Test def shouldSendMessageToActorAndTimeout(): Unit = {
|
||||
val actor = actorOf[Tester3].start
|
||||
val endpoint = actorEndpoint("actor:uuid:%s" format actor.uuid)
|
||||
val exchange = endpoint.createExchange(ExchangePattern.InOut)
|
||||
exchange.getIn.setBody("Martin")
|
||||
|
|
@ -1,46 +1,41 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel;
|
||||
|
||||
import akka.japi.SideEffect;
|
||||
|
||||
import akka.actor.ActorRef;
|
||||
import akka.actor.ActorSystem;
|
||||
import akka.actor.Props;
|
||||
import akka.util.FiniteDuration;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import static akka.actor.Actors.*;
|
||||
import static akka.camel.CamelContextManager.*;
|
||||
import static akka.camel.CamelServiceManager.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
public class ConsumerJavaTestBase {
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpBeforeClass() {
|
||||
startCamelService();
|
||||
}
|
||||
static ActorSystem system = ActorSystem.create("test");
|
||||
static Camel camel = (Camel) CamelExtension.get(system);
|
||||
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownAfterClass() {
|
||||
stopCamelService();
|
||||
registry().local().shutdownAll();
|
||||
system.shutdown();
|
||||
}
|
||||
|
||||
@Test @Ignore // TODO: fix race
|
||||
|
||||
// org.apache.camel.CamelExchangeException: No consumers available
|
||||
// on endpoint: Endpoint[direct://error-handler-test-java]
|
||||
|
||||
@Test
|
||||
public void shouldHandleExceptionThrownByActorAndGenerateCustomResponse() {
|
||||
getMandatoryService().awaitEndpointActivation(1, new SideEffect() {
|
||||
public void apply() {
|
||||
actorOf(SampleErrorHandlingConsumer.class);
|
||||
}
|
||||
});
|
||||
String result = getMandatoryTemplate().requestBody("direct:error-handler-test-java", "hello", String.class);
|
||||
ActorRef ref = system.actorOf(new Props().withCreator(SampleErrorHandlingConsumer.class));
|
||||
camel.awaitActivation(ref, new FiniteDuration(1, TimeUnit.SECONDS));
|
||||
|
||||
String result = camel.template().requestBody("direct:error-handler-test-java", "hello", String.class);
|
||||
assertEquals("error: hello", result);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,109 +1,127 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel;
|
||||
|
||||
import akka.actor.ActorSystem;
|
||||
import akka.japi.Function;
|
||||
import org.apache.camel.NoTypeConversionAvailableException;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import akka.camel.CamelContextManager;
|
||||
import akka.camel.Message;
|
||||
import akka.japi.Function;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
public class MessageJavaTestBase {
|
||||
static Camel camel;
|
||||
private static ActorSystem system;
|
||||
private Map<String,Object> empty = new HashMap<String, Object>();
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpBeforeClass() {
|
||||
CamelContextManager.init();
|
||||
system = ActorSystem.create("test");
|
||||
camel = (Camel) CamelExtension.get(system);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanup(){
|
||||
system.shutdown();
|
||||
}
|
||||
|
||||
Message message(Object body){ return new Message(body, new HashMap()); }
|
||||
Message message(Object body, Map<String, Object> headers){ return new Message(body, headers); }
|
||||
private RichMessage rich(Message message) { return new RichMessage(message, camel.context()); }
|
||||
|
||||
|
||||
@Test public void shouldConvertDoubleBodyToString() {
|
||||
assertEquals("1.4", new Message("1.4").getBodyAs(String.class));
|
||||
assertEquals("1.4", rich(message("1.4", empty)).getBodyAs(String.class));
|
||||
}
|
||||
|
||||
@Test(expected=NoTypeConversionAvailableException.class)
|
||||
public void shouldThrowExceptionWhenConvertingDoubleBodyToInputStream() {
|
||||
new Message(1.4).getBodyAs(InputStream.class);
|
||||
rich(message(1.4)).getBodyAs(InputStream.class);
|
||||
}
|
||||
|
||||
|
||||
@Test public void shouldReturnDoubleHeader() {
|
||||
Message message = new Message("test" , createMap("test", 1.4));
|
||||
Message message = message("test" , createMap("test", 1.4));
|
||||
assertEquals(1.4, message.getHeader("test"));
|
||||
}
|
||||
|
||||
@Test public void shouldConvertDoubleHeaderToString() {
|
||||
Message message = new Message("test" , createMap("test", 1.4));
|
||||
assertEquals("1.4", message.getHeaderAs("test", String.class));
|
||||
Message message = message("test" , createMap("test", 1.4));
|
||||
assertEquals("1.4", rich(message).getHeaderAs("test", String.class));
|
||||
}
|
||||
|
||||
@Test public void shouldReturnSubsetOfHeaders() {
|
||||
Message message = new Message("test" , createMap("A", "1", "B", "2"));
|
||||
Message message = message("test" , createMap("A", "1", "B", "2"));
|
||||
assertEquals(createMap("B", "2"), message.getHeaders(createSet("B")));
|
||||
}
|
||||
|
||||
@Test(expected=UnsupportedOperationException.class)
|
||||
public void shouldReturnSubsetOfHeadersUnmodifiable() {
|
||||
Message message = new Message("test" , createMap("A", "1", "B", "2"));
|
||||
Message message = message("test" , createMap("A", "1", "B", "2"));
|
||||
message.getHeaders(createSet("B")).put("x", "y");
|
||||
}
|
||||
|
||||
@Test public void shouldReturnAllHeaders() {
|
||||
Message message = new Message("test" , createMap("A", "1", "B", "2"));
|
||||
Message message = message("test" , createMap("A", "1", "B", "2"));
|
||||
assertEquals(createMap("A", "1", "B", "2"), message.getHeaders());
|
||||
}
|
||||
|
||||
@Test(expected=UnsupportedOperationException.class)
|
||||
public void shouldReturnAllHeadersUnmodifiable() {
|
||||
Message message = new Message("test" , createMap("A", "1", "B", "2"));
|
||||
Message message = message("test" , createMap("A", "1", "B", "2"));
|
||||
message.getHeaders().put("x", "y");
|
||||
}
|
||||
|
||||
@Test public void shouldTransformBodyAndPreserveHeaders() {
|
||||
assertEquals(
|
||||
new Message("ab", createMap("A", "1")),
|
||||
new Message("a" , createMap("A", "1")).transformBody((Function<String, Object>) new TestTransformer()));
|
||||
message("ab", createMap("A", "1")),
|
||||
message("a" , createMap("A", "1")).mapBody(new TestTransformer()));
|
||||
}
|
||||
|
||||
@Test public void shouldConvertBodyAndPreserveHeaders() {
|
||||
assertEquals(
|
||||
new Message("1.4", createMap("A", "1")),
|
||||
new Message(1.4 , createMap("A", "1")).setBodyAs(String.class));
|
||||
message("1.4", createMap("A", "1")),
|
||||
rich(message(1.4 , createMap("A", "1"))).withBodyAs(String.class));
|
||||
}
|
||||
|
||||
@Test public void shouldSetBodyAndPreserveHeaders() {
|
||||
assertEquals(
|
||||
new Message("test2" , createMap("A", "1")),
|
||||
new Message("test1" , createMap("A", "1")).setBody("test2"));
|
||||
message("test2" , createMap("A", "1")),
|
||||
message("test1" , createMap("A", "1")).withBody("test2"));
|
||||
}
|
||||
|
||||
@Test public void shouldSetHeadersAndPreserveBody() {
|
||||
assertEquals(
|
||||
new Message("test1" , createMap("C", "3")),
|
||||
new Message("test1" , createMap("A", "1")).setHeaders(createMap("C", "3")));
|
||||
message("test1" , createMap("C", "3")),
|
||||
message("test1" , createMap("A", "1")).withHeaders(createMap("C", "3")));
|
||||
}
|
||||
|
||||
@Test public void shouldAddHeaderAndPreserveBodyAndHeaders() {
|
||||
assertEquals(
|
||||
new Message("test1" , createMap("A", "1", "B", "2")),
|
||||
new Message("test1" , createMap("A", "1")).addHeader("B", "2"));
|
||||
message("test1" , createMap("A", "1", "B", "2")),
|
||||
message("test1" , createMap("A", "1")).plusHeader("B", "2"));
|
||||
}
|
||||
|
||||
@Test public void shouldAddHeadersAndPreserveBodyAndHeaders() {
|
||||
assertEquals(
|
||||
new Message("test1" , createMap("A", "1", "B", "2")),
|
||||
new Message("test1" , createMap("A", "1")).addHeaders(createMap("B", "2")));
|
||||
message("test1" , createMap("A", "1", "B", "2")),
|
||||
message("test1" , createMap("A", "1")).plusHeaders(createMap("B", "2")));
|
||||
}
|
||||
|
||||
@Test public void shouldRemoveHeadersAndPreserveBodyAndRemainingHeaders() {
|
||||
assertEquals(
|
||||
new Message("test1" , createMap("A", "1")),
|
||||
new Message("test1" , createMap("A", "1", "B", "2")).removeHeader("B"));
|
||||
message("test1" , createMap("A", "1")),
|
||||
message("test1" , createMap("A", "1", "B", "2")).withoutHeader("B"));
|
||||
}
|
||||
|
||||
private static Set<String> createSet(String... entries) {
|
||||
|
|
@ -120,7 +138,7 @@ public class MessageJavaTestBase {
|
|||
return map;
|
||||
}
|
||||
|
||||
private static class TestTransformer implements Function<String, Object> {
|
||||
private static class TestTransformer implements Function<String, String> {
|
||||
public String apply(String param) {
|
||||
return param + "b";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,15 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel;
|
||||
|
||||
import akka.camel.javaapi.UntypedConsumerActor;
|
||||
import akka.util.Duration;
|
||||
import org.apache.camel.builder.Builder;
|
||||
import org.apache.camel.model.ProcessorDefinition;
|
||||
import org.apache.camel.model.RouteDefinition;
|
||||
import scala.Option;
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
|
|
@ -13,22 +20,28 @@ public class SampleErrorHandlingConsumer extends UntypedConsumerActor {
|
|||
return "direct:error-handler-test-java";
|
||||
}
|
||||
|
||||
public boolean isBlocking() {
|
||||
return true;
|
||||
@Override
|
||||
//TODO write test confirming this gets called in java
|
||||
public ProcessorDefinition onRouteDefinition(RouteDefinition rd) {
|
||||
return rd.onException(Exception.class).handled(true).transform(Builder.exceptionMessage()).end();
|
||||
}
|
||||
|
||||
public void preStart() {
|
||||
onRouteDefinition(new RouteDefinitionHandler() {
|
||||
public ProcessorDefinition<?> onRouteDefinition(RouteDefinition rd) {
|
||||
return rd.onException(Exception.class).handled(true).transform(Builder.exceptionMessage()).end();
|
||||
}
|
||||
});
|
||||
@Override
|
||||
public Duration replyTimeout(){
|
||||
return Duration.create(1, "second");
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void onReceive(Object message) throws Exception {
|
||||
Message msg = (Message)message;
|
||||
String body = msg.getBodyAs(String.class);
|
||||
Message msg = (Message) message;
|
||||
String body = rich(msg).getBodyAs(String.class);
|
||||
throw new Exception(String.format("error: %s", body));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preRestart(Throwable reason, Option<Object> message){
|
||||
getSender().tell(new Failure(reason));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,7 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel;
|
||||
|
||||
import akka.actor.UntypedActor;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel;
|
||||
|
||||
import akka.camel.UntypedConsumerActor;
|
||||
import akka.camel.javaapi.UntypedConsumerActor;
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
|
|
@ -12,10 +16,10 @@ public class SampleUntypedConsumer extends UntypedConsumerActor {
|
|||
}
|
||||
|
||||
public void onReceive(Object message) {
|
||||
Message msg = (Message)message;
|
||||
RichMessage msg = rich((Message)message);
|
||||
String body = msg.getBodyAs(String.class);
|
||||
String header = msg.getHeaderAs("test", String.class);
|
||||
sender.tell(String.format("%s %s", body, header));
|
||||
sender().tell(String.format("%s %s", body, header));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,23 +0,0 @@
|
|||
package akka.camel;
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
public class SampleUntypedConsumerBlocking extends UntypedConsumerActor {
|
||||
|
||||
public String getEndpointUri() {
|
||||
return "direct:test-untyped-consumer-blocking";
|
||||
}
|
||||
|
||||
public boolean isBlocking() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void onReceive(Object message) {
|
||||
Message msg = (Message)message;
|
||||
String body = msg.getBodyAs(String.class);
|
||||
String header = msg.getHeaderAs("test", String.class);
|
||||
sender.tell(String.format("%s %s", body, header));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,5 +1,10 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel;
|
||||
|
||||
import akka.camel.javaapi.UntypedProducerActor;
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
|
|
@ -11,8 +16,8 @@ public class SampleUntypedForwardingProducer extends UntypedProducerActor {
|
|||
|
||||
@Override
|
||||
public void onReceiveAfterProduce(Object message) {
|
||||
Message msg = (Message)message;
|
||||
RichMessage msg = rich((Message)message);
|
||||
String body = msg.getBodyAs(String.class);
|
||||
CamelContextManager.getMandatoryTemplate().sendBody("direct:forward-test-1", body);
|
||||
getProducerTemplate().sendBody("direct:forward-test-1", body);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,11 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel;
|
||||
|
||||
import akka.camel.javaapi.UntypedProducerActor;
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -1,11 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="false" debug="false">
|
||||
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>[%4p] [%d{ISO8601}] [%t] %c{1}: %m%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
<root level="OFF">
|
||||
<appender-ref ref="stdout"/>
|
||||
</root>
|
||||
</configuration>
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
import akka.util.duration._
|
||||
import org.apache.camel.ProducerTemplate
|
||||
import akka.actor._
|
||||
import akka.util.Timeout
|
||||
import java.util.concurrent.{ TimeUnit, CountDownLatch }
|
||||
import TestSupport._
|
||||
import org.scalatest.WordSpec
|
||||
|
||||
class ActivationIntegrationTest extends WordSpec with MustMatchers with SharedCamelSystem {
|
||||
implicit val timeout = Timeout(10 seconds)
|
||||
def template: ProducerTemplate = camel.template
|
||||
|
||||
def testActorWithEndpoint(uri: String): ActorRef = { system.actorOf(Props(new TestConsumer(uri))) }
|
||||
|
||||
"ActivationAware must be notified when endpoint is activated" in {
|
||||
val actor = testActorWithEndpoint("direct:actor-1")
|
||||
try {
|
||||
camel.awaitActivation(actor, 1 second)
|
||||
} catch {
|
||||
case e: ActivationTimeoutException ⇒ fail("Failed to get notification within 1 second")
|
||||
}
|
||||
|
||||
template.requestBody("direct:actor-1", "test") must be("received test")
|
||||
}
|
||||
|
||||
"ActivationAware must be notified when endpoint is de-activated" in {
|
||||
val stopped = new CountDownLatch(1)
|
||||
val actor = start(new Consumer {
|
||||
def endpointUri = "direct:a3"
|
||||
def receive = { case _ ⇒ {} }
|
||||
|
||||
override def postStop() = {
|
||||
super.postStop()
|
||||
stopped.countDown()
|
||||
}
|
||||
})
|
||||
camel.awaitActivation(actor, 1 second)
|
||||
|
||||
system.stop(actor)
|
||||
camel.awaitDeactivation(actor, 1 second)
|
||||
if (!stopped.await(1, TimeUnit.SECONDS)) fail("Actor must have stopped quickly after deactivation!")
|
||||
}
|
||||
|
||||
"ActivationAware must time out when waiting for endpoint de-activation for too long" in {
|
||||
val actor = start(new TestConsumer("direct:a5"))
|
||||
camel.awaitActivation(actor, 1 second)
|
||||
intercept[DeActivationTimeoutException] {
|
||||
camel.awaitDeactivation(actor, 1 millis)
|
||||
}
|
||||
}
|
||||
|
||||
"awaitActivation must fail if notification timeout is too short and activation is not complete yet" in {
|
||||
val actor = testActorWithEndpoint("direct:actor-4")
|
||||
intercept[ActivationTimeoutException] {
|
||||
camel.awaitActivation(actor, 0 seconds)
|
||||
}
|
||||
}
|
||||
|
||||
class TestConsumer(uri: String) extends Consumer {
|
||||
def endpointUri = uri
|
||||
override def receive = {
|
||||
case msg: Message ⇒ sender ! "received " + msg.body
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
package akka.camel
|
||||
|
||||
import org.apache.camel.impl.{ DefaultProducerTemplate, DefaultCamelContext }
|
||||
import org.junit.Test
|
||||
import org.scalatest.junit.JUnitSuite
|
||||
|
||||
class CamelContextLifecycleTest extends JUnitSuite with CamelContextLifecycle {
|
||||
@Test
|
||||
def shouldManageCustomCamelContext {
|
||||
assert(context === None)
|
||||
assert(template === None)
|
||||
|
||||
intercept[IllegalStateException] { mandatoryContext }
|
||||
intercept[IllegalStateException] { mandatoryTemplate }
|
||||
|
||||
val ctx = new TestCamelContext
|
||||
assert(ctx.isStreamCaching === false)
|
||||
|
||||
init(ctx)
|
||||
|
||||
assert(mandatoryContext.isStreamCaching === true)
|
||||
assert(!mandatoryContext.asInstanceOf[TestCamelContext].isStarted)
|
||||
assert(mandatoryTemplate.asInstanceOf[DefaultProducerTemplate].isStarted)
|
||||
|
||||
start
|
||||
|
||||
assert(mandatoryContext.asInstanceOf[TestCamelContext].isStarted)
|
||||
assert(mandatoryTemplate.asInstanceOf[DefaultProducerTemplate].isStarted)
|
||||
|
||||
stop
|
||||
|
||||
assert(!mandatoryContext.asInstanceOf[TestCamelContext].isStarted)
|
||||
assert(!mandatoryTemplate.asInstanceOf[DefaultProducerTemplate].isStarted)
|
||||
}
|
||||
|
||||
class TestCamelContext extends DefaultCamelContext
|
||||
}
|
||||
|
|
@ -1,95 +1,92 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import org.apache.camel.impl.{ DefaultCamelContext, DefaultExchange }
|
||||
import org.apache.camel.ExchangePattern
|
||||
import org.junit.Test
|
||||
import org.scalatest.junit.JUnitSuite
|
||||
import internal.CamelExchangeAdapter
|
||||
import org.apache.camel.impl.DefaultExchange
|
||||
import org.apache.camel.{ Exchange, ExchangePattern }
|
||||
import akka.camel.TestSupport.{ SharedCamelSystem, MessageSugar }
|
||||
import org.scalatest.FunSuite
|
||||
|
||||
class CamelExchangeAdapterTest extends JUnitSuite {
|
||||
import CamelMessageConversion.toExchangeAdapter
|
||||
class CamelExchangeAdapterTest extends FunSuite with SharedCamelSystem with MessageSugar {
|
||||
|
||||
@Test
|
||||
def shouldSetInMessageFromRequestMessage = {
|
||||
val e1 = sampleInOnly.fromRequestMessage(Message("x"))
|
||||
//TODO: Get rid of implicit.
|
||||
// It is here, as was easier to add this implicit than to rewrite the whole test...
|
||||
implicit def exchangeToAdapter(e: Exchange) = new CamelExchangeAdapter(e)
|
||||
|
||||
test("mustSetInMessageFromRequestMessage") {
|
||||
val e1 = sampleInOnly
|
||||
e1.setRequest(Message("x"))
|
||||
assert(e1.getIn.getBody === "x")
|
||||
val e2 = sampleInOut.fromRequestMessage(Message("y"))
|
||||
val e2 = sampleInOut
|
||||
e2.setRequest(Message("y"))
|
||||
assert(e2.getIn.getBody === "y")
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldSetOutMessageFromResponseMessage = {
|
||||
val e1 = sampleInOut.fromResponseMessage(Message("y"))
|
||||
test("mustSetOutMessageFromResponseMessage") {
|
||||
val e1 = sampleInOut; e1.setResponse(Message("y"))
|
||||
assert(e1.getOut.getBody === "y")
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldSetInMessageFromResponseMessage = {
|
||||
val e1 = sampleInOnly.fromResponseMessage(Message("x"))
|
||||
test("mustSetInMessageFromResponseMessage") {
|
||||
val e1 = sampleInOnly; e1.setResponse(Message("x"))
|
||||
assert(e1.getIn.getBody === "x")
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldSetExceptionFromFailureMessage = {
|
||||
val e1 = sampleInOnly.fromFailureMessage(Failure(new Exception("test1")))
|
||||
test("mustSetExceptionFromFailureMessage") {
|
||||
val e1 = sampleInOnly; e1.setFailure(Failure(new Exception("test1")))
|
||||
assert(e1.getException.getMessage === "test1")
|
||||
val e2 = sampleInOut.fromFailureMessage(Failure(new Exception("test2")))
|
||||
val e2 = sampleInOut; e2.setFailure(Failure(new Exception("test2")))
|
||||
assert(e2.getException.getMessage === "test2")
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateRequestMessageFromInMessage = {
|
||||
test("mustCreateRequestMessageFromInMessage") {
|
||||
val m = sampleInOnly.toRequestMessage
|
||||
assert(m === Message("test-in", Map("key-in" -> "val-in")))
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateResponseMessageFromInMessage = {
|
||||
test("mustCreateResponseMessageFromInMessage") {
|
||||
val m = sampleInOnly.toResponseMessage
|
||||
assert(m === Message("test-in", Map("key-in" -> "val-in")))
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateResponseMessageFromOutMessage = {
|
||||
test("mustCreateResponseMessageFromOutMessage") {
|
||||
val m = sampleInOut.toResponseMessage
|
||||
assert(m === Message("test-out", Map("key-out" -> "val-out")))
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateFailureMessageFromExceptionAndInMessage = {
|
||||
test("mustCreateFailureMessageFromExceptionAndInMessage") {
|
||||
val e1 = sampleInOnly
|
||||
e1.setException(new Exception("test1"))
|
||||
assert(e1.toFailureMessage.cause.getMessage === "test1")
|
||||
assert(e1.toFailureMessage.headers("key-in") === "val-in")
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateFailureMessageFromExceptionAndOutMessage = {
|
||||
test("mustCreateFailureMessageFromExceptionAndOutMessage") {
|
||||
val e1 = sampleInOut
|
||||
e1.setException(new Exception("test2"))
|
||||
assert(e1.toFailureMessage.cause.getMessage === "test2")
|
||||
assert(e1.toFailureMessage.headers("key-out") === "val-out")
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateRequestMessageFromInMessageWithAdditionalHeader = {
|
||||
test("mustCreateRequestMessageFromInMessageWithAdditionalHeader") {
|
||||
val m = sampleInOnly.toRequestMessage(Map("x" -> "y"))
|
||||
assert(m === Message("test-in", Map("key-in" -> "val-in", "x" -> "y")))
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateResponseMessageFromInMessageWithAdditionalHeader = {
|
||||
test("mustCreateResponseMessageFromInMessageWithAdditionalHeader") {
|
||||
val m = sampleInOnly.toResponseMessage(Map("x" -> "y"))
|
||||
assert(m === Message("test-in", Map("key-in" -> "val-in", "x" -> "y")))
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateResponseMessageFromOutMessageWithAdditionalHeader = {
|
||||
test("mustCreateResponseMessageFromOutMessageWithAdditionalHeader") {
|
||||
val m = sampleInOut.toResponseMessage(Map("x" -> "y"))
|
||||
assert(m === Message("test-out", Map("key-out" -> "val-out", "x" -> "y")))
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateFailureMessageFromExceptionAndInMessageWithAdditionalHeader = {
|
||||
test("mustCreateFailureMessageFromExceptionAndInMessageWithAdditionalHeader") {
|
||||
val e1 = sampleInOnly
|
||||
e1.setException(new Exception("test1"))
|
||||
assert(e1.toFailureMessage.cause.getMessage === "test1")
|
||||
|
|
@ -98,8 +95,7 @@ class CamelExchangeAdapterTest extends JUnitSuite {
|
|||
assert(headers("x") === "y")
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateFailureMessageFromExceptionAndOutMessageWithAdditionalHeader = {
|
||||
test("mustCreateFailureMessageFromExceptionAndOutMessageWithAdditionalHeader") {
|
||||
val e1 = sampleInOut
|
||||
e1.setException(new Exception("test2"))
|
||||
assert(e1.toFailureMessage.cause.getMessage === "test2")
|
||||
|
|
@ -112,7 +108,7 @@ class CamelExchangeAdapterTest extends JUnitSuite {
|
|||
private def sampleInOut = sampleExchange(ExchangePattern.InOut)
|
||||
|
||||
private def sampleExchange(pattern: ExchangePattern) = {
|
||||
val exchange = new DefaultExchange(new DefaultCamelContext)
|
||||
val exchange = new DefaultExchange(camel.context)
|
||||
exchange.getIn.setBody("test-in")
|
||||
exchange.getOut.setBody("test-out")
|
||||
exchange.getIn.setHeader("key-in", "val-in")
|
||||
|
|
|
|||
|
|
@ -1,40 +0,0 @@
|
|||
package akka.camel
|
||||
|
||||
import org.apache.camel.impl.DefaultMessage
|
||||
import org.junit.Test
|
||||
import org.scalatest.junit.JUnitSuite
|
||||
|
||||
class CamelMessageAdapterTest extends JUnitSuite {
|
||||
import CamelMessageConversion.toMessageAdapter
|
||||
|
||||
@Test
|
||||
def shouldOverwriteBodyAndAddHeader = {
|
||||
val cm = sampleMessage.fromMessage(Message("blah", Map("key" -> "baz")))
|
||||
assert(cm.getBody === "blah")
|
||||
assert(cm.getHeader("foo") === "bar")
|
||||
assert(cm.getHeader("key") === "baz")
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateMessageWithBodyAndHeader = {
|
||||
val m = sampleMessage.toMessage
|
||||
assert(m.body === "test")
|
||||
assert(m.headers("foo") === "bar")
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateMessageWithBodyAndHeaderAndCustomHeader = {
|
||||
val m = sampleMessage.toMessage(Map("key" -> "baz"))
|
||||
assert(m.body === "test")
|
||||
assert(m.headers("foo") === "bar")
|
||||
assert(m.headers("key") === "baz")
|
||||
}
|
||||
|
||||
private[camel] def sampleMessage = {
|
||||
val message = new DefaultMessage
|
||||
message.setBody("test")
|
||||
message.setHeader("foo", "bar")
|
||||
message
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
package akka.camel
|
||||
|
||||
import org.scalatest.{ BeforeAndAfterAll, WordSpec }
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
|
||||
import akka.actor.Actor
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
class CamelServiceManagerTest extends WordSpec with BeforeAndAfterAll with MustMatchers {
|
||||
|
||||
override def afterAll = {
|
||||
CamelServiceManager.stopCamelService
|
||||
Actor.registry.local.shutdownAll
|
||||
}
|
||||
|
||||
"A CamelServiceManager" when {
|
||||
"the startCamelService method been has been called" must {
|
||||
"have registered the started CamelService instance" in {
|
||||
val service = CamelServiceManager.startCamelService
|
||||
CamelServiceManager.mandatoryService must be theSameInstanceAs (service)
|
||||
}
|
||||
}
|
||||
"the stopCamelService method been has been called" must {
|
||||
"have unregistered the current CamelService instance" in {
|
||||
val service = CamelServiceManager.stopCamelService
|
||||
CamelServiceManager.service must be(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"A CamelServiceManager" when {
|
||||
val service = CamelServiceFactory.createCamelService
|
||||
"a CamelService instance has been started externally" must {
|
||||
"have registered the started CamelService instance" in {
|
||||
service.start
|
||||
CamelServiceManager.mandatoryService must be theSameInstanceAs (service)
|
||||
}
|
||||
}
|
||||
"the current CamelService instance has been stopped externally" must {
|
||||
"have unregistered the current CamelService instance" in {
|
||||
service.stop
|
||||
CamelServiceManager.service must be(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"A CamelServiceManager" when {
|
||||
"a CamelService has been started" must {
|
||||
"not allow further CamelService instances to be started" in {
|
||||
CamelServiceManager.startCamelService
|
||||
intercept[IllegalStateException] { CamelServiceManager.startCamelService }
|
||||
}
|
||||
}
|
||||
"a CamelService has been stopped" must {
|
||||
"only allow the current CamelService instance to be stopped" in {
|
||||
intercept[IllegalStateException] { CamelServiceFactory.createCamelService.stop }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,159 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import akka.actor._
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
import akka.util.duration._
|
||||
import java.util.concurrent.TimeUnit._
|
||||
import TestSupport._
|
||||
import org.scalatest.WordSpec
|
||||
import org.apache.camel.model.RouteDefinition
|
||||
import org.apache.camel.builder.Builder
|
||||
import org.apache.camel.{ FailedToCreateRouteException, CamelExecutionException }
|
||||
import java.util.concurrent.{ ExecutionException, TimeUnit, TimeoutException, CountDownLatch }
|
||||
|
||||
class ConsumerIntegrationTest extends WordSpec with MustMatchers with NonSharedCamelSystem {
|
||||
|
||||
"Consumer must throw FailedToCreateRouteException, while awaiting activation, if endpoint is invalid" in {
|
||||
val actorRef = system.actorOf(Props(new TestActor(uri = "some invalid uri")))
|
||||
|
||||
intercept[FailedToCreateRouteException] {
|
||||
camel.awaitActivation(actorRef, timeout = 1 second)
|
||||
}
|
||||
}
|
||||
|
||||
"Consumer must support in-out messaging" in {
|
||||
start(new Consumer {
|
||||
def endpointUri = "direct:a1"
|
||||
protected def receive = {
|
||||
case m: Message ⇒ sender ! "received " + m.bodyAs[String]
|
||||
}
|
||||
})
|
||||
camel.sendTo("direct:a1", msg = "some message") must be("received some message")
|
||||
}
|
||||
|
||||
"Consumer must time-out if consumer is slow" in {
|
||||
val SHORT_TIMEOUT = 10 millis
|
||||
val LONG_WAIT = 200 millis
|
||||
|
||||
start(new Consumer {
|
||||
override def replyTimeout = SHORT_TIMEOUT
|
||||
|
||||
def endpointUri = "direct:a3"
|
||||
protected def receive = { case _ ⇒ { Thread.sleep(LONG_WAIT.toMillis); sender ! "done" } }
|
||||
})
|
||||
|
||||
val exception = intercept[CamelExecutionException] {
|
||||
camel.sendTo("direct:a3", msg = "some msg 3")
|
||||
}
|
||||
exception.getCause.getClass must be(classOf[TimeoutException])
|
||||
}
|
||||
|
||||
"Consumer must process messages even after actor restart" in {
|
||||
val restarted = new CountDownLatch(1)
|
||||
val consumer = start(new Consumer {
|
||||
def endpointUri = "direct:a2"
|
||||
|
||||
protected def receive = {
|
||||
case "throw" ⇒ throw new Exception
|
||||
case m: Message ⇒ sender ! "received " + m.bodyAs[String]
|
||||
}
|
||||
|
||||
override def postRestart(reason: Throwable) {
|
||||
restarted.countDown()
|
||||
}
|
||||
})
|
||||
consumer ! "throw"
|
||||
if (!restarted.await(1, SECONDS)) fail("Actor failed to restart!")
|
||||
|
||||
val response = camel.sendTo("direct:a2", msg = "xyz")
|
||||
response must be("received xyz")
|
||||
}
|
||||
|
||||
"Consumer must unregister itself when stopped" in {
|
||||
val consumer = start(new TestActor())
|
||||
camel.awaitActivation(consumer, 1 second)
|
||||
|
||||
camel.routeCount must be > (0)
|
||||
|
||||
system.stop(consumer)
|
||||
camel.awaitDeactivation(consumer, 1 second)
|
||||
|
||||
camel.routeCount must be(0)
|
||||
}
|
||||
|
||||
"Error passing consumer supports error handling through route modification" in {
|
||||
start(new ErrorThrowingConsumer("direct:error-handler-test") with ErrorPassing {
|
||||
override def onRouteDefinition(rd: RouteDefinition) = {
|
||||
rd.onException(classOf[Exception]).handled(true).transform(Builder.exceptionMessage).end
|
||||
}
|
||||
})
|
||||
camel.sendTo("direct:error-handler-test", msg = "hello") must be("error: hello")
|
||||
}
|
||||
|
||||
"Error passing consumer supports redelivery through route modification" in {
|
||||
start(new FailingOnceConsumer("direct:failing-once-concumer") with ErrorPassing {
|
||||
override def onRouteDefinition(rd: RouteDefinition) = {
|
||||
rd.onException(classOf[Exception]).maximumRedeliveries(1).end
|
||||
}
|
||||
})
|
||||
camel.sendTo("direct:failing-once-concumer", msg = "hello") must be("accepted: hello")
|
||||
}
|
||||
|
||||
"Consumer supports manual Ack" in {
|
||||
start(new ManualAckConsumer() {
|
||||
def endpointUri = "direct:manual-ack"
|
||||
protected def receive = { case _ ⇒ sender ! Ack }
|
||||
})
|
||||
camel.template.asyncSendBody("direct:manual-ack", "some message").get(1, TimeUnit.SECONDS) must be(null) //should not timeout
|
||||
}
|
||||
|
||||
"Consumer handles manual Ack failure" in {
|
||||
val someException = new Exception("e1")
|
||||
start(new ManualAckConsumer() {
|
||||
def endpointUri = "direct:manual-ack"
|
||||
protected def receive = { case _ ⇒ sender ! Failure(someException) }
|
||||
})
|
||||
|
||||
intercept[ExecutionException] {
|
||||
camel.template.asyncSendBody("direct:manual-ack", "some message").get(1, TimeUnit.SECONDS)
|
||||
}.getCause.getCause must be(someException)
|
||||
}
|
||||
|
||||
"Consumer should time-out, if manual Ack not received within replyTimeout and should give a human readable error message" in {
|
||||
start(new ManualAckConsumer() {
|
||||
override def replyTimeout = 10 millis
|
||||
def endpointUri = "direct:manual-ack"
|
||||
protected def receive = { case _ ⇒ }
|
||||
})
|
||||
|
||||
intercept[ExecutionException] {
|
||||
camel.template.asyncSendBody("direct:manual-ack", "some message").get(1, TimeUnit.SECONDS)
|
||||
}.getCause.getCause.getMessage must include("Failed to get Ack")
|
||||
}
|
||||
}
|
||||
|
||||
class ErrorThrowingConsumer(override val endpointUri: String) extends Consumer {
|
||||
def receive = {
|
||||
case msg: Message ⇒ throw new Exception("error: %s" format msg.body)
|
||||
}
|
||||
}
|
||||
|
||||
class FailingOnceConsumer(override val endpointUri: String) extends Consumer {
|
||||
|
||||
def receive = {
|
||||
case msg: Message ⇒
|
||||
if (msg.headerAs[Boolean]("CamelRedelivered").getOrElse(false))
|
||||
sender ! ("accepted: %s" format msg.body)
|
||||
else
|
||||
throw new Exception("rejected: %s" format msg.body)
|
||||
}
|
||||
}
|
||||
|
||||
class TestActor(uri: String = "file://target/abcde") extends Consumer {
|
||||
def endpointUri = uri
|
||||
protected def receive = { case _ ⇒ /* do nothing */ }
|
||||
}
|
||||
|
|
@ -1,5 +1,9 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import org.scalatest.junit.JUnitSuite
|
||||
|
||||
class ConsumerJavaTest extends ConsumerJavaTestBase with JUnitSuite
|
||||
class ConsumerJavaTest extends ConsumerJavaTestBase with JUnitSuite
|
||||
|
|
@ -1,61 +0,0 @@
|
|||
package akka.camel
|
||||
|
||||
import java.util.concurrent.{ CountDownLatch, TimeUnit }
|
||||
|
||||
import org.junit.{ Before, After, Test }
|
||||
import org.scalatest.junit.JUnitSuite
|
||||
|
||||
import akka.actor._
|
||||
import akka.actor.Actor._
|
||||
import akka.camel.CamelTestSupport.{ SetExpectedMessageCount ⇒ SetExpectedTestMessageCount, _ }
|
||||
import akka.dispatch.Await
|
||||
|
||||
class ConsumerPublishRequestorTest extends JUnitSuite {
|
||||
import ConsumerPublishRequestorTest._
|
||||
|
||||
var publisher: ActorRef = _
|
||||
var requestor: ActorRef = _
|
||||
var consumer: LocalActorRef = _
|
||||
|
||||
@Before
|
||||
def setUp{
|
||||
publisher = actorOf(Props(new ConsumerPublisherMock)
|
||||
requestor = actorOf(Props(new ConsumerPublishRequestor)
|
||||
requestor ! InitPublishRequestor(publisher)
|
||||
consumer = actorOf(Props(new Actor with Consumer {
|
||||
def endpointUri = "mock:test"
|
||||
protected def receive = null
|
||||
}).asInstanceOf[LocalActorRef]
|
||||
}
|
||||
|
||||
@After
|
||||
def tearDown = {
|
||||
Actor.registry.removeListener(requestor);
|
||||
Actor.registry.local.shutdownAll
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldReceiveOneConsumerRegisteredEvent = {
|
||||
val latch = Await.result((publisher ? SetExpectedTestMessageCount(1)).mapTo[CountDownLatch], 5 seconds)
|
||||
requestor ! ActorRegistered(consumer.address, consumer)
|
||||
assert(latch.await(5000, TimeUnit.MILLISECONDS))
|
||||
assert(Await.result(publisher ? GetRetainedMessage, 5 seconds) ===
|
||||
ConsumerActorRegistered(consumer, consumer.underlyingActorInstance.asInstanceOf[Consumer]))
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldReceiveOneConsumerUnregisteredEvent = {
|
||||
val latch = Await.result((publisher ? SetExpectedTestMessageCount(1)).mapTo[CountDownLatch], 5 seconds)
|
||||
requestor ! ActorUnregistered(consumer.address, consumer)
|
||||
assert(latch.await(5000, TimeUnit.MILLISECONDS))
|
||||
assert(Await.result(publisher ? GetRetainedMessage, 5 seconds) ===
|
||||
ConsumerActorUnregistered(consumer, consumer.underlyingActorInstance.asInstanceOf[Consumer]))
|
||||
}
|
||||
}
|
||||
|
||||
object ConsumerPublishRequestorTest {
|
||||
class ConsumerPublisherMock extends TestActor with Retain with Countdown {
|
||||
def handler = retain andThen countdown
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,72 +0,0 @@
|
|||
package akka.camel
|
||||
|
||||
import org.junit.Test
|
||||
import org.scalatest.junit.JUnitSuite
|
||||
import akka.actor.{ ActorRef, Actor, LocalActorRef }
|
||||
|
||||
class ConsumerRegisteredTest extends JUnitSuite {
|
||||
import ConsumerRegisteredTest._
|
||||
|
||||
@Test
|
||||
def shouldCreateSomeNonBlockingPublishRequestFromConsumer = {
|
||||
val c = Actor.actorOf(Props[ConsumerActor1]
|
||||
val event = ConsumerActorRegistered.eventFor(c)
|
||||
assert(event === Some(ConsumerActorRegistered(c, consumerOf(c))))
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateSomeBlockingPublishRequestFromConsumer = {
|
||||
val c = Actor.actorOf(Props[ConsumerActor2]
|
||||
val event = ConsumerActorRegistered.eventFor(c)
|
||||
assert(event === Some(ConsumerActorRegistered(c, consumerOf(c))))
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateNoneFromConsumer = {
|
||||
val event = ConsumerActorRegistered.eventFor(Actor.actorOf(Props[PlainActor])
|
||||
assert(event === None)
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateSomeNonBlockingPublishRequestFromUntypedConsumer = {
|
||||
val uc = Actor.actorOf(classOf[SampleUntypedConsumer])
|
||||
val event = ConsumerActorRegistered.eventFor(uc)
|
||||
assert(event === Some(ConsumerActorRegistered(uc, consumerOf(uc))))
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateSomeBlockingPublishRequestFromUntypedConsumer = {
|
||||
val uc = Actor.actorOf(classOf[SampleUntypedConsumerBlocking])
|
||||
val event = ConsumerActorRegistered.eventFor(uc)
|
||||
assert(event === Some(ConsumerActorRegistered(uc, consumerOf(uc))))
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateNoneFromUntypedConsumer = {
|
||||
val a = Actor.actorOf(classOf[SampleUntypedActor])
|
||||
val event = ConsumerActorRegistered.eventFor(a)
|
||||
assert(event === None)
|
||||
}
|
||||
|
||||
private def consumerOf(ref: ActorRef) = ref match {
|
||||
case l: LocalActorRef ⇒ l.underlyingActorInstance.asInstanceOf[Consumer]
|
||||
case _ ⇒ null: Consumer
|
||||
}
|
||||
}
|
||||
|
||||
object ConsumerRegisteredTest {
|
||||
class ConsumerActor1 extends Actor with Consumer {
|
||||
def endpointUri = "mock:test1"
|
||||
protected def receive = null
|
||||
}
|
||||
|
||||
class ConsumerActor2 extends Actor with Consumer {
|
||||
def endpointUri = "mock:test2"
|
||||
override def blocking = true
|
||||
protected def receive = null
|
||||
}
|
||||
|
||||
class PlainActor extends Actor {
|
||||
protected def receive = null
|
||||
}
|
||||
}
|
||||
|
|
@ -1,295 +0,0 @@
|
|||
package akka.camel
|
||||
|
||||
import java.util.concurrent.{ TimeoutException, CountDownLatch, TimeUnit }
|
||||
|
||||
import org.apache.camel.{ AsyncProcessor, AsyncCallback, CamelExecutionException }
|
||||
import org.apache.camel.builder.Builder
|
||||
import org.apache.camel.component.direct.DirectEndpoint
|
||||
import org.apache.camel.model.RouteDefinition
|
||||
import org.scalatest.{ BeforeAndAfterAll, WordSpec }
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
|
||||
import akka.actor.Actor._
|
||||
import akka.actor._
|
||||
|
||||
/**
|
||||
* @author Martin Krasser
|
||||
*/
|
||||
class ConsumerScalaTest extends WordSpec with BeforeAndAfterAll with MustMatchers {
|
||||
import CamelContextManager.mandatoryContext
|
||||
import CamelContextManager.mandatoryTemplate
|
||||
import ConsumerScalaTest._
|
||||
|
||||
var service: CamelService = null
|
||||
|
||||
override protected def beforeAll = {
|
||||
registry.local.shutdownAll
|
||||
service = CamelServiceFactory.createCamelService
|
||||
// register test consumer before registering the publish requestor
|
||||
// and before starting the CamelService (registry is scanned for consumers)
|
||||
actorOf(Props(new TestConsumer("direct:publish-test-1"))
|
||||
service.registerPublishRequestor
|
||||
service.awaitEndpointActivation(1) {
|
||||
service.start
|
||||
} must be(true)
|
||||
}
|
||||
|
||||
override protected def afterAll = {
|
||||
service.stop
|
||||
registry.local.shutdownAll
|
||||
}
|
||||
|
||||
"A responding consumer" when {
|
||||
var consumer: ActorRef = null
|
||||
"started before starting the CamelService" must {
|
||||
"support an in-out message exchange via its endpoint" in {
|
||||
mandatoryTemplate.requestBody("direct:publish-test-1", "msg1") must equal("received msg1")
|
||||
}
|
||||
}
|
||||
"not started" must {
|
||||
"not have an associated endpoint in the CamelContext" in {
|
||||
CamelContextManager.mandatoryContext.hasEndpoint("direct:publish-test-2") must be(null)
|
||||
}
|
||||
}
|
||||
"started" must {
|
||||
"support an in-out message exchange via its endpoint" in {
|
||||
service.awaitEndpointActivation(1) {
|
||||
consumer = actorOf(Props(new TestConsumer("direct:publish-test-2"))
|
||||
} must be(true)
|
||||
mandatoryTemplate.requestBody("direct:publish-test-2", "msg2") must equal("received msg2")
|
||||
}
|
||||
"have an associated endpoint in the CamelContext" in {
|
||||
CamelContextManager.mandatoryContext.hasEndpoint("direct:publish-test-2") must not be (null)
|
||||
}
|
||||
}
|
||||
"stopped" must {
|
||||
"not support an in-out message exchange via its endpoint" in {
|
||||
service.awaitEndpointDeactivation(1) {
|
||||
consumer.stop
|
||||
} must be(true)
|
||||
intercept[CamelExecutionException] {
|
||||
mandatoryTemplate.requestBody("direct:publish-test-2", "msg2")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"A responding, untyped consumer" when {
|
||||
var consumer: ActorRef = null
|
||||
"started" must {
|
||||
"support an in-out message exchange via its endpoint" in {
|
||||
service.awaitEndpointActivation(1) {
|
||||
consumer = Actor.actorOf(classOf[SampleUntypedConsumer])
|
||||
} must be(true)
|
||||
mandatoryTemplate.requestBodyAndHeader("direct:test-untyped-consumer", "x", "test", "y") must equal("x y")
|
||||
}
|
||||
}
|
||||
"stopped" must {
|
||||
"not support an in-out message exchange via its endpoint" in {
|
||||
service.awaitEndpointDeactivation(1) {
|
||||
consumer.stop
|
||||
} must be(true)
|
||||
intercept[CamelExecutionException] {
|
||||
mandatoryTemplate.sendBodyAndHeader("direct:test-untyped-consumer", "blah", "test", "blub")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"A non-responding, blocking consumer" when {
|
||||
"receiving an in-out message exchange" must {
|
||||
"lead to a TimeoutException" in {
|
||||
service.awaitEndpointActivation(1) {
|
||||
actorOf(Props(creator = () ⇒ new TestBlocker("direct:publish-test-5")))
|
||||
} must be(true)
|
||||
|
||||
try {
|
||||
mandatoryTemplate.requestBody("direct:publish-test-5", "msg3")
|
||||
fail("expected TimoutException not thrown")
|
||||
} catch {
|
||||
case e ⇒ {
|
||||
assert(e.getCause.isInstanceOf[TimeoutException])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"A responding, blocking consumer" when {
|
||||
"activated with a custom error handler" must {
|
||||
"handle thrown exceptions by generating a custom response" in {
|
||||
service.awaitEndpointActivation(1) {
|
||||
actorOf(Props[ErrorHandlingConsumer]
|
||||
} must be(true)
|
||||
mandatoryTemplate.requestBody("direct:error-handler-test", "hello") must equal("error: hello")
|
||||
|
||||
}
|
||||
}
|
||||
"activated with a custom redelivery handler" must {
|
||||
"handle thrown exceptions by redelivering the initial message" in {
|
||||
service.awaitEndpointActivation(1) {
|
||||
actorOf(Props[RedeliveringConsumer]
|
||||
} must be(true)
|
||||
mandatoryTemplate.requestBody("direct:redelivery-test", "hello") must equal("accepted: hello")
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"An non auto-acknowledging consumer" when {
|
||||
"started" must {
|
||||
"must support acknowledgements on system level" in {
|
||||
|
||||
var consumer: ActorRef = null
|
||||
|
||||
service.awaitEndpointActivation(1) {
|
||||
consumer = actorOf(Props(new TestAckConsumer("direct:system-ack-test"))
|
||||
} must be(true)
|
||||
|
||||
val endpoint = mandatoryContext.getEndpoint("direct:system-ack-test", classOf[DirectEndpoint])
|
||||
val producer = endpoint.createProducer.asInstanceOf[AsyncProcessor]
|
||||
val exchange = endpoint.createExchange
|
||||
|
||||
val latch = new CountDownLatch(1)
|
||||
val handler = new AsyncCallback {
|
||||
def done(doneSync: Boolean) = {
|
||||
doneSync must be(false)
|
||||
latch.countDown
|
||||
}
|
||||
}
|
||||
|
||||
exchange.getIn.setBody("test")
|
||||
producer.process(exchange, handler)
|
||||
|
||||
latch.await(5, TimeUnit.SECONDS) must be(true)
|
||||
consumer.stop
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"A supervised consumer" must {
|
||||
"be able to reply during receive" in {
|
||||
val consumer = Actor.actorOf(Props(new SupervisedConsumer("reply-channel-test-1"))
|
||||
(consumer ? "succeed").get must equal("ok")
|
||||
}
|
||||
|
||||
"be able to reply on failure during preRestart" in {
|
||||
val consumer = Actor.actorOf(Props(new SupervisedConsumer("reply-channel-test-2"))
|
||||
val supervisor = Supervisor(
|
||||
SupervisorConfig(
|
||||
OneForOneStrategy(List(classOf[Exception]), 2, 10000),
|
||||
Supervise(consumer, Permanent) :: Nil))
|
||||
|
||||
val latch = new CountDownLatch(1)
|
||||
val sender = Actor.actorOf(Props(new Sender("pr", latch))
|
||||
|
||||
consumer.!("fail")(Some(sender))
|
||||
latch.await(5, TimeUnit.SECONDS) must be(true)
|
||||
}
|
||||
|
||||
"be able to reply on failure during postStop" in {
|
||||
val consumer = Actor.actorOf(Props(new SupervisedConsumer("reply-channel-test-3")))
|
||||
val supervisor = Supervisor(
|
||||
SupervisorConfig(
|
||||
OneForOneStrategy(List(classOf[Exception]), Some(0)),
|
||||
Supervise(consumer, Temporary) :: Nil))
|
||||
|
||||
val latch = new CountDownLatch(1)
|
||||
val sender = Actor.actorOf(Props(new Sender("ps", latch))
|
||||
|
||||
consumer.!("fail")(Some(sender))
|
||||
latch.await(5, TimeUnit.SECONDS) must be(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
object ConsumerScalaTest {
|
||||
trait BlockingConsumer extends Consumer { self: Actor ⇒
|
||||
override def blocking = true
|
||||
}
|
||||
|
||||
class TestConsumer(uri: String) extends Actor with Consumer {
|
||||
def endpointUri = uri
|
||||
protected def receive = {
|
||||
case msg: Message ⇒ sender ! "received %s" format msg.body
|
||||
}
|
||||
}
|
||||
|
||||
class TestBlocker(uri: String) extends Actor with BlockingConsumer {
|
||||
def endpointUri = uri
|
||||
protected def receive = {
|
||||
case msg: Message ⇒ { /* do not reply */ }
|
||||
}
|
||||
}
|
||||
|
||||
class TestAckConsumer(uri: String) extends Actor with Consumer {
|
||||
def endpointUri = uri
|
||||
override def autoack = false
|
||||
protected def receive = {
|
||||
case msg: Message ⇒ sender ! Ack
|
||||
}
|
||||
}
|
||||
|
||||
class ErrorHandlingConsumer extends Actor with BlockingConsumer {
|
||||
def endpointUri = "direct:error-handler-test"
|
||||
|
||||
onRouteDefinition { rd: RouteDefinition ⇒
|
||||
rd.onException(classOf[Exception]).handled(true).transform(Builder.exceptionMessage).end
|
||||
}
|
||||
|
||||
protected def receive = {
|
||||
case msg: Message ⇒ throw new Exception("error: %s" format msg.body)
|
||||
}
|
||||
}
|
||||
|
||||
class SupervisedConsumer(name: String) extends Actor with Consumer {
|
||||
def endpointUri = "direct:%s" format name
|
||||
|
||||
protected def receive = {
|
||||
case "fail" ⇒ { throw new Exception("test") }
|
||||
case "succeed" ⇒ sender ! "ok"
|
||||
}
|
||||
|
||||
override def preRestart(reason: scala.Throwable, msg: Option[Any]) {
|
||||
sender.tell("pr")
|
||||
}
|
||||
|
||||
override def postStop {
|
||||
sender.tell("ps")
|
||||
}
|
||||
}
|
||||
|
||||
class Sender(expected: String, latch: CountDownLatch) extends Actor {
|
||||
def receive = {
|
||||
case msg if (msg == expected) ⇒ latch.countDown
|
||||
case _ ⇒ {}
|
||||
}
|
||||
}
|
||||
|
||||
class RedeliveringConsumer extends Actor with BlockingConsumer {
|
||||
def endpointUri = "direct:redelivery-test"
|
||||
|
||||
onRouteDefinition { rd: RouteDefinition ⇒
|
||||
rd.onException(classOf[Exception]).maximumRedeliveries(1).end
|
||||
}
|
||||
|
||||
//
|
||||
// first message to this actor is not valid and will be rejected
|
||||
//
|
||||
|
||||
var valid = false
|
||||
|
||||
protected def receive = {
|
||||
case msg: Message ⇒ try {
|
||||
respondTo(msg)
|
||||
} finally {
|
||||
valid = true
|
||||
}
|
||||
}
|
||||
|
||||
private def respondTo(msg: Message) =
|
||||
if (valid) sender ! ("accepted: %s" format msg.body)
|
||||
else throw new Exception("rejected: %s" format msg.body)
|
||||
|
||||
}
|
||||
}
|
||||
59
akka-camel/src/test/scala/akka/camel/DefaultCamelTest.scala
Normal file
59
akka-camel/src/test/scala/akka/camel/DefaultCamelTest.scala
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
/**
|
||||
* Copyright (C) 2009when2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import akka.camel.TestSupport.SharedCamelSystem
|
||||
import internal.DefaultCamel
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
import org.scalatest.mock.MockitoSugar
|
||||
import akka.actor.ActorSystem
|
||||
import org.apache.camel.{ CamelContext, ProducerTemplate }
|
||||
import org.scalatest.WordSpec
|
||||
import akka.event.LoggingAdapter
|
||||
|
||||
class DefaultCamelTest extends WordSpec with SharedCamelSystem with MustMatchers with MockitoSugar {
|
||||
|
||||
import org.mockito.Mockito.{ when, verify }
|
||||
|
||||
def camelWitMocks = new DefaultCamel(mock[ActorSystem]) {
|
||||
override val log = mock[LoggingAdapter]
|
||||
override lazy val template = mock[ProducerTemplate]
|
||||
override lazy val context = mock[CamelContext]
|
||||
}
|
||||
|
||||
"during shutdown, when both context and template fail to shutdown" when {
|
||||
val camel = camelWitMocks
|
||||
|
||||
when(camel.context.stop()) thenThrow new RuntimeException("context")
|
||||
when(camel.template.stop()) thenThrow new RuntimeException("template")
|
||||
val exception = intercept[RuntimeException] {
|
||||
camel.shutdown()
|
||||
}
|
||||
|
||||
"throws exception thrown by context.stop()" in {
|
||||
exception.getMessage() must be("context");
|
||||
}
|
||||
|
||||
"tries to stop both template and context" in {
|
||||
verify(camel.template).stop()
|
||||
verify(camel.context).stop()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
"during start, if template fails to start, it will stop the context" in {
|
||||
val camel = camelWitMocks
|
||||
|
||||
when(camel.template.start()) thenThrow new RuntimeException
|
||||
|
||||
intercept[RuntimeException] {
|
||||
camel.start
|
||||
}
|
||||
|
||||
verify(camel.context).stop()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,3 +1,7 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import org.scalatest.junit.JUnitSuite
|
||||
|
|
|
|||
|
|
@ -1,94 +1,78 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import java.io.InputStream
|
||||
|
||||
import org.apache.camel.NoTypeConversionAvailableException
|
||||
import org.junit.Assert._
|
||||
import org.junit.Test
|
||||
import akka.camel.TestSupport.{ SharedCamelSystem, MessageSugar }
|
||||
import org.scalatest.FunSuite
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
|
||||
import org.scalatest.BeforeAndAfterAll
|
||||
import org.scalatest.junit.JUnitSuite
|
||||
class MessageScalaTest extends FunSuite with MustMatchers with SharedCamelSystem with MessageSugar {
|
||||
|
||||
class MessageScalaTest extends JUnitSuite with BeforeAndAfterAll {
|
||||
override protected def beforeAll = CamelContextManager.init
|
||||
|
||||
@Test
|
||||
def shouldConvertDoubleBodyToString = {
|
||||
assertEquals("1.4", Message(1.4).bodyAs[String])
|
||||
test("mustConvertDoubleBodyToString") {
|
||||
Message(1.4).bodyAs[String] must be("1.4")
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldThrowExceptionWhenConvertingDoubleBodyToInputStream {
|
||||
test("mustThrowExceptionWhenConvertingDoubleBodyToInputStream") {
|
||||
intercept[NoTypeConversionAvailableException] {
|
||||
Message(1.4).bodyAs[InputStream]
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldReturnDoubleHeader = {
|
||||
test("mustReturnDoubleHeader") {
|
||||
val message = Message("test", Map("test" -> 1.4))
|
||||
assertEquals(1.4, message.header("test"))
|
||||
message.header("test").get must be(1.4)
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldConvertDoubleHeaderToString = {
|
||||
test("mustConvertDoubleHeaderToString") {
|
||||
val message = Message("test", Map("test" -> 1.4))
|
||||
assertEquals("1.4", message.headerAs[String]("test"))
|
||||
message.headerAs[String]("test").get must be("1.4")
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldReturnSubsetOfHeaders = {
|
||||
test("mustReturnSubsetOfHeaders") {
|
||||
val message = Message("test", Map("A" -> "1", "B" -> "2"))
|
||||
assertEquals(Map("B" -> "2"), message.headers(Set("B")))
|
||||
message.headers(Set("B")) must be(Map("B" -> "2"))
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldTransformBodyAndPreserveHeaders = {
|
||||
assertEquals(
|
||||
Message("ab", Map("A" -> "1")),
|
||||
Message("a", Map("A" -> "1")).transformBody((body: String) ⇒ body + "b"))
|
||||
test("mustTransformBodyAndPreserveHeaders") {
|
||||
Message("a", Map("A" -> "1")).mapBody((body: String) ⇒ body + "b") must be(Message("ab", Map("A" -> "1")))
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldConvertBodyAndPreserveHeaders = {
|
||||
assertEquals(
|
||||
Message("1.4", Map("A" -> "1")),
|
||||
Message(1.4, Map("A" -> "1")).setBodyAs[String])
|
||||
test("mustConvertBodyAndPreserveHeaders") {
|
||||
Message(1.4, Map("A" -> "1")).withBodyAs[String] must be(Message("1.4", Map("A" -> "1")))
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldSetBodyAndPreserveHeaders = {
|
||||
assertEquals(
|
||||
Message("test2", Map("A" -> "1")),
|
||||
Message("test1", Map("A" -> "1")).setBody("test2"))
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldSetHeadersAndPreserveBody = {
|
||||
assertEquals(
|
||||
Message("test1", Map("C" -> "3")),
|
||||
Message("test1", Map("A" -> "1")).setHeaders(Map("C" -> "3")))
|
||||
test("mustSetBodyAndPreserveHeaders") {
|
||||
Message("test1", Map("A" -> "1")).withBody("test2") must be(
|
||||
Message("test2", Map("A" -> "1")))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldAddHeaderAndPreserveBodyAndHeaders = {
|
||||
assertEquals(
|
||||
Message("test1", Map("A" -> "1", "B" -> "2")),
|
||||
Message("test1", Map("A" -> "1")).addHeader("B" -> "2"))
|
||||
test("mustSetHeadersAndPreserveBody") {
|
||||
Message("test1", Map("A" -> "1")).withHeaders(Map("C" -> "3")) must be(
|
||||
Message("test1", Map("C" -> "3")))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldAddHeadersAndPreserveBodyAndHeaders = {
|
||||
assertEquals(
|
||||
Message("test1", Map("A" -> "1", "B" -> "2")),
|
||||
Message("test1", Map("A" -> "1")).addHeaders(Map("B" -> "2")))
|
||||
test("mustAddHeaderAndPreserveBodyAndHeaders") {
|
||||
Message("test1", Map("A" -> "1")).plusHeader("B" -> "2") must be(
|
||||
Message("test1", Map("A" -> "1", "B" -> "2")))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldRemoveHeadersAndPreserveBodyAndRemainingHeaders = {
|
||||
assertEquals(
|
||||
Message("test1", Map("A" -> "1")),
|
||||
Message("test1", Map("A" -> "1", "B" -> "2")).removeHeader("B"))
|
||||
test("mustAddHeadersAndPreserveBodyAndHeaders") {
|
||||
Message("test1", Map("A" -> "1")).plusHeaders(Map("B" -> "2")) must be(
|
||||
Message("test1", Map("A" -> "1", "B" -> "2")))
|
||||
|
||||
}
|
||||
|
||||
test("mustRemoveHeadersAndPreserveBodyAndRemainingHeaders") {
|
||||
Message("test1", Map("A" -> "1", "B" -> "2")).withoutHeader("B") must be(
|
||||
Message("test1", Map("A" -> "1")))
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
47
akka-camel/src/test/scala/akka/camel/MessageTest.scala
Normal file
47
akka-camel/src/test/scala/akka/camel/MessageTest.scala
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import org.apache.camel.impl.{ DefaultExchange, DefaultMessage }
|
||||
import org.apache.camel.{ Message ⇒ CamelMessage }
|
||||
import akka.camel.TestSupport.SharedCamelSystem
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
import org.scalatest.WordSpec
|
||||
|
||||
//TODO merge it with MessageScalaTest
|
||||
class MessageTest extends MustMatchers with WordSpec with SharedCamelSystem {
|
||||
|
||||
"Message" must {
|
||||
|
||||
"overwrite body and add header" in {
|
||||
val msg = sampleMessage
|
||||
Message("blah", Map("key" -> "baz")).copyContentTo(msg)
|
||||
assert(msg.getBody === "blah")
|
||||
assert(msg.getHeader("foo") === "bar")
|
||||
assert(msg.getHeader("key") === "baz")
|
||||
}
|
||||
|
||||
"create message with body and header" in {
|
||||
val m = Message.from(sampleMessage)
|
||||
assert(m.body === "test")
|
||||
assert(m.headers("foo") === "bar")
|
||||
}
|
||||
|
||||
"create message with body and header and custom header" in {
|
||||
val m = Message.from(sampleMessage, Map("key" -> "baz"))
|
||||
assert(m.body === "test")
|
||||
assert(m.headers("foo") === "bar")
|
||||
assert(m.headers("key") === "baz")
|
||||
}
|
||||
}
|
||||
|
||||
private[camel] def sampleMessage = {
|
||||
val message = new DefaultMessage
|
||||
message.setBody("test")
|
||||
message.setHeader("foo", "bar")
|
||||
message.setExchange(new DefaultExchange(camel.context))
|
||||
message
|
||||
}
|
||||
}
|
||||
|
|
@ -1,250 +1,289 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import org.apache.camel.{ Exchange, Processor }
|
||||
import org.apache.camel.builder.RouteBuilder
|
||||
import org.apache.camel.component.mock.MockEndpoint
|
||||
import org.scalatest.{ GivenWhenThen, BeforeAndAfterEach, BeforeAndAfterAll, FeatureSpec }
|
||||
import akka.actor._
|
||||
import akka.pattern._
|
||||
import akka.dispatch.Await
|
||||
import akka.util.duration._
|
||||
import akka.camel.TestSupport.SharedCamelSystem
|
||||
|
||||
import akka.actor.Actor._
|
||||
import akka.actor.{ ActorRef, Actor }
|
||||
/**
|
||||
* Tests the features of the Camel Producer.
|
||||
*/
|
||||
class ProducerFeatureTest extends FeatureSpec with BeforeAndAfterAll with BeforeAndAfterEach with SharedCamelSystem with GivenWhenThen {
|
||||
|
||||
class ProducerFeatureTest extends FeatureSpec with BeforeAndAfterAll with BeforeAndAfterEach with GivenWhenThen {
|
||||
import ProducerFeatureTest._
|
||||
|
||||
override protected def beforeAll = {
|
||||
Actor.registry.local.shutdownAll
|
||||
CamelContextManager.init
|
||||
CamelContextManager.mandatoryContext.addRoutes(new TestRoute)
|
||||
CamelContextManager.start
|
||||
}
|
||||
val camelContext = camel.context
|
||||
// to make testing equality of messages easier, otherwise the breadcrumb shows up in the result.
|
||||
camelContext.setUseBreadcrumb(false)
|
||||
|
||||
override protected def afterAll = {
|
||||
CamelContextManager.stop
|
||||
Actor.registry.local.shutdownAll
|
||||
}
|
||||
val timeout = 1 second
|
||||
|
||||
override protected def afterEach = {
|
||||
mockEndpoint.reset
|
||||
}
|
||||
override protected def beforeAll { camelContext.addRoutes(new TestRoute(system)) }
|
||||
|
||||
feature("Produce a message to a sync Camel route") {
|
||||
override protected def afterEach { mockEndpoint.reset() }
|
||||
|
||||
scenario("produce message and receive normal response") {
|
||||
feature("Producer on a sync Camel route") {
|
||||
|
||||
scenario("produces a message and receives normal response") {
|
||||
given("a registered two-way producer")
|
||||
val producer = actorOf(Props(new TestProducer("direct:producer-test-2", true))
|
||||
|
||||
val producer = system.actorOf(Props(new TestProducer("direct:producer-test-2", true)))
|
||||
when("a test message is sent to the producer with ?")
|
||||
val message = Message("test", Map(Message.MessageExchangeId -> "123"))
|
||||
val result = (producer ? message).get
|
||||
|
||||
then("a normal response should have been returned by the producer")
|
||||
val future = producer.ask(message)(timeout)
|
||||
then("a normal response must have been returned by the producer")
|
||||
val expected = Message("received TEST", Map(Message.MessageExchangeId -> "123"))
|
||||
assert(result === expected)
|
||||
Await.result(future, timeout) match {
|
||||
case result: Message ⇒ assert(result === expected)
|
||||
case unexpected ⇒ fail("Actor responded with unexpected message:" + unexpected)
|
||||
}
|
||||
}
|
||||
|
||||
scenario("produce message and receive failure response") {
|
||||
scenario("produces a message and receives failure response") {
|
||||
given("a registered two-way producer")
|
||||
val producer = actorOf(Props(new TestProducer("direct:producer-test-2"))
|
||||
val producer = system.actorOf(Props(new TestProducer("direct:producer-test-2")))
|
||||
|
||||
when("a test message causing an exception is sent to the producer with ?")
|
||||
val message = Message("fail", Map(Message.MessageExchangeId -> "123"))
|
||||
val result = (producer ? message).as[Failure]
|
||||
|
||||
then("a failure response should have been returned by the producer")
|
||||
val expectedFailureText = result.get.cause.getMessage
|
||||
val expectedHeaders = result.get.headers
|
||||
assert(expectedFailureText === "failure")
|
||||
assert(expectedHeaders === Map(Message.MessageExchangeId -> "123"))
|
||||
val future = producer.ask(message)(timeout)
|
||||
Await.result(future, timeout) match {
|
||||
case result: Failure ⇒ {
|
||||
then("a failure response must have been returned by the producer")
|
||||
val expectedFailureText = result.cause.getMessage
|
||||
val expectedHeaders = result.headers
|
||||
assert(expectedFailureText === "failure")
|
||||
assert(expectedHeaders === Map(Message.MessageExchangeId -> "123"))
|
||||
}
|
||||
case unexpected ⇒ fail("Actor responded with unexpected message:" + unexpected)
|
||||
}
|
||||
}
|
||||
|
||||
scenario("produce message oneway") {
|
||||
scenario("produces message oneway") {
|
||||
given("a registered one-way producer")
|
||||
val producer = actorOf(Props(new TestProducer("direct:producer-test-1", true) with Oneway)
|
||||
val producer = system.actorOf(Props(new TestProducer("direct:producer-test-1", true) with Oneway))
|
||||
|
||||
when("a test message is sent to the producer with !")
|
||||
mockEndpoint.expectedBodiesReceived("TEST")
|
||||
producer ! Message("test")
|
||||
producer ! Message("test", Map())
|
||||
|
||||
then("the test message should have been sent to mock:mock")
|
||||
mockEndpoint.assertIsSatisfied
|
||||
then("the test message must have been sent to mock:mock")
|
||||
mockEndpoint.assertIsSatisfied()
|
||||
}
|
||||
|
||||
scenario("produce message twoway without sender reference") {
|
||||
scenario("produces message twoway without sender reference") {
|
||||
given("a registered two-way producer")
|
||||
val producer = actorOf(Props(new TestProducer("direct:producer-test-1"))
|
||||
val producer = system.actorOf(Props(new TestProducer("direct:producer-test-1")))
|
||||
|
||||
when("a test message is sent to the producer with !")
|
||||
mockEndpoint.expectedBodiesReceived("test")
|
||||
producer ! Message("test")
|
||||
producer ! Message("test", Map())
|
||||
|
||||
then("there should be only a warning that there's no sender reference")
|
||||
mockEndpoint.assertIsSatisfied
|
||||
then("there must be only a warning that there's no sender reference")
|
||||
mockEndpoint.assertIsSatisfied()
|
||||
}
|
||||
}
|
||||
|
||||
feature("Produce a message to an async Camel route") {
|
||||
feature("Producer on an async Camel route") {
|
||||
|
||||
scenario("produce message and receive normal response") {
|
||||
scenario("produces message to direct:producer-test-3 and receives normal response") {
|
||||
given("a registered two-way producer")
|
||||
val producer = actorOf(Props(new TestProducer("direct:producer-test-3"))
|
||||
val producer = system.actorOf(Props(new TestProducer("direct:producer-test-3")))
|
||||
|
||||
when("a test message is sent to the producer with ?")
|
||||
val message = Message("test", Map(Message.MessageExchangeId -> "123"))
|
||||
val result = (producer ? message).as[Message].get
|
||||
val future = producer.ask(message)(timeout)
|
||||
|
||||
then("a normal response should have been returned by the producer")
|
||||
assert(result.headers(Message.MessageExchangeId) === "123")
|
||||
Await.result(future, timeout) match {
|
||||
case result: Message ⇒ {
|
||||
then("a normal response must have been returned by the producer")
|
||||
val expected = Message("received test", Map(Message.MessageExchangeId -> "123"))
|
||||
assert(result === expected)
|
||||
}
|
||||
case unexpected ⇒ fail("Actor responded with unexpected message:" + unexpected)
|
||||
}
|
||||
}
|
||||
|
||||
scenario("produce message and receive failure response") {
|
||||
scenario("produces message to direct:producer-test-3 and receives failure response") {
|
||||
given("a registered two-way producer")
|
||||
val producer = actorOf(Props(new TestProducer("direct:producer-test-3"))
|
||||
val producer = system.actorOf(Props(new TestProducer("direct:producer-test-3")))
|
||||
|
||||
when("a test message causing an exception is sent to the producer with ?")
|
||||
val message = Message("fail", Map(Message.MessageExchangeId -> "123"))
|
||||
val result = (producer ? message).as[Failure]
|
||||
|
||||
then("a failure response should have been returned by the producer")
|
||||
val expectedFailureText = result.get.cause.getMessage
|
||||
val expectedHeaders = result.get.headers
|
||||
assert(expectedFailureText === "failure")
|
||||
assert(expectedHeaders === Map(Message.MessageExchangeId -> "123"))
|
||||
val future = producer.ask(message)(timeout)
|
||||
Await.result(future, timeout) match {
|
||||
case result: Failure ⇒ {
|
||||
then("a failure response must have been returned by the producer")
|
||||
val expectedFailureText = result.cause.getMessage
|
||||
val expectedHeaders = result.headers
|
||||
assert(expectedFailureText === "failure")
|
||||
assert(expectedHeaders === Map(Message.MessageExchangeId -> "123"))
|
||||
}
|
||||
case unexpected ⇒ fail("Actor responded with unexpected message:" + unexpected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
feature("Produce a message to a sync Camel route and then forward the response") {
|
||||
|
||||
scenario("produce message, forward normal response to a replying target actor and receive response") {
|
||||
scenario("produces message, forwards normal response of direct:producer-test-2 to a replying target actor and receives response") {
|
||||
given("a registered two-way producer configured with a forward target")
|
||||
val target = actorOf(Props[ReplyingForwardTarget]
|
||||
val producer = actorOf(Props(new TestForwarder("direct:producer-test-2", target))
|
||||
val target = system.actorOf(Props[ReplyingForwardTarget])
|
||||
val producer = system.actorOf(Props(new TestForwarder("direct:producer-test-2", target)))
|
||||
|
||||
when("a test message is sent to the producer with ?")
|
||||
val message = Message("test", Map(Message.MessageExchangeId -> "123"))
|
||||
val result = (producer ? message).get
|
||||
val future = producer.ask(message)(timeout)
|
||||
|
||||
then("a normal response should have been returned by the forward target")
|
||||
val expected = Message("received test", Map(Message.MessageExchangeId -> "123", "test" -> "result"))
|
||||
assert(result === expected)
|
||||
Await.result(future, timeout) match {
|
||||
case result: Message ⇒ {
|
||||
then("a normal response must have been returned by the forward target")
|
||||
val expected = Message("received test", Map(Message.MessageExchangeId -> "123", "test" -> "result"))
|
||||
assert(result === expected)
|
||||
}
|
||||
case unexpected ⇒ fail("Actor responded with unexpected message:" + unexpected)
|
||||
}
|
||||
}
|
||||
|
||||
scenario("produce message, forward failure response to a replying target actor and receive response") {
|
||||
scenario("produces message, forwards failure response of direct:producer-test-2 to a replying target actor and receives response") {
|
||||
given("a registered two-way producer configured with a forward target")
|
||||
val target = actorOf(Props[ReplyingForwardTarget]
|
||||
val producer = actorOf(Props(new TestForwarder("direct:producer-test-2", target))
|
||||
val target = system.actorOf(Props[ReplyingForwardTarget])
|
||||
val producer = system.actorOf(Props(new TestForwarder("direct:producer-test-2", target)))
|
||||
|
||||
when("a test message causing an exception is sent to the producer with ?")
|
||||
val message = Message("fail", Map(Message.MessageExchangeId -> "123"))
|
||||
val result = (producer ? message).as[Failure].get
|
||||
|
||||
then("a failure response should have been returned by the forward target")
|
||||
val expectedFailureText = result.cause.getMessage
|
||||
val expectedHeaders = result.headers
|
||||
assert(expectedFailureText === "failure")
|
||||
assert(expectedHeaders === Map(Message.MessageExchangeId -> "123", "test" -> "failure"))
|
||||
val future = producer.ask(message)(timeout)
|
||||
Await.result(future, timeout) match {
|
||||
case failure: Failure ⇒ {
|
||||
then("a failure response must have been returned by the forward target")
|
||||
val expectedFailureText = failure.cause.getMessage
|
||||
val expectedHeaders = failure.headers
|
||||
assert(expectedFailureText === "failure")
|
||||
assert(expectedHeaders === Map(Message.MessageExchangeId -> "123", "test" -> "failure"))
|
||||
}
|
||||
case unexpected ⇒ fail("Actor responded with unexpected message:" + unexpected)
|
||||
}
|
||||
}
|
||||
|
||||
scenario("produce message, forward normal response to a producing target actor and produce response to direct:forward-test-1") {
|
||||
scenario("produces message, forwards normal response to a producing target actor and produces response to direct:forward-test-1") {
|
||||
given("a registered one-way producer configured with a forward target")
|
||||
val target = actorOf(Props[ProducingForwardTarget]
|
||||
val producer = actorOf(Props(new TestForwarder("direct:producer-test-2", target))
|
||||
val target = system.actorOf(Props[ProducingForwardTarget])
|
||||
val producer = system.actorOf(Props(new TestForwarder("direct:producer-test-2", target)))
|
||||
|
||||
when("a test message is sent to the producer with !")
|
||||
mockEndpoint.expectedBodiesReceived("received test")
|
||||
val result = producer.!(Message("test"))(Some(producer))
|
||||
producer.tell(Message("test", Map()), producer)
|
||||
|
||||
then("a normal response should have been produced by the forward target")
|
||||
mockEndpoint.assertIsSatisfied
|
||||
then("a normal response must have been produced by the forward target")
|
||||
mockEndpoint.assertIsSatisfied()
|
||||
}
|
||||
|
||||
scenario("produce message, forward failure response to a producing target actor and produce response to direct:forward-test-1") {
|
||||
scenario("produces message, forwards failure response to a producing target actor and produces response to direct:forward-test-1") {
|
||||
given("a registered one-way producer configured with a forward target")
|
||||
val target = actorOf(Props[ProducingForwardTarget]
|
||||
val producer = actorOf(Props(new TestForwarder("direct:producer-test-2", target))
|
||||
|
||||
val target = system.actorOf(Props[ProducingForwardTarget])
|
||||
val producer = system.actorOf(Props(new TestForwarder("direct:producer-test-2", target)))
|
||||
|
||||
when("a test message causing an exception is sent to the producer with !")
|
||||
mockEndpoint.expectedMessageCount(1)
|
||||
mockEndpoint.message(0).body().isInstanceOf(classOf[Failure])
|
||||
val result = producer.!(Message("fail"))(Some(producer))
|
||||
producer.tell(Message("fail", Map()), producer)
|
||||
|
||||
then("a failure response should have been produced by the forward target")
|
||||
mockEndpoint.assertIsSatisfied
|
||||
then("a failure response must have been produced by the forward target")
|
||||
mockEndpoint.assertIsSatisfied()
|
||||
}
|
||||
}
|
||||
|
||||
feature("Produce a message to an async Camel route and then forward the response") {
|
||||
|
||||
scenario("produce message, forward normal response to a replying target actor and receive response") {
|
||||
scenario("produces message, forwards normal response from direct:producer-test-3 to a replying target actor and receives response") {
|
||||
given("a registered two-way producer configured with a forward target")
|
||||
val target = actorOf(Props[ReplyingForwardTarget]
|
||||
val producer = actorOf(Props(new TestForwarder("direct:producer-test-3", target))
|
||||
val target = system.actorOf(Props[ReplyingForwardTarget])
|
||||
val producer = system.actorOf(Props(new TestForwarder("direct:producer-test-3", target)))
|
||||
|
||||
when("a test message is sent to the producer with ?")
|
||||
val message = Message("test", Map(Message.MessageExchangeId -> "123"))
|
||||
val result = (producer ? message).as[Message].get
|
||||
|
||||
then("a normal response should have been returned by the forward target")
|
||||
assert(result.headers(Message.MessageExchangeId) === "123")
|
||||
assert(result.headers("test") === "result")
|
||||
val future = producer.ask(message)(timeout)
|
||||
|
||||
then("a normal response must have been returned by the forward target")
|
||||
Await.result(future, timeout) match {
|
||||
case message: Message ⇒ {
|
||||
val expected = Message("received test", Map(Message.MessageExchangeId -> "123", "test" -> "result"))
|
||||
assert(message === expected)
|
||||
}
|
||||
case unexpected ⇒ fail("Actor responded with unexpected message:" + unexpected)
|
||||
}
|
||||
}
|
||||
|
||||
scenario("produce message, forward failure response to a replying target actor and receive response") {
|
||||
scenario("produces message, forwards failure response from direct:producer-test-3 to a replying target actor and receives response") {
|
||||
given("a registered two-way producer configured with a forward target")
|
||||
val target = actorOf(Props[ReplyingForwardTarget]
|
||||
val producer = actorOf(Props(new TestForwarder("direct:producer-test-3", target))
|
||||
val target = system.actorOf(Props[ReplyingForwardTarget])
|
||||
val producer = system.actorOf(Props(new TestForwarder("direct:producer-test-3", target)))
|
||||
|
||||
when("a test message causing an exception is sent to the producer with ?")
|
||||
when("a test message causing an exception is sent to the producer with !!")
|
||||
val message = Message("fail", Map(Message.MessageExchangeId -> "123"))
|
||||
val result = (producer ? message).as[Failure]
|
||||
|
||||
then("a failure response should have been returned by the forward target")
|
||||
val expectedFailureText = result.get.cause.getMessage
|
||||
val expectedHeaders = result.get.headers
|
||||
assert(expectedFailureText === "failure")
|
||||
assert(expectedHeaders === Map(Message.MessageExchangeId -> "123", "test" -> "failure"))
|
||||
val future = producer.ask(message)(timeout)
|
||||
Await.result(future, timeout) match {
|
||||
case failure: Failure ⇒ {
|
||||
then("a failure response must have been returned by the forward target")
|
||||
val expectedFailureText = failure.cause.getMessage
|
||||
val expectedHeaders = failure.headers
|
||||
assert(expectedFailureText === "failure")
|
||||
assert(expectedHeaders === Map(Message.MessageExchangeId -> "123", "test" -> "failure"))
|
||||
}
|
||||
case unexpected ⇒ fail("Actor responded with unexpected message:" + unexpected)
|
||||
}
|
||||
}
|
||||
|
||||
scenario("produce message, forward normal response to a producing target actor and produce response to direct:forward-test-1") {
|
||||
scenario("produces message, forwards normal response from direct:producer-test-3 to a producing target actor and produces response to direct:forward-test-1") {
|
||||
given("a registered one-way producer configured with a forward target")
|
||||
val target = actorOf(Props[ProducingForwardTarget]
|
||||
val producer = actorOf(Props(new TestForwarder("direct:producer-test-3", target))
|
||||
val target = system.actorOf(Props[ProducingForwardTarget])
|
||||
val producer = system.actorOf(Props(new TestForwarder("direct:producer-test-3", target)))
|
||||
|
||||
when("a test message is sent to the producer with !")
|
||||
mockEndpoint.expectedBodiesReceived("received test")
|
||||
val result = producer.!(Message("test"))(Some(producer))
|
||||
producer.tell(Message("test", Map()), producer)
|
||||
|
||||
then("a normal response should have been produced by the forward target")
|
||||
mockEndpoint.assertIsSatisfied
|
||||
then("a normal response must have been produced by the forward target")
|
||||
mockEndpoint.assertIsSatisfied()
|
||||
}
|
||||
|
||||
scenario("produce message, forward failure response to a producing target actor and produce response to direct:forward-test-1") {
|
||||
scenario("produces message, forwards failure response from direct:producer-test-3 to a producing target actor and produces response to direct:forward-test-1") {
|
||||
given("a registered one-way producer configured with a forward target")
|
||||
val target = actorOf(Props[ProducingForwardTarget]
|
||||
val producer = actorOf(Props(new TestForwarder("direct:producer-test-3", target))
|
||||
val target = system.actorOf(Props[ProducingForwardTarget])
|
||||
val producer = system.actorOf(Props(new TestForwarder("direct:producer-test-3", target)))
|
||||
|
||||
when("a test message causing an exception is sent to the producer with !")
|
||||
mockEndpoint.expectedMessageCount(1)
|
||||
mockEndpoint.message(0).body().isInstanceOf(classOf[Failure])
|
||||
val result = producer.!(Message("fail"))(Some(producer))
|
||||
producer.tell(Message("fail", Map()), producer)
|
||||
|
||||
then("a failure response should have been produced by the forward target")
|
||||
mockEndpoint.assertIsSatisfied
|
||||
then("a failure response must have been produced by the forward target")
|
||||
mockEndpoint.assertIsSatisfied()
|
||||
}
|
||||
}
|
||||
|
||||
private def mockEndpoint = CamelContextManager.mandatoryContext.getEndpoint("mock:mock", classOf[MockEndpoint])
|
||||
private def mockEndpoint = camel.context.getEndpoint("mock:mock", classOf[MockEndpoint])
|
||||
}
|
||||
|
||||
object ProducerFeatureTest {
|
||||
|
||||
class TestProducer(uri: String, upper: Boolean = false) extends Actor with Producer {
|
||||
def endpointUri = uri
|
||||
|
||||
override protected def receiveBeforeProduce = {
|
||||
case msg: Message ⇒ if (upper) msg.transformBody { body: String ⇒ body.toUpperCase } else msg
|
||||
case msg: Message ⇒ if (upper) msg.mapBody {
|
||||
body: String ⇒ body.toUpperCase
|
||||
}
|
||||
else msg
|
||||
}
|
||||
}
|
||||
|
||||
class TestForwarder(uri: String, target: ActorRef) extends Actor with Producer {
|
||||
def endpointUri = uri
|
||||
|
||||
override protected def receiveAfterProduce = {
|
||||
case msg ⇒ target forward msg
|
||||
}
|
||||
|
|
@ -253,16 +292,24 @@ object ProducerFeatureTest {
|
|||
class TestResponder extends Actor {
|
||||
protected def receive = {
|
||||
case msg: Message ⇒ msg.body match {
|
||||
case "fail" ⇒ sender ! Failure(new Exception("failure"), msg.headers)
|
||||
case _ ⇒ sender ! (msg.transformBody { body: String ⇒ "received %s" format body })
|
||||
case "fail" ⇒ {
|
||||
context.sender ! (Failure(new Exception("failure"), msg.headers))
|
||||
}
|
||||
case bod: Any ⇒ {
|
||||
context.sender ! (msg.mapBody {
|
||||
body: String ⇒ "received %s" format body
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ReplyingForwardTarget extends Actor {
|
||||
protected def receive = {
|
||||
case msg: Message ⇒ sender ! msg.addHeader("test" -> "result")
|
||||
case msg: Failure ⇒ sender ! Failure(msg.cause, msg.headers + ("test" -> "failure"))
|
||||
case msg: Message ⇒
|
||||
context.sender ! (msg.plusHeader("test" -> "result"))
|
||||
case msg: Failure ⇒
|
||||
context.sender ! (Failure(msg.cause, msg.headers + ("test" -> "failure")))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -270,14 +317,15 @@ object ProducerFeatureTest {
|
|||
def endpointUri = "direct:forward-test-1"
|
||||
}
|
||||
|
||||
class TestRoute extends RouteBuilder {
|
||||
val responder = actorOf(Props[TestResponder]
|
||||
class TestRoute(system: ActorSystem) extends RouteBuilder {
|
||||
val responder = system.actorOf(Props[TestResponder], name = "TestResponder")
|
||||
|
||||
def configure {
|
||||
from("direct:forward-test-1").to("mock:mock")
|
||||
// for one-way messaging tests
|
||||
from("direct:producer-test-1").to("mock:mock")
|
||||
// for two-way messaging tests (async)
|
||||
from("direct:producer-test-3").to("actor:uuid:%s" format responder.uuid)
|
||||
from("direct:producer-test-3").to(responder)
|
||||
// for two-way messaging tests (sync)
|
||||
from("direct:producer-test-2").process(new Processor() {
|
||||
def process(exchange: Exchange) = {
|
||||
|
|
@ -289,4 +337,5 @@ object ProducerFeatureTest {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
import org.scalatest.WordSpec
|
||||
import akka.camel.TestSupport.SharedCamelSystem
|
||||
import akka.util.duration._
|
||||
import akka.actor.{ ActorRef, Props }
|
||||
|
||||
class ProducerRegistryTest extends WordSpec with MustMatchers with SharedCamelSystem {
|
||||
|
||||
"A ProducerRegistry" must {
|
||||
def newEmptyActor: ActorRef = system.actorOf(Props.empty)
|
||||
def registerProcessorFor(actorRef: ActorRef) = camel.registerProducer(actorRef, "mock:mock")._2
|
||||
|
||||
"register a started SendProcessor for the producer, which is stopped when the actor is stopped" in {
|
||||
val actorRef = newEmptyActor
|
||||
val processor = registerProcessorFor(actorRef)
|
||||
camel.awaitActivation(actorRef, 1 second)
|
||||
processor.isStarted must be(true)
|
||||
system.stop(actorRef)
|
||||
camel.awaitDeactivation(actorRef, 1 second)
|
||||
(processor.isStopping || processor.isStopped) must be(true)
|
||||
}
|
||||
"remove and stop the SendProcessor if the actorRef is registered" in {
|
||||
val actorRef = newEmptyActor
|
||||
val processor = registerProcessorFor(actorRef)
|
||||
camel.unregisterProducer(actorRef)
|
||||
(processor.isStopping || processor.isStopped) must be(true)
|
||||
}
|
||||
"remove and stop only the SendProcessor for the actorRef that is registered" in {
|
||||
val actorRef1 = newEmptyActor
|
||||
val actorRef2 = newEmptyActor
|
||||
val processor = registerProcessorFor(actorRef2)
|
||||
// this generates a warning on the activationTracker.
|
||||
camel.unregisterProducer(actorRef1)
|
||||
(!processor.isStopped && !processor.isStopping) must be(true)
|
||||
|
||||
camel.unregisterProducer(actorRef2)
|
||||
(processor.isStopping || processor.isStopped) must be(true)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
90
akka-camel/src/test/scala/akka/camel/TestSupport.scala
Normal file
90
akka-camel/src/test/scala/akka/camel/TestSupport.scala
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import akka.actor.{ Props, ActorSystem, Actor }
|
||||
import akka.util.duration._
|
||||
import java.util.concurrent.{ TimeoutException, ExecutionException, TimeUnit }
|
||||
import org.scalatest.{ BeforeAndAfterEach, BeforeAndAfterAll, Suite }
|
||||
import org.scalatest.matchers.{ BePropertyMatcher, BePropertyMatchResult }
|
||||
import akka.util.{ FiniteDuration, Duration }
|
||||
|
||||
private[camel] object TestSupport {
|
||||
|
||||
def start(actor: ⇒ Actor)(implicit system: ActorSystem) = {
|
||||
val actorRef = system.actorOf(Props(actor))
|
||||
CamelExtension(system).awaitActivation(actorRef, 1 second)
|
||||
actorRef
|
||||
}
|
||||
|
||||
private[camel] implicit def camelToTestWrapper(camel: Camel) = new CamelTestWrapper(camel)
|
||||
|
||||
class CamelTestWrapper(camel: Camel) {
|
||||
/**
|
||||
* Sends msg to the endpoint and returns response.
|
||||
* It only waits for the response until timeout passes.
|
||||
* This is to reduce cases when unit-tests block infinitely.
|
||||
*/
|
||||
def sendTo(to: String, msg: String, timeout: Duration = 1 second): AnyRef = {
|
||||
try {
|
||||
camel.template.asyncRequestBody(to, msg).get(timeout.toNanos, TimeUnit.NANOSECONDS)
|
||||
} catch {
|
||||
case e: ExecutionException ⇒ throw e.getCause
|
||||
case e: TimeoutException ⇒ throw new AssertionError("Failed to get response to message [%s], send to endpoint [%s], within [%s]" format (msg, to, timeout), e)
|
||||
}
|
||||
}
|
||||
|
||||
def routeCount = camel.context.getRoutes().size()
|
||||
}
|
||||
|
||||
@deprecated
|
||||
trait MessageSugar {
|
||||
def Message(body: Any) = akka.camel.Message(body, Map.empty)
|
||||
def Message(body: Any, headers: Map[String, Any]) = akka.camel.Message(body, headers)
|
||||
}
|
||||
|
||||
trait SharedCamelSystem extends BeforeAndAfterAll { this: Suite ⇒
|
||||
implicit lazy val system = ActorSystem("test")
|
||||
implicit lazy val camel = CamelExtension(system)
|
||||
|
||||
abstract override protected def afterAll() {
|
||||
super.afterAll()
|
||||
system.shutdown()
|
||||
}
|
||||
}
|
||||
|
||||
trait NonSharedCamelSystem extends BeforeAndAfterEach { this: Suite ⇒
|
||||
implicit var system: ActorSystem = _
|
||||
implicit var camel: Camel = _
|
||||
|
||||
override protected def beforeEach() {
|
||||
super.beforeEach()
|
||||
system = ActorSystem("test")
|
||||
camel = CamelExtension(system)
|
||||
}
|
||||
|
||||
override protected def afterEach() {
|
||||
system.shutdown()
|
||||
super.afterEach()
|
||||
}
|
||||
|
||||
}
|
||||
def time[A](block: ⇒ A): FiniteDuration = {
|
||||
val start = System.currentTimeMillis()
|
||||
block
|
||||
val duration = System.currentTimeMillis() - start
|
||||
duration millis
|
||||
}
|
||||
|
||||
def anInstanceOf[T](implicit manifest: Manifest[T]) = {
|
||||
val clazz = manifest.erasure.asInstanceOf[Class[T]]
|
||||
new BePropertyMatcher[AnyRef] {
|
||||
def apply(left: AnyRef) = BePropertyMatchResult(
|
||||
clazz.isAssignableFrom(left.getClass),
|
||||
"an instance of " + clazz.getName)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,94 +0,0 @@
|
|||
package akka.camel
|
||||
|
||||
import org.apache.camel.{ Exchange, Processor }
|
||||
import org.apache.camel.builder.RouteBuilder
|
||||
import org.apache.camel.component.mock.MockEndpoint
|
||||
import org.scalatest.{ GivenWhenThen, BeforeAndAfterEach, BeforeAndAfterAll, FeatureSpec }
|
||||
|
||||
import akka.actor.Actor._
|
||||
|
||||
class UntypedProducerFeatureTest extends FeatureSpec with BeforeAndAfterAll with BeforeAndAfterEach with GivenWhenThen {
|
||||
import UntypedProducerFeatureTest._
|
||||
|
||||
override protected def beforeAll = {
|
||||
registry.local.shutdownAll
|
||||
CamelContextManager.init
|
||||
CamelContextManager.mandatoryContext.addRoutes(new TestRoute)
|
||||
CamelContextManager.start
|
||||
}
|
||||
|
||||
override protected def afterAll = {
|
||||
CamelContextManager.stop
|
||||
registry.local.shutdownAll
|
||||
}
|
||||
|
||||
override protected def afterEach = {
|
||||
mockEndpoint.reset
|
||||
}
|
||||
|
||||
feature("Produce a message to a sync Camel route") {
|
||||
|
||||
scenario("produce message and receive normal response") {
|
||||
given("a registered two-way producer")
|
||||
val producer = actorOf(classOf[SampleUntypedReplyingProducer])
|
||||
|
||||
when("a test message is sent to the producer with ?")
|
||||
val message = Message("test", Map(Message.MessageExchangeId -> "123"))
|
||||
val result = producer.ask(message).get
|
||||
|
||||
then("a normal response should have been returned by the producer")
|
||||
val expected = Message("received test", Map(Message.MessageExchangeId -> "123"))
|
||||
assert(result === expected)
|
||||
}
|
||||
|
||||
scenario("produce message and receive failure response") {
|
||||
given("a registered two-way producer")
|
||||
val producer = actorOf(classOf[SampleUntypedReplyingProducer])
|
||||
|
||||
when("a test message causing an exception is sent to the producer with ?")
|
||||
val message = Message("fail", Map(Message.MessageExchangeId -> "123"))
|
||||
val result = producer.ask(message).as[Failure].get
|
||||
|
||||
then("a failure response should have been returned by the producer")
|
||||
val expectedFailureText = result.cause.getMessage
|
||||
val expectedHeaders = result.headers
|
||||
assert(expectedFailureText === "failure")
|
||||
assert(expectedHeaders === Map(Message.MessageExchangeId -> "123"))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
feature("Produce a message to a sync Camel route and then forward the response") {
|
||||
|
||||
scenario("produce message and send normal response to direct:forward-test-1") {
|
||||
given("a registered one-way producer configured with a forward target")
|
||||
val producer = actorOf(classOf[SampleUntypedForwardingProducer])
|
||||
|
||||
when("a test message is sent to the producer with !")
|
||||
mockEndpoint.expectedBodiesReceived("received test")
|
||||
val result = producer.tell(Message("test"), producer)
|
||||
|
||||
then("a normal response should have been sent")
|
||||
mockEndpoint.assertIsSatisfied
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private def mockEndpoint = CamelContextManager.mandatoryContext.getEndpoint("mock:mock", classOf[MockEndpoint])
|
||||
}
|
||||
|
||||
object UntypedProducerFeatureTest {
|
||||
class TestRoute extends RouteBuilder {
|
||||
def configure {
|
||||
from("direct:forward-test-1").to("mock:mock")
|
||||
from("direct:producer-test-1").process(new Processor() {
|
||||
def process(exchange: Exchange) = {
|
||||
exchange.getIn.getBody match {
|
||||
case "fail" ⇒ throw new Exception("failure")
|
||||
case body ⇒ exchange.getOut.setBody("received %s" format body)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
101
akka-camel/src/test/scala/akka/camel/UntypedProducerTest.scala
Normal file
101
akka-camel/src/test/scala/akka/camel/UntypedProducerTest.scala
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel
|
||||
|
||||
import org.apache.camel.{ Exchange, Processor }
|
||||
import org.apache.camel.builder.RouteBuilder
|
||||
import org.apache.camel.component.mock.MockEndpoint
|
||||
|
||||
import akka.camel.TestSupport.SharedCamelSystem
|
||||
import akka.actor.Props
|
||||
import akka.pattern._
|
||||
import akka.dispatch.Await
|
||||
import akka.util.duration._
|
||||
import org.scalatest._
|
||||
|
||||
class UntypedProducerTest extends WordSpec with BeforeAndAfterAll with BeforeAndAfterEach with SharedCamelSystem with GivenWhenThen {
|
||||
import UntypedProducerTest._
|
||||
val timeout = 1 second
|
||||
override protected def beforeAll = {
|
||||
camel.context.addRoutes(new TestRoute)
|
||||
}
|
||||
|
||||
override protected def afterEach = {
|
||||
mockEndpoint.reset
|
||||
}
|
||||
|
||||
"An UntypedProducer producing a message to a sync Camel route" must {
|
||||
|
||||
"produce a message and receive a normal response" in {
|
||||
given("a registered two-way producer")
|
||||
val producer = system.actorOf(Props[SampleUntypedReplyingProducer])
|
||||
|
||||
when("a test message is sent to the producer with !!")
|
||||
val message = Message("test", Map(Message.MessageExchangeId -> "123"))
|
||||
val future = producer.ask(message)(timeout)
|
||||
then("a normal response should have been returned by the producer")
|
||||
val expected = Message("received test", Map(Message.MessageExchangeId -> "123"))
|
||||
Await.result(future, timeout) match {
|
||||
case result: Message ⇒ assert(result === expected)
|
||||
case unexpected ⇒ fail("Actor responded with unexpected message:" + unexpected)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
"produce a message and receive a failure response" in {
|
||||
given("a registered two-way producer")
|
||||
val producer = system.actorOf(Props[SampleUntypedReplyingProducer])
|
||||
|
||||
when("a test message causing an exception is sent to the producer with !!")
|
||||
val message = Message("fail", Map(Message.MessageExchangeId -> "123"))
|
||||
val future = producer.ask(message)(timeout)
|
||||
then("a failure response should have been returned by the producer")
|
||||
Await.result(future, timeout) match {
|
||||
case result: Failure ⇒ {
|
||||
val expectedFailureText = result.cause.getMessage
|
||||
val expectedHeaders = result.headers
|
||||
assert(expectedFailureText === "failure")
|
||||
assert(expectedHeaders === Map(Message.MessageExchangeId -> "123"))
|
||||
}
|
||||
case unexpected ⇒ fail("Actor responded with unexpected message:" + unexpected)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
"An UntypedProducer producing a message to a sync Camel route and then forwarding the response" must {
|
||||
|
||||
"produce a message and send a normal response to direct:forward-test-1" in {
|
||||
given("a registered one-way producer configured with a forward target")
|
||||
val producer = system.actorOf(Props[SampleUntypedForwardingProducer])
|
||||
|
||||
when("a test message is sent to the producer with !")
|
||||
mockEndpoint.expectedBodiesReceived("received test")
|
||||
val result = producer.tell(Message("test", Map[String, Any]()), producer)
|
||||
|
||||
then("a normal response should have been sent")
|
||||
mockEndpoint.assertIsSatisfied
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private def mockEndpoint = camel.context.getEndpoint("mock:mock", classOf[MockEndpoint])
|
||||
}
|
||||
|
||||
object UntypedProducerTest {
|
||||
class TestRoute extends RouteBuilder {
|
||||
def configure {
|
||||
from("direct:forward-test-1").to("mock:mock")
|
||||
from("direct:producer-test-1").process(new Processor() {
|
||||
def process(exchange: Exchange) = {
|
||||
exchange.getIn.getBody match {
|
||||
case "fail" ⇒ throw new Exception("failure")
|
||||
case body ⇒ exchange.getOut.setBody("received %s" format body)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,103 +0,0 @@
|
|||
package akka.camel.component
|
||||
|
||||
import org.apache.camel.{ Endpoint, AsyncProcessor }
|
||||
import org.apache.camel.impl.DefaultCamelContext
|
||||
import org.junit._
|
||||
import org.scalatest.junit.JUnitSuite
|
||||
|
||||
import akka.actor.uuidFrom
|
||||
|
||||
class ActorComponentTest extends JUnitSuite {
|
||||
val component: ActorComponent = ActorComponentTest.actorComponent
|
||||
|
||||
def testUUID = "93da8c80-c3fd-11df-abed-60334b120057"
|
||||
|
||||
@Test
|
||||
def shouldCreateEndpointWithIdDefined = {
|
||||
val ep1: ActorEndpoint = component.createEndpoint("actor:abc").asInstanceOf[ActorEndpoint]
|
||||
val ep2: ActorEndpoint = component.createEndpoint("actor:id:abc").asInstanceOf[ActorEndpoint]
|
||||
assert(ep1.idValue === Some("abc"))
|
||||
assert(ep2.idValue === Some("abc"))
|
||||
assert(ep1.idType === "id")
|
||||
assert(ep2.idType === "id")
|
||||
assert(!ep1.blocking)
|
||||
assert(!ep2.blocking)
|
||||
assert(ep1.autoack)
|
||||
assert(ep2.autoack)
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateEndpointWithIdTemplate = {
|
||||
val ep: ActorEndpoint = component.createEndpoint("actor:id:").asInstanceOf[ActorEndpoint]
|
||||
assert(ep.idValue === None)
|
||||
assert(ep.idType === "id")
|
||||
assert(!ep.blocking)
|
||||
assert(ep.autoack)
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateEndpointWithIdTemplateAndBlockingSet = {
|
||||
val ep: ActorEndpoint = component.createEndpoint("actor:id:?blocking=true").asInstanceOf[ActorEndpoint]
|
||||
assert(ep.idValue === None)
|
||||
assert(ep.idType === "id")
|
||||
assert(ep.blocking)
|
||||
assert(ep.autoack)
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateEndpointWithUuidDefined = {
|
||||
val ep: ActorEndpoint = component.createEndpoint("actor:uuid:%s" format testUUID).asInstanceOf[ActorEndpoint]
|
||||
assert(ep.idValue === Some(testUUID))
|
||||
assert(ep.idType === "uuid")
|
||||
assert(!ep.blocking)
|
||||
assert(ep.autoack)
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateEndpointWithUuidTemplate = {
|
||||
val ep: ActorEndpoint = component.createEndpoint("actor:uuid:").asInstanceOf[ActorEndpoint]
|
||||
assert(ep.idValue === None)
|
||||
assert(ep.idType === "uuid")
|
||||
assert(!ep.blocking)
|
||||
assert(ep.autoack)
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateEndpointWithUuidTemplateAndBlockingSet = {
|
||||
val ep: ActorEndpoint = component.createEndpoint("actor:uuid:?blocking=true").asInstanceOf[ActorEndpoint]
|
||||
assert(ep.idValue === None)
|
||||
assert(ep.idType === "uuid")
|
||||
assert(ep.blocking)
|
||||
assert(ep.autoack)
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateEndpointWithBlockingSet = {
|
||||
val ep: ActorEndpoint = component.createEndpoint("actor:uuid:%s?blocking=true" format testUUID).asInstanceOf[ActorEndpoint]
|
||||
assert(ep.idValue === Some(testUUID))
|
||||
assert(ep.idType === "uuid")
|
||||
assert(ep.blocking)
|
||||
assert(ep.autoack)
|
||||
}
|
||||
|
||||
@Test
|
||||
def shouldCreateEndpointWithAutoackUnset = {
|
||||
val ep: ActorEndpoint = component.createEndpoint("actor:uuid:%s?autoack=false" format testUUID).asInstanceOf[ActorEndpoint]
|
||||
assert(ep.idValue === Some(testUUID))
|
||||
assert(ep.idType === "uuid")
|
||||
assert(!ep.blocking)
|
||||
assert(!ep.autoack)
|
||||
}
|
||||
}
|
||||
|
||||
object ActorComponentTest {
|
||||
def actorComponent = {
|
||||
val component = new ActorComponent
|
||||
component.setCamelContext(new DefaultCamelContext)
|
||||
component
|
||||
}
|
||||
|
||||
def actorEndpoint(uri: String) = actorComponent.createEndpoint(uri)
|
||||
def actorProducer(endpoint: Endpoint) = endpoint.createProducer
|
||||
def actorAsyncProducer(endpoint: Endpoint) = endpoint.createProducer.asInstanceOf[AsyncProcessor]
|
||||
}
|
||||
|
|
@ -0,0 +1,136 @@
|
|||
package akka.camel.internal
|
||||
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
import akka.testkit.{ TestProbe, TestKit }
|
||||
import akka.util.duration._
|
||||
import org.scalatest.{ GivenWhenThen, BeforeAndAfterEach, BeforeAndAfterAll, WordSpec }
|
||||
import akka.actor.{ Props, ActorSystem }
|
||||
import akka.util.Duration
|
||||
|
||||
class ActivationTrackerTest extends TestKit(ActorSystem("test")) with WordSpec with MustMatchers with BeforeAndAfterAll with BeforeAndAfterEach with GivenWhenThen {
|
||||
|
||||
override protected def afterAll() { system.shutdown() }
|
||||
|
||||
var actor: TestProbe = _
|
||||
var awaiting: Awaiting = _
|
||||
var anotherAwaiting: Awaiting = _
|
||||
val cause = new Exception("cause of failure")
|
||||
|
||||
override protected def beforeEach() {
|
||||
actor = TestProbe()
|
||||
awaiting = new Awaiting(actor)
|
||||
anotherAwaiting = new Awaiting(actor)
|
||||
}
|
||||
|
||||
val at = system.actorOf(Props[ActivationTracker])
|
||||
|
||||
"ActivationTracker forwards activation message to all awaiting parties" in {
|
||||
awaiting.awaitActivation()
|
||||
anotherAwaiting.awaitActivation()
|
||||
|
||||
publish(EndpointActivated(actor.ref))
|
||||
|
||||
awaiting.verifyActivated()
|
||||
anotherAwaiting.verifyActivated()
|
||||
}
|
||||
|
||||
"ActivationTracker send activation message even if activation happened earlier" in {
|
||||
publish(EndpointActivated(actor.ref))
|
||||
Thread.sleep(50)
|
||||
awaiting.awaitActivation()
|
||||
|
||||
awaiting.verifyActivated()
|
||||
}
|
||||
|
||||
"ActivationTracker send activation message even if actor is already deactivated" in {
|
||||
publish(EndpointActivated(actor.ref))
|
||||
publish(EndpointDeActivated(actor.ref))
|
||||
Thread.sleep(50)
|
||||
awaiting.awaitActivation()
|
||||
|
||||
awaiting.verifyActivated()
|
||||
}
|
||||
|
||||
"ActivationTracker forwards de-activation message to all awaiting parties" in {
|
||||
given("Actor is activated")
|
||||
publish(EndpointActivated(actor.ref))
|
||||
given("Actor is deactivated")
|
||||
publish(EndpointDeActivated(actor.ref))
|
||||
|
||||
when("Multiple parties await deactivation")
|
||||
awaiting.awaitDeActivation()
|
||||
anotherAwaiting.awaitDeActivation()
|
||||
|
||||
then("all awaiting parties are notified")
|
||||
awaiting.verifyDeActivated()
|
||||
anotherAwaiting.verifyDeActivated()
|
||||
}
|
||||
|
||||
"ActivationTracker forwards de-activation message even if deactivation happened earlier" in {
|
||||
given("Actor is activated")
|
||||
publish(EndpointActivated(actor.ref))
|
||||
|
||||
given("Someone is awaiting de-activation")
|
||||
awaiting.awaitDeActivation()
|
||||
|
||||
when("Actor is de-activated")
|
||||
publish(EndpointDeActivated(actor.ref))
|
||||
|
||||
then("Awaiting gets notified")
|
||||
awaiting.verifyDeActivated()
|
||||
}
|
||||
|
||||
"ActivationTracker forwards de-activation message even if someone awaits de-activation even before activation happens" in {
|
||||
given("Someone is awaiting de-activation")
|
||||
val awaiting = new Awaiting(actor)
|
||||
awaiting.awaitDeActivation()
|
||||
|
||||
given("Actor is activated")
|
||||
publish(EndpointActivated(actor.ref))
|
||||
|
||||
when("Actor is de-activated")
|
||||
publish(EndpointDeActivated(actor.ref))
|
||||
|
||||
then("Awaiting gets notified")
|
||||
awaiting.verifyDeActivated()
|
||||
}
|
||||
|
||||
"ActivationTracker sends activation failure when failed to activate" in {
|
||||
awaiting.awaitActivation()
|
||||
publish(EndpointFailedToActivate(actor.ref, cause))
|
||||
|
||||
awaiting.verifyFailedToActivate()
|
||||
}
|
||||
|
||||
"ActivationTracker sends de-activation failure when failed to de-activate" in {
|
||||
publish(EndpointActivated(actor.ref))
|
||||
awaiting.awaitDeActivation()
|
||||
publish(EndpointFailedToDeActivate(actor.ref, cause))
|
||||
|
||||
awaiting.verifyFailedToDeActivate()
|
||||
}
|
||||
|
||||
"ActivationTracker sends activation message even if it failed to de-activate" in {
|
||||
publish(EndpointActivated(actor.ref))
|
||||
publish(EndpointFailedToDeActivate(actor.ref, cause))
|
||||
awaiting.awaitActivation()
|
||||
|
||||
awaiting.verifyActivated()
|
||||
}
|
||||
|
||||
def publish(msg: AnyRef) = system.eventStream.publish(msg)
|
||||
|
||||
class Awaiting(actor: TestProbe) {
|
||||
val probe = TestProbe()
|
||||
def awaitActivation() = at.tell(AwaitActivation(actor.ref), probe.ref)
|
||||
def awaitDeActivation() = at.tell(AwaitDeActivation(actor.ref), probe.ref)
|
||||
def verifyActivated(timeout: Duration = 50 millis) = within(timeout) { probe.expectMsg(EndpointActivated(actor.ref)) }
|
||||
def verifyDeActivated(timeout: Duration = 50 millis) = within(timeout) { probe.expectMsg(EndpointDeActivated(actor.ref)) }
|
||||
|
||||
def verifyFailedToActivate(timeout: Duration = 50 millis) = within(timeout) { probe.expectMsg(EndpointFailedToActivate(actor.ref, cause)) }
|
||||
def verifyFailedToDeActivate(timeout: Duration = 50 millis) = within(timeout) { probe.expectMsg(EndpointFailedToDeActivate(actor.ref, cause)) }
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
63
akka-camel/src/test/scala/akka/camel/internal/TryTest.scala
Normal file
63
akka-camel/src/test/scala/akka/camel/internal/TryTest.scala
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package akka.camel.internal
|
||||
|
||||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
import akka.camel.internal.Try._
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
import org.scalatest.mock.MockitoSugar
|
||||
import akka.event.LoggingAdapter
|
||||
import org.scalatest.{ BeforeAndAfterEach, WordSpec }
|
||||
|
||||
class TryTest extends WordSpec with MustMatchers with MockitoSugar with BeforeAndAfterEach {
|
||||
|
||||
import org.mockito.Mockito._
|
||||
import org.mockito.Matchers._
|
||||
import org.mockito.Matchers.{ eq ⇒ the }
|
||||
|
||||
implicit var log: LoggingAdapter = _
|
||||
|
||||
override def beforeEach() {
|
||||
log = mock[LoggingAdapter]
|
||||
}
|
||||
|
||||
"Safe executes block" in {
|
||||
var executed = false
|
||||
safe {
|
||||
executed = true
|
||||
}
|
||||
executed must be(true)
|
||||
verifyNoMoreInteractions(log)
|
||||
}
|
||||
|
||||
"Safe swallows exception and logs it" in {
|
||||
safe(throw new Exception)
|
||||
verify(log).warning(any[String], any[Any])
|
||||
}
|
||||
|
||||
"Try-otherwise runs otherwise and throws exception when the first block fails" in {
|
||||
var otherwiseCalled = false
|
||||
intercept[Exception] {
|
||||
Try(throw new Exception) otherwise (otherwiseCalled = true)
|
||||
}
|
||||
otherwiseCalled must be(true)
|
||||
verifyNoMoreInteractions(log)
|
||||
}
|
||||
|
||||
"Try-otherwise swallows exception thrown in otherwise clause and logs it" in {
|
||||
val exceptionFromOtherwise = new RuntimeException("e2")
|
||||
intercept[RuntimeException] {
|
||||
Try(throw new RuntimeException("e1")) otherwise (throw exceptionFromOtherwise)
|
||||
}.getMessage must be("e1")
|
||||
verify(log, only()).warning(any[String], the(exceptionFromOtherwise))
|
||||
}
|
||||
|
||||
"Try-otherwise doesnt run otherwise if first block doesnt fail" in {
|
||||
var otherwiseCalled = false
|
||||
Try(2 + 2) otherwise (otherwiseCalled = true)
|
||||
otherwiseCalled must be(false)
|
||||
verifyNoMoreInteractions(log)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel.internal.component
|
||||
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
import akka.util.duration._
|
||||
import akka.camel.TestSupport.SharedCamelSystem
|
||||
import org.apache.camel.Component
|
||||
import org.scalatest.WordSpec
|
||||
|
||||
class ActorComponentConfigurationTest extends WordSpec with MustMatchers with SharedCamelSystem {
|
||||
|
||||
val component: Component = camel.context.getComponent("actor")
|
||||
|
||||
"Endpoint url config must be correctly parsed" in {
|
||||
val actorEndpointConfig = component.createEndpoint("actor://path:akka://test/user/$a?autoack=false&replyTimeout=987000000+nanos").asInstanceOf[ActorEndpointConfig]
|
||||
|
||||
actorEndpointConfig must have(
|
||||
'endpointUri("actor://path:akka://test/user/$a?autoack=false&replyTimeout=987000000+nanos"),
|
||||
'path(ActorEndpointPath.fromCamelPath("path:akka://test/user/$a")),
|
||||
'autoack(false),
|
||||
'replyTimeout(987000000 nanos))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* Copyright (C) 2009when2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel.internal.component
|
||||
|
||||
import org.scalatest.mock.MockitoSugar
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
import akka.camel.TestSupport.SharedCamelSystem
|
||||
import akka.actor.Props
|
||||
import org.scalatest.WordSpec
|
||||
|
||||
class ActorEndpointPathTest extends WordSpec with SharedCamelSystem with MustMatchers with MockitoSugar {
|
||||
|
||||
def find(path: String) = ActorEndpointPath.fromCamelPath("path:" + path).findActorIn(system)
|
||||
|
||||
"findActorIn returns Some(actor ref) if actor exists" in {
|
||||
val path = system.actorOf(Props(behavior = ctx ⇒ { case _ ⇒ {} })).path
|
||||
find(path.toString) must be('defined)
|
||||
}
|
||||
|
||||
"findActorIn returns None" when {
|
||||
"invalid path" in { find("some_invalid_path") must be(None) }
|
||||
"non existing valid path" in { find("akka://system/user/$a") must be(None) }
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,346 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel.internal.component
|
||||
|
||||
import org.scalatest.mock.MockitoSugar
|
||||
import org.mockito.Matchers.{ eq ⇒ the, any }
|
||||
import org.mockito.Mockito._
|
||||
import org.apache.camel.AsyncCallback
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import akka.util.duration._
|
||||
import akka.util.Duration
|
||||
import akka.testkit.{ TestKit, TestProbe }
|
||||
import java.lang.String
|
||||
import akka.actor.{ ActorRef, Props, ActorSystem, Actor }
|
||||
import akka.camel._
|
||||
import internal.CamelExchangeAdapter
|
||||
import org.scalatest.{ Suite, WordSpec, BeforeAndAfterAll, BeforeAndAfterEach }
|
||||
import akka.camel.TestSupport._
|
||||
import java.util.concurrent.{ TimeoutException, CountDownLatch, TimeUnit }
|
||||
import org.mockito.{ ArgumentMatcher, Matchers, Mockito }
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
|
||||
class ActorProducerTest extends TestKit(ActorSystem("test")) with WordSpec with MustMatchers with ActorProducerFixture {
|
||||
|
||||
"ActorProducer" when {
|
||||
|
||||
"synchronous" when {
|
||||
|
||||
"consumer actor doesnt exist" must {
|
||||
"set failure message on exchange" in {
|
||||
producer = given(actor = null)
|
||||
producer.process(exchange)
|
||||
|
||||
verify(exchange).setFailure(any[Failure])
|
||||
}
|
||||
}
|
||||
|
||||
"in-only" must {
|
||||
def producer = given(outCapable = false)
|
||||
|
||||
"pass the message to the consumer" in {
|
||||
producer.process(exchange)
|
||||
within(1 second)(probe.expectMsg(message))
|
||||
}
|
||||
|
||||
"not expect response and not block" in {
|
||||
time(producer.process(exchange)) must be < (30 millis)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
"out capable" when {
|
||||
"response is sent back by actor" must {
|
||||
|
||||
"get a response" in {
|
||||
producer = given(actor = echoActor, outCapable = true)
|
||||
|
||||
producer.process(exchange)
|
||||
|
||||
verify(exchange).setResponse(msg("received " + message))
|
||||
}
|
||||
}
|
||||
|
||||
"response is not sent by actor" must {
|
||||
|
||||
def process() = {
|
||||
producer = given(outCapable = true, replyTimeout = 100 millis)
|
||||
time(producer.process(exchange))
|
||||
}
|
||||
|
||||
"timeout after replyTimeout" in {
|
||||
val duration = process()
|
||||
duration must (be >= (100 millis) and be < (200 millis))
|
||||
}
|
||||
|
||||
"never set the response on exchange" in {
|
||||
process()
|
||||
verify(exchange, Mockito.never()).setResponse(any[Message])
|
||||
}
|
||||
|
||||
"set failure message to timeout" in {
|
||||
process()
|
||||
verify(exchange).setFailure(any[Failure])
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//TODO: write more tests for synchronous process(exchange) method
|
||||
|
||||
}
|
||||
|
||||
"asynchronous" when {
|
||||
|
||||
def verifyFailureIsSet {
|
||||
producer.process(exchange, asyncCallback)
|
||||
asyncCallback.awaitCalled()
|
||||
verify(exchange).setFailure(any[Failure])
|
||||
}
|
||||
|
||||
"out-capable" when {
|
||||
|
||||
"consumer actor doesnt exist" must {
|
||||
"set failure message on exchange" in {
|
||||
producer = given(actor = null, outCapable = true)
|
||||
verifyFailureIsSet
|
||||
}
|
||||
}
|
||||
|
||||
"response is ok" must {
|
||||
"get a response and async callback as soon as it gets the response (but not before)" in {
|
||||
producer = given(outCapable = true)
|
||||
|
||||
val doneSync = producer.process(exchange, asyncCallback)
|
||||
|
||||
asyncCallback.expectNoCallWithin(100 millis); info("no async callback before response")
|
||||
|
||||
within(1 second) {
|
||||
probe.expectMsgType[Message]
|
||||
probe.sender ! "some message"
|
||||
}
|
||||
doneSync must be(false); info("done async")
|
||||
|
||||
asyncCallback.expectDoneAsyncWithin(1 second); info("async callback received")
|
||||
verify(exchange).setResponse(msg("some message")); info("response as expected")
|
||||
}
|
||||
}
|
||||
|
||||
"response is Failure" must {
|
||||
"set an exception on exchange" in {
|
||||
val failure = Failure(new RuntimeException("some failure"))
|
||||
|
||||
producer = given(outCapable = true)
|
||||
|
||||
producer.process(exchange, asyncCallback)
|
||||
|
||||
within(1 second) {
|
||||
probe.expectMsgType[Message]
|
||||
probe.sender ! failure
|
||||
asyncCallback.awaitCalled(remaining)
|
||||
}
|
||||
|
||||
verify(exchange).setFailure(failure)
|
||||
}
|
||||
}
|
||||
|
||||
"no response is sent within timeout" must {
|
||||
"set TimeoutException on exchange" in {
|
||||
producer = given(outCapable = true, replyTimeout = 10 millis)
|
||||
producer.process(exchange, asyncCallback)
|
||||
asyncCallback.awaitCalled(100 millis)
|
||||
verify(exchange).setFailure(Matchers.argThat(new ArgumentMatcher[Failure] {
|
||||
def matches(failure: AnyRef) = { failure.asInstanceOf[Failure].getCause must be(anInstanceOf[TimeoutException]); true }
|
||||
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
"in-only" when {
|
||||
|
||||
"consumer actor doesnt exist" must {
|
||||
"set failure message on exchange" in {
|
||||
producer = given(actor = null, outCapable = false)
|
||||
verifyFailureIsSet
|
||||
}
|
||||
}
|
||||
|
||||
"autoAck" must {
|
||||
|
||||
"get sync callback as soon as it sends a message" in {
|
||||
|
||||
producer = given(outCapable = false, autoAck = true)
|
||||
val doneSync = producer.process(exchange, asyncCallback)
|
||||
|
||||
doneSync must be(true); info("done sync")
|
||||
asyncCallback.expectDoneSyncWithin(1 second); info("async callback called")
|
||||
verify(exchange, never()).setResponse(any[Message]); info("no response forwarded to exchange")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
"manualAck" when {
|
||||
|
||||
"response is Ack" must {
|
||||
"get async callback" in {
|
||||
producer = given(outCapable = false, autoAck = false)
|
||||
|
||||
val doneSync = producer.process(exchange, asyncCallback)
|
||||
|
||||
doneSync must be(false)
|
||||
within(1 second) {
|
||||
probe.expectMsgType[Message]; info("message sent to consumer")
|
||||
probe.sender ! Ack
|
||||
asyncCallback.expectDoneAsyncWithin(remaining); info("async callback called")
|
||||
}
|
||||
verify(exchange, never()).setResponse(any[Message]); info("no response forwarded to exchange")
|
||||
}
|
||||
}
|
||||
|
||||
"expecting Ack or Failure message and some other message is sent as a response" must {
|
||||
"fail" in {
|
||||
producer = given(outCapable = false, autoAck = false)
|
||||
|
||||
producer.process(exchange, asyncCallback)
|
||||
|
||||
within(1 second) {
|
||||
probe.expectMsgType[Message]; info("message sent to consumer")
|
||||
probe.sender ! "some neither Ack nor Failure response"
|
||||
asyncCallback.expectDoneAsyncWithin(remaining); info("async callback called")
|
||||
}
|
||||
verify(exchange, never()).setResponse(any[Message]); info("no response forwarded to exchange")
|
||||
verify(exchange).setFailure(any[Failure]); info("failure set")
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
"no Ack is sent within timeout" must {
|
||||
"set failure on exchange" in {
|
||||
producer = given(outCapable = false, replyTimeout = 10 millis, autoAck = false)
|
||||
|
||||
producer.process(exchange, asyncCallback)
|
||||
asyncCallback.awaitCalled(100 millis)
|
||||
verify(exchange).setFailure(any[Failure])
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
"response is Failure" must {
|
||||
"set an exception on exchange" in {
|
||||
producer = given(outCapable = false, autoAck = false)
|
||||
|
||||
val doneSync = producer.process(exchange, asyncCallback)
|
||||
|
||||
doneSync must be(false)
|
||||
within(1 second) {
|
||||
probe.expectMsgType[Message]; info("message sent to consumer")
|
||||
probe.sender ! Failure(new Exception)
|
||||
asyncCallback.awaitCalled(remaining);
|
||||
}
|
||||
verify(exchange, never()).setResponse(any[Message]); info("no response forwarded to exchange")
|
||||
verify(exchange).setFailure(any[Failure]); info("failure set")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
trait ActorProducerFixture extends MockitoSugar with BeforeAndAfterAll with BeforeAndAfterEach { self: TestKit with MustMatchers with Suite ⇒
|
||||
var camel: Camel = _
|
||||
var exchange: CamelExchangeAdapter = _
|
||||
var callback: AsyncCallback = _
|
||||
|
||||
var producer: ConsumerAsyncProcessor = _
|
||||
var message: Message = _
|
||||
var probe: TestProbe = _
|
||||
var asyncCallback: TestAsyncCallback = _
|
||||
var actorEndpointPath: ActorEndpointPath = _
|
||||
|
||||
override protected def beforeEach() {
|
||||
asyncCallback = createAsyncCallback
|
||||
|
||||
probe = TestProbe()
|
||||
camel = mock[Camel]
|
||||
exchange = mock[CamelExchangeAdapter]
|
||||
callback = mock[AsyncCallback]
|
||||
actorEndpointPath = mock[ActorEndpointPath]
|
||||
|
||||
producer = new ConsumerAsyncProcessor(config(), camel)
|
||||
message = Message(null, null)
|
||||
}
|
||||
|
||||
override protected def afterAll() {
|
||||
system.shutdown()
|
||||
}
|
||||
|
||||
def msg(s: String) = Message(s, Map.empty)
|
||||
|
||||
def given(actor: ActorRef = probe.ref, outCapable: Boolean = true, autoAck: Boolean = true, replyTimeout: Duration = Int.MaxValue seconds) = {
|
||||
prepareMocks(actor, outCapable = outCapable)
|
||||
new ConsumerAsyncProcessor(config(isAutoAck = autoAck, _replyTimeout = replyTimeout), camel)
|
||||
}
|
||||
|
||||
def createAsyncCallback = new TestAsyncCallback
|
||||
|
||||
class TestAsyncCallback extends AsyncCallback {
|
||||
def expectNoCallWithin(duration: Duration) {
|
||||
if (callbackReceived.await(duration.toNanos, TimeUnit.NANOSECONDS)) fail("NOT expected callback, but received one!")
|
||||
}
|
||||
|
||||
def awaitCalled(timeout: Duration = 1 second) { valueWithin(1 second) }
|
||||
|
||||
val callbackReceived = new CountDownLatch(1)
|
||||
val callbackValue = new AtomicBoolean()
|
||||
|
||||
def done(doneSync: Boolean) {
|
||||
callbackValue set doneSync
|
||||
callbackReceived.countDown()
|
||||
}
|
||||
|
||||
private[this] def valueWithin(implicit timeout: Duration) = {
|
||||
if (!callbackReceived.await(timeout.toNanos, TimeUnit.NANOSECONDS)) fail("Callback not received!")
|
||||
callbackValue.get
|
||||
}
|
||||
|
||||
def expectDoneSyncWithin(implicit timeout: Duration) {
|
||||
if (!valueWithin(timeout)) fail("Expected to be done Synchronously")
|
||||
}
|
||||
def expectDoneAsyncWithin(implicit timeout: Duration) {
|
||||
if (valueWithin(timeout)) fail("Expected to be done Asynchronously")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
def config(endpointUri: String = "test-uri", isAutoAck: Boolean = true, _replyTimeout: Duration = Int.MaxValue seconds) = {
|
||||
new ActorEndpointConfig {
|
||||
val path = actorEndpointPath
|
||||
val getEndpointUri = endpointUri
|
||||
autoack = isAutoAck
|
||||
replyTimeout = _replyTimeout
|
||||
}
|
||||
}
|
||||
|
||||
def prepareMocks(actor: ActorRef, message: Message = message, outCapable: Boolean) {
|
||||
when(actorEndpointPath.findActorIn(any[ActorSystem])) thenReturn Option(actor)
|
||||
when(exchange.toRequestMessage(any[Map[String, Any]])) thenReturn message
|
||||
when(exchange.isOutCapable) thenReturn outCapable
|
||||
}
|
||||
|
||||
def echoActor = system.actorOf(Props(new Actor {
|
||||
protected def receive = {
|
||||
case msg ⇒ sender ! "received " + msg
|
||||
}
|
||||
}))
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.camel.internal.component
|
||||
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
import akka.util.duration._
|
||||
import akka.util.Duration
|
||||
import org.scalatest.WordSpec
|
||||
|
||||
class DurationConverterTest extends WordSpec with MustMatchers {
|
||||
import DurationTypeConverter._
|
||||
|
||||
"DurationTypeConverter must convert '10 nanos'" in {
|
||||
convertTo(classOf[Duration], "10 nanos") must be(10 nanos)
|
||||
}
|
||||
|
||||
"DurationTypeConverter must do the roundtrip" in {
|
||||
convertTo(classOf[Duration], DurationTypeConverter.toString(10 seconds)) must be(10 seconds)
|
||||
}
|
||||
|
||||
"DurationTypeConverter must throw if invalid format" in {
|
||||
intercept[Exception] {
|
||||
convertTo(classOf[Duration], "abc nanos") must be(10 nanos)
|
||||
}
|
||||
}
|
||||
|
||||
"DurationTypeConverter must throw if doesn't end with nanos" in {
|
||||
intercept[Exception] {
|
||||
convertTo(classOf[Duration], "10233") must be(10 nanos)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
3
akka-camel/src/test/scala/application.conf
Normal file
3
akka-camel/src/test/scala/application.conf
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
akka{
|
||||
loglevel = "ERROR"
|
||||
}
|
||||
|
|
@ -32,7 +32,7 @@ object AkkaBuild extends Build {
|
|||
Unidoc.unidocExclude := Seq(samples.id, tutorials.id),
|
||||
Dist.distExclude := Seq(actorTests.id, akkaSbtPlugin.id, docs.id)
|
||||
),
|
||||
aggregate = Seq(actor, testkit, actorTests, remote, cluster, slf4j, agent, transactor, mailboxes, zeroMQ, kernel, akkaSbtPlugin, actorMigration, samples, tutorials, docs)
|
||||
aggregate = Seq(actor, testkit, actorTests, remote, camel, cluster, slf4j, agent, transactor, mailboxes, zeroMQ, kernel, akkaSbtPlugin, actorMigration, samples, tutorials, docs)
|
||||
)
|
||||
|
||||
lazy val actor = Project(
|
||||
|
|
@ -239,6 +239,15 @@ object AkkaBuild extends Build {
|
|||
libraryDependencies ++= Dependencies.kernel
|
||||
)
|
||||
)
|
||||
|
||||
lazy val camel = Project(
|
||||
id = "akka-camel",
|
||||
base = file("akka-camel"),
|
||||
dependencies = Seq(actor, slf4j, testkit % "test->test"),
|
||||
settings = defaultSettings ++ Seq(
|
||||
libraryDependencies ++= Dependencies.camel
|
||||
)
|
||||
)
|
||||
|
||||
lazy val actorMigration = Project(
|
||||
id = "akka-actor-migration",
|
||||
|
|
@ -465,6 +474,8 @@ object Dependencies {
|
|||
|
||||
val kernel = Seq(Test.scalatest, Test.junit)
|
||||
|
||||
val camel = Seq(Test.scalatest, Test.junit, Test.mockito, camelCore)
|
||||
|
||||
// TODO: resolve Jetty version conflict
|
||||
// val sampleCamel = Seq(camelCore, camelSpring, commonsCodec, Runtime.camelJms, Runtime.activemq, Runtime.springJms,
|
||||
// Test.junit, Test.scalatest, Test.logback)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue