01 apache camel-intro

Embed Size (px)

Citation preview

  • 1. REDPILL LINPROSWATCompetence Gathering #1Apache CamelApril 2011 introductionPRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING

2. AgendaBackgroundEIPArchitecture from 10,000 ftEndpoints & componentsProgramming modelHiding the integration API from your codeError handlingType convertersDeploymentPRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 3. BackgroundStarted as a subproject to ActiveMQ Now one of the top Apache projectsImplementation of the EIP patternsFramework for declaring routing logicNOT a fullblown runtime, such as an ESB. but has the same characteristicsReuses Spring concepts such as Templates, DI, registries,contexts etc.. PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 4. Enterprise Integration Patterns(EIP)Pipe and filter stateless flowsThe de facto bible when it comes to the messagingintegration styleEIP is the basis for all types of flow-based integration frameworks (i.e. Camel, Spring Integration, Jboss ESB, Mule)PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 5. ...EIP continuedPRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 6. A Camel EIP example Queue PapersSplit on eachRoute on body QueueQueuelineitem PencilspurchaseOrder File dir /invalidfrom("jms:queue:purchaseOrder?concurrentConsumers=5") .split().xpath("//lineItem") .choice().when(body().contains("Pen")) .to("jms:queue:Pencils").when(body().contains("Paper")).to("jms:queue:Papers").otherwise().to("file:target/messages/invalid");PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 7. 10,000 ft architectureCamelContext- Runtime "engine", similar to a SpringContextRoutes- Your DSL/XML declared flowsComponents- Physical connectivity factories to Endpoints, i.e to()/from()Processors- Implements the Processor interface (process() method)PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 8. Components and endpoints> 100 components jms:myQueue?concurrentConsumers=5&username=BillyComponent Path OptionsSeparate JARsThe usual suspects... File / JMS / RMI / WS / Http / DB+ a lot more SEDA, Direct, MINA, XMPP, WebSockets, Twitter...Components are bound to URI schemes..which are then interpreted as Endpoints PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 9. Message ExchangesContext for messagesExchange ID Unique idMEP InOnly/InOut EnumException Contains eventual runtimeexceptionProperties Meta-Data with a lifecycleof the complete exchangeMessage In/Out used in processorsand request/reply PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 10. Programming model Java "DSL"from("file:data/orders") (chained methods) .split.xpath("//order") .to("jms:queue:order"); Declarative XML (NS support) //order - Scala and Groovy alternatives...PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 11. Adding the routes, Java stylepublic class MyRoutes extends RouteBuilder{@Overridepublic void configure() throws Exception {from("file:/orders").to("jms:/orders");from("file:/payments").to("jms/payments");}}camelContext.addRoutes(new MyRoutes()); PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 12. Adding the routes, Spring / Blueprint style Bootstraps the context as Spring starts Main, servlet listener, camel included Main class etc PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 13. Loading Java Routes from XMLMixn match between XML/JavaConsistent concept throughout the framework com.rl.routes**.*Excluded***.*Irrespective of the language choice, the routes will be the same as its only instructions to CamelMeans that cross-functionality such as visualization etcworks across the languages PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 14. ProcessorsSimple way to add custom logic in a routeImplements the Processor interfaceQuick and dirty way, define your own anonymous impl inlinefrom("file:/payments") .process(new Processor(){ public void process(Exchange exchng) throws Exception {//business logic on exchange here}}) .to("jms:payments");PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 15. Service activator patternInstead of implementing the Processor intfReference service beans instead Flexible, less Camel API smell Camel can guess method based on message type/parameters, annotations or method-ref Can also reference by class type instead of registered beans from("file:/payments") .beanRef("paymentService") .to("jms/payments"); @Service class PaymentService{ public PaymentAck conductPayment(String payment){...PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 16. The Registrypublic interface Registry {Object lookup(String name); T lookup(String name, Class type); Map lookupByType(Class type);}Strategy interface for bean lookupsUsed internally, i.e for finding a TransactionManager implAs well as from your flows, using beanRef(myBean)Impl delegates to SpringContext, CDI BeanManager, Guice,OSGi etc...camelContext.setRegistry(myRegistry) PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 17. Payload type conversion from("file://inbox") .convertBodyTo(String.class, "UTF-8") .to("jms:inbox");Camel often automatically tries to convert message payload tomethod param based on registered convertersIf not, use .convertBodyTo(clazz) from flow to invoke type converter explicitlyCould be used where default type would not be the best fit I.e. explicitly wanting JMS TextMessage over ByteMessage PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 18. Writing type converters @Converter public final class IOConverter { @Converter public static InputStream toInputStream(URL url) throws IOException { return url.openStream(); } }META-INF/services/org/apache/camel/TypeConverter com.rl.common.typeconverters.IOConverter com.rl..typeconverters.MyConverter ....Or in runtime using..:context.getTypeConverterRegistry().addTypeConverter(from, to, converter);PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 19. CamelProxy Hiding Camel from clientsSame concept as i.e. Spring RemotingCamel automatically creates proxy implementations of a giveninterfaceDI-wire your proxy to the client ...... PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 20. Error handlingThrown exceptions Exchange.exception Typically technical exceptionsCamel Error handling by default only kicks in whenexchange.getException() != nullError handling is declarative and very flexible But you should test your assumptions! PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 21. Error handlersDefault Error handling: No redelivery Exceptions are propagated back to the caller / incoming endpointDefaultErrorHandlerDefault if none specifiedDeadLetterChannelImplements DLQ pattern. Redirect message.Also supports rolling back to original messageTransactionErrorHandlerOnly used for transactionsNoErrorHandler DummyLoggingErrorHandlerLogging PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 22. Error handler featureserrorHandler(defaultErrorHandler().maximumRedeliveries(2).redeliveryDelay(1000).retryAttemptedLogLevel(LoggingLevel.WARN));from("seda:queue.inbox").beanRef("orderService", "validate").beanRef("orderService", "enrich").log("Received order ${body}").to("mock:queue.order");Redelivery policies Specify retries, exponential backoff etc.Redelivery will be on the exception origin,NOT from the start of the routeScope Context or on a route basisException policies (onException(E)) Specific policies for particular exceptionsonWhenEven more fine-grained control. I.e checkstatus code on Http exceptions.PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 23. Exception policies onException(JmsException.class).handled(true).beanRef("errorFormatter"); from("jetty:http://0.0.0.0:80/orderservice") .beanRef("orderTransformer") .to("jms:queue:incomingOrders") .beanRef("orderAckTransformer");onException lets you provide granularity to exception handlinghandled(true) instructs the incoming endpoint to not throwexception back at callerBasically an alternative route for i.e constructing an error replymessage, without it the consumer would get the actualexception PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 24. Deployment optionsCamel is only a framework, not a deployment serverWAR, Java standalone, JMS Provider, ServiceMix OSGI, in- applicationThink about what Spring did for web app environmentindependence, Camel does the same for integrationsolutionsDoes not impose restrictions on deploymentenvironment! PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 25. Proposed deployment pathCamel as stand-alone app or deployed into Jetty/TomcatGood for PoC and getting to know the frameworkAnother option is to bundle Jetty inside your application (as indemo) Tomcat/Jetty OrderRoutes.war camel-camel-camel-core filecxfP2pRoutes.war camel-camel- camel-core file cxf PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 26. For transactional use-cases and messaging, consider deployingto your standard Java EE container (JBoss, Glassfish,Weblogic, Websphere)Java EE container OrderRoutes.war camel-camel-camel-core jms jdbcP2pRoutes.war camel-camel- camel-core jms jdbcJMS /JPA /JTA JCA adapters DataSources PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 27. To scale go full OSGI, deploying to Fuse ESB/ServiceMix Removes need to bundle Camel in application Makes multi-versions at runtime possible Extra features for route management etc.. Fuse ESB/ServiceMix camel-core1.0 OrderRoutes.jar camel-jms 1.0 camel-jdbc1.0P2pRoutes.jarcamel-... x.x PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 28. More info atCamel Official site http://camel.apache.org/FuseSource webinars http://fusesource.com/resources/video-archived-webinars/Open Source SOA with Fuse http://www.parleys.com/#sl=2&st=5&id=1577And of course, the Camel In Action book! PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING 29. DEMO!PRODUCTS CONSULTING APPLICATION MANAGEMENT IT OPERATIONS SUPPORT TRAINING