Repository: camel Updated Branches: refs/heads/camel-2.15.x dacc46d4e -> e081e569a refs/heads/master 198d45d1c -> c0c27ed8b
CAMEL-8783: Fixed scala dsl to let the routes be unprepared during the various ways Scala DSL allow to build routes. Camel will prepare the routes before start, which is the correct spot. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c0c27ed8 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c0c27ed8 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c0c27ed8 Branch: refs/heads/master Commit: c0c27ed8b81e0b53deb961fcedab3d0a017dd121 Parents: 198d45d Author: Claus Ibsen <[email protected]> Authored: Sat Sep 5 11:04:52 2015 +0200 Committer: Claus Ibsen <[email protected]> Committed: Sat Sep 5 11:04:52 2015 +0200 ---------------------------------------------------------------------- .../org/apache/camel/model/RouteDefinition.java | 11 +++++++++++ .../camel/scala/dsl/builder/RouteBuilder.scala | 18 +++++++++++++++--- .../apache/camel/scala/dsl/TransactedTest.scala | 16 ++++++++++++---- 3 files changed, 38 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/c0c27ed8/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java b/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java index 1780992..467bfa2 100644 --- a/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java @@ -129,6 +129,17 @@ public class RouteDefinition extends ProcessorDefinition<RouteDefinition> { prepared.set(true); } + /** + * Marks the route definition as un-prepared. + * <p/> + * This is needed if routes have been created by components such as + * <tt>camel-scala</tt>. To unset the prepare so the routes can be prepared + * at a later stage when scala has build the routes completely. + */ + public void markUnprepared() { + prepared.set(false); + } + @Override public String toString() { if (getId() != null) { http://git-wip-us.apache.org/repos/asf/camel/blob/c0c27ed8/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala ---------------------------------------------------------------------- diff --git a/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala b/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala index cbaa426..37a39ab 100644 --- a/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala +++ b/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala @@ -18,7 +18,7 @@ package org.apache.camel package scala package dsl.builder -import org.apache.camel.model.DataFormatDefinition +import org.apache.camel.model._ import org.apache.camel.builder.{LoggingErrorHandlerBuilder, DeadLetterChannelBuilder, ErrorHandlerBuilder} import org.apache.camel.spi.Policy @@ -44,6 +44,18 @@ class RouteBuilder extends Preamble with DSL with RoutesBuilder with Languages w override def configure() { onJavaBuilder(this) } + + override def checkInitialized(): Unit = { + super.checkInitialized() + + // must un-prepare the routes as they may have been eager pre-pared before scala has properly build the entire routes + // Camel will prepare the routes before the routes is started so it happens eventually at the right time + val it = getRouteCollection.getRoutes.iterator() + while (it.hasNext) { + var route = it.next() + route.markUnprepared() + } + } } val stack = new Stack[DSL] @@ -63,7 +75,7 @@ class RouteBuilder extends Preamble with DSL with RoutesBuilder with Languages w */ def onJavaBuilder(builder: org.apache.camel.builder.RouteBuilder) {} - implicit def stringToRoute(target: String) : SRouteDefinition = new SRouteDefinition(builder.from(target), this) + implicit def stringToRoute(target: String) : SRouteDefinition = new SRouteDefinition(builder.from(target), this) implicit def unwrap[W](wrapper: Wrapper[W]) = wrapper.unwrap implicit def constantToExpression(value: Any) : (Exchange => Any) = (exchange: Exchange) => value @@ -199,7 +211,7 @@ class RouteBuilder extends Preamble with DSL with RoutesBuilder with Languages w def throwException(exception: Exception) = stack.top.throwException(exception) def throwException(exceptionType: Class[_ <: Exception], message: String) = stack.top.throwException(exceptionType, message) def transacted = stack.top.transacted - def transacted(uri: String) = stack.top.transacted + def transacted(uri: String) = stack.top.transacted(uri) def transform(expression: Exchange => Any) = stack.top.transform(expression) def unmarshal(format: DataFormatDefinition) = stack.top.unmarshal(format) http://git-wip-us.apache.org/repos/asf/camel/blob/c0c27ed8/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/TransactedTest.scala ---------------------------------------------------------------------- diff --git a/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/TransactedTest.scala b/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/TransactedTest.scala index 700ce50..6d84de4 100644 --- a/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/TransactedTest.scala +++ b/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/TransactedTest.scala @@ -22,11 +22,10 @@ import org.junit.Test import org.springframework.transaction.support.{DefaultTransactionStatus, AbstractPlatformTransactionManager} import org.springframework.transaction.{TransactionDefinition, TransactionStatus, PlatformTransactionManager} - class TransactedTest extends ScalaTestSupport { @throws(classOf[Exception]) - override def createRegistry: JndiRegistry = { + override def createRegistry: JndiRegistry = { val registry = super.createRegistry // Just setup a dummy platform transaction manager for testing registry.bind("transactionManager", new AbstractPlatformTransactionManager() { @@ -36,7 +35,9 @@ class TransactedTest extends ScalaTestSupport { override def doRollback(status: DefaultTransactionStatus): Unit = {} - override def doGetTransaction(): AnyRef = {new Object()} + override def doGetTransaction(): AnyRef = { + new Object() + } }) registry } @@ -54,8 +55,15 @@ class TransactedTest extends ScalaTestSupport { override lazy val builder = { new ScalaRouteBuilder(context()) { - from("direct:start").transacted.to("mock:result") + + "direct:start" ==> { + routeId("myRoute") + transacted + to("mock:foo") + to("mock:result") + } } } + }
