9. What Are Design Patterns?
A design pattern is a formal way of
documenting a solution to a design
problem in a particular field of
expertise.
(Wikipedia)
9
13. What is Apache Camel?
A framework for simplifying
integration through the use of the
Enterprise Integration Patterns for
message mediation, processing,
http://camel.apache.org/
routing and transformation
13
14. Apache Camel is Focused on EIP
=>
http://camel.apache.org/ http://eaipatterns.com/
14
32. Throttler
public class MyRouteBuilder extends RouteBuilder {
public void configure() {
from("seda:a”)
.throttler(3).timePeriodMillis(30000)
.to("seda:b");
}
}
Java DSL
32
33. Delayer
public class MyRouteBuilder extends RouteBuilder {
public void configure() {
from("seda:a”)
.delayer(header("JMSTimestamp", 3000)
.to("seda:b");
}
}
Java DSL
33
34. Load Balancer
public class MyRouteBuilder extends RouteBuilder {
public void configure() {
from("file:/path/to/file")
.loadBalance().roundRobin()
.to("file:///one", "activemq:MY.Q", "http://host1:8181/fooApp");
}
}
Policy Description
Round Robin Balance the exchange load across the available endpoints
Random Randomly choose an endpoint to send the exchange
Sticky Sticky load balancing of exchanges using an expression
Topic Send exchange to all endpoints (JMS topic semantics)
Java DSL
34
35. Multicast
public class MyRouteBuilder extends RouteBuilder {
public void configure() {
from("direct:a")
.multicast()
.to("direct:x", "mock:y", "file:///tmp/bar?fileName=test.txt");
}
}
Java DSL
35
44. Make Context Discover Beans
package com.mycompany.beans;
public class MyBean {
public void someMethod(String name) {
...
}
}
<camelContext
xmlns="http://activemq.apache.org/camel/schema/spring">
<package>com.mycompany.beans</package>
</camelContext>
44
45. Bean as a Message Translator
public class MyRouteBuilder extends RouteBuilder {
public void configure() {
from("activemq:Incoming”).
beanRef("myBean").
to("activemq:Outgoing");
}
}
Java DSL
45
46. Bean as a Message Translator
*With Method Name
public class MyRouteBuilder extends RouteBuilder {
public void configure() {
from("activemq:Incoming”).
beanRef("myBean", "someMethod").
to("activemq:Outgoing");
}
}
Java DSL
46
47. Binding Beans to Camel Endpoints
public class Foo {
@MessageDriven(uri=”activemq:cheese”)
public void onCheese(String name) {
...
}
}
Java DSL
47
48. Binding Method Arguments
public class Foo {
public void onCheese(
@XPath(“/foo/bar/”) String name,
@Header(“JMSCorrelationID”) String id) {
...
}
}
Java DSL
48
49. Injecting Endpoints Into Beans
public class Foo {
@EndpointInject(uri= “activemq:foo.bar”)
ProducerTemplate producer;
public void doSomething() {
if (whatever) {
producer.sendBody(“<hello>world</hello>”);
}
}
}
Java DSL
49
50. Type Convertors
public class MyRouteBuilder extends RouteBuilder {
public void configure() {
from("direct:start").process(new Processor() {
public void process(Exchange exchange) {
Message in = exchange.getIn();
in.setBody(in.getBody(String.class) + " World!");
}
}).to("mock:result");
}
}
Support for the following types:
• File
• String
• byte[] and ByteBuffer
• InputStream and OutputStream
• Reader and Writer
• Document and Source
50
51. Type Conversion
@Converter
public class IOConverter {
@Converter
public static InputStream toInputStream(File file)
throws FileNotFoundException {
return new BufferedInputStream(
new FileInputStream(file));
}
}
51
52. Business Activity Monitoring (BAM)
public class MyActivities extends ProcessBuilder {
public void configure() throws Exception {
// lets define some activities, correlating on an
// XPath query of the message body
ActivityBuilder purchaseOrder = activity("activemq:PurchaseOrders")
.correlate(xpath("/purchaseOrder/@id").stringResult());
ActivityBuilder invoice = activity("activemq:Invoices")
.correlate(xpath("/invoice/@purchaseOrderId").stringResult());
// now lets add some BAM rules
invoice.starts().after(purchaseOrder.completes())
.expectWithin(seconds(1))
.errorIfOver(seconds(2)).to("activemq:FailedProcesses");
}
}
52