|
Page Edited :
CAMEL :
Scala DSL - EIP
Scala DSL - EIP has been edited by Gert Vanthienen (May 20, 2008). Content:
Messaging systemsPipelineThere is a simple syntax available for specifying pipeline, by simple putting to or → between the different steps in the pipeline. "direct:a" --> "mock:a" --> "mock:b" "direct:c" to "mock:c" to "mock:d" For more advanced use cases, you can also use a block-based syntax, where every step in the pipeline starts with either to or →. "direct:e" ==> { --> ("mock:e") --> ("mock:f") } "direct:g" ==> { to ("mock:g") to ("mock:h") } FilterFor a message filter, use the when() method with a parameter of type The Exchange ⇒ Boolean. In the example below, we use a Scala convenience method named in to access the 'in' message body; only messages where the 'in' message is <hello/> will arrive at the mock:a endpoint. "direct:a" when(_.in == "<hello/>") to("mock:a") Once again, if you need to specify a more advanced route, you can use the more elaborate syntax. "direct:b" ==> { when(_.in == "<hallo/>") { --> ("mock:b") to ("mock:c") } otherwise { to ("mock:e") } to ("mock:d") } Message routingContent based routerSimilar to the Filter , the content based router uses when methods with Exchange ⇒ Boolean function literals and an optional otherwise. The function literal can contain plain Scala code as well as any of the supported languages . The example below routes a given message based on the language of the message body. "direct:a" ==> { to ("mock:polyglot") choice { when (_.in == "<hello/>") to ("mock:english") when (_.in == "<hallo/>") { to ("mock:dutch") to ("mock:german") } otherwise to ("mock:french") } } DelayerUnlike a throttler, which only slows down messages if the rate exceeds a treshold, a delayer delays every messages with a fixed amount of time. An example: to delay every message going from seda:a to mock:a with 1 second, you write... "seda:a" delay(1 seconds) to ("mock:a") Our second example will delay the entire block (containing mock:c) without doing anything to mock:b "seda:b" ==> { to ("mock:b") delay(1 seconds) { to ("mock:c") } } Load balancerTo distribute the message handling load over multiple endpoints, we add a loadbalance to our route definition. You can optionally specify a load balancer strategy, like roundrobin "direct:a" ==> { loadbalance roundrobin { to ("mock:a") to ("mock:b") to ("mock:c") } } MulticastMulticast allows you to send a message to multiple endpoints at the same time. In a simple route, you can specify multiple targets in the to or → method call: "direct:a" --> ("mock:a", "mock:b") --> "mock:c" "direct:d" to ("mock:d", "mock:e") to "mock:f" You can also explicitly define an explicit multicast in your RouteBuilder: "direct:a" ==> { multicast { to ("mock:a") to ("mock:b") to ("mock:c") } } Recipient listYou can handle a static recipient list with a multicast or pipeline , but this EIP is usually applied when you want to dynamically determine the name(s) of the next endpoint(s) to route to. Use the recipients() method with a function literal (Exchange => Any) that returns the endpoint name(s). In the example below, the target endpoint name can be found in the String message starting at position 21. "direct:a" recipients(_.in(classOf[String]).substring(21)) Because the recipients() method just takes a function literal, you can basically use any kind of valid Scala code to determine the endpoint name. Have a look at the next example which uses pattern matching to figure out where to send the message: "direct:b" recipients(_.in match { case Toddler(_) => "mock:playgarden" case _ => "mock:work" }) Again, we can also use the same thing in a more block-like syntax. For this example, we use the Scala DSL's support for JXPath to determine the target. "direct:c" ==> { to("mock:c") recipients(_.jxpath("./in/body/destination")) } SplitterTo handle large message in smaller chunks, you can write a Scala Exchange ⇒ Any* method and add it to your route with the splitter method. As with many other EIPs, we support a short, in-line version as well as a more elaborate block based one. "direct:a" as(classOf[Document]) splitter(_.xpath("/persons/person")) to "mock:a" "direct:b" ==> { as(classOf[Document]) splitter(_.xpath("/persons/person")) { to("mock:b") to("mock:c") } } The above examples also show you how other languages like XPath can be within the Scala DSL. ThrottlerThe throttler allows you to slow down messages before sending them along. The throttle methods allows you to specify the maximum throughput rate of message: "seda:a" throttle 3 per 2 seconds to ("mock:a") It can also be used in front of block to throttle messages at that point. In the example below, message are passed on to mock:b in a normal rate (i.e. as fast as possible), but a maximum 3 messages/2 seconds will arrive at the mock:c endpoint. "seda:b" ==> { to ("mock:b") throttle(3) per 2 seconds { to ("mock:c") } } Message transformationContent enricherUsing a processor function (Exchange → Unit), you can alter/enrich the message content. This example uses a simple function literal to append " says Hello" to the message content: "direct:a" process(_.in += " says hello") to ("mock:a") However, you can also define a separate method/function to handle the transformation and pass that to the process method instead. The example below uses pattern matching to enrich the message content: "direct:b" process(myProcessor) to ("mock:b") def myProcessor(exchange: Exchange) = { exchange.in match { case "hello" => exchange.in = "hello from the UK" case "hallo" => exchange.in = "hallo vanuit Belgie" case "bonjour" => exchange.in = "bonjour de la douce France" } } Off course, you can also use any other Camel component (e.g. Velocity) to enrich the content and add it to a pipeline "direct:c" to ("velocity:org/apache/camel/scala/dsl/enricher.vm") to ("mock:c") TODOTODO: Create a Scala alternative and example for every EIP available on http://activemq.apache.org/camel/enterprise-integration-patterns.html
|
Unsubscribe or edit your notifications preferences
