fusesource camel-persistence-part2-webinar-charles-moulliard
DESCRIPTION
Second part of my webinar about Transaction Management with Camel on Fuse ESB / Apache ServiceMix. Include also persistence with Idempotent consumer and aggregator EIP patternsTRANSCRIPT
Copyright © 2010 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 1 A Progress So3ware Company
A Progress So3ware Company
Transac6on Management with Apache Camel, ServiceMix -‐ EIPs Part II
Charles Moulliard – Raul Kripalani, November 22nd, 2011
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 2 A Progress So3ware Company
When you joined today’s session …
Audio is broadcast from your computer
Submit your questions via the Chat Window
Contact today’s Host via the Chat Window
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 3 A Progress So3ware Company
Learn More at hLp://fusesource.com
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 4 A Progress So3ware Company
Speaker – Charles Moulliard
Engineer in Agronomy & Master in Zoology
17 years of experience in IT world development (J2EE, .NET) • Specialised in new technologies
web/Internet & integra6on
• Project manager in bank, financial, telco, insurance and transport world
Solu6on Architect at FuseSource CommiLer on Apache projects :
• Apache ServiceMix, Apache Karaf (PMC) , Apache Camel, Fuse Fabric, Scalate
Follow me on :
hLp://twiLer.com/cmoulliard
hLp://www.linkedin.com/in/charlesmoulliard
hLp://cmoulliard.blogspot.com
hLp://www.slideshare.net/cmoulliard
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 5 A Progress So3ware Company
Agenda
Goal : Transac6on Management with Apache Camel, ServiceMix -‐ EIPs • Transac6ons with Camel • JMS and JDBC alone • Fuse ESB Tx Manager
• Using Global Transac6on Manager (XA) • Persistence with EIPs
• Idempotent processor
• Aggregator • Summary
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 6 A Progress So3ware Company
Table of contents
Transac6ons with Camel JMS and JDBC alone
Fuse ESB Tx Manager
Using Global Transac6on Manager (XA)
Persistence with EIPs • Idempotent processor • Aggregator
Summary
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 7 A Progress So3ware Company
Transac6ons with Camel
Camel does not re-‐invent the wheel and use Spring or Blueprint API (on Apache ServiceMix)
Can work with local or provided Tx Manager • Local = Spring (JMS, JPA, JDBC, Hibernate), Atomikos, Bitronix, jOTM
• Provided = J2EE (WebSphere, JBoss, Glassfish, BEA, Geronimo), Apache ServiceMix
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 8 A Progress So3ware Company
Transac6ons with Camel
Factors affec6ng Architecture depends on : • Component (Transac6onal or not), • Type of Transac6on Manager and DataSource (XA or not),
• Route (simple, complex), • Transacrion Propaga6on Type (REQUIRED, NEW, MANDATORY, ….)
How to configure a Camel route to be transac6onal 2 approaches exist and they can be combined
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 9 A Progress So3ware Company
Transac6ons with Camel (1)
A) Using a Transac6onal component for producer or consumer
How To : Check which component are Tx
aware jpa, sql, jms, file, hibernate, iba6s.
Configure a Transac6on Manager for the endpoint
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 10 A Progress So3ware Company
Transac6ons with Camel (2)
Simple Transac6onal JMS client configura6on example
Spring JMS Tx manager is defined
Here we provide the parameters
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 11 A Progress So3ware Company
Transac6ons with Camel (3)
B) Configure a Camel route as Transac6onal
Advantage : allow to include processors (beans, jpa, hibernate) with a Transac6onal route
How to configure it add <transacted> to the route How does it work
• Camel will automa6c lookup to find a Spring transac6on manager (org.springframework.transac6on.PlasormTransac6onManager)
• Transac6on Manager creates a Transac6on (according to PROPAGATION Policy)
• Transac6onError handler will take care of excep6ons occuring during camel exchange and propagated it to the caller
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 12 A Progress So3ware Company
Transac6ons with Camel (4)
Example of Camel Transac6on route A
B
Camel will lookup for “PROPAGATION_REQUIRED”
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 13 A Progress So3ware Company
Transac6ons with Camel (5)
Reference to the bean
Reference of the SpringTransac6onPolicy can be provided
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 14 A Progress So3ware Company
Transac6ons with Camel (6)
Another example using <Policy> DSL and SpringTransac6onPolicy is provided to the TxErrorHandler
Transacted is replaced by Policy
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 15 A Progress So3ware Company
Transac6ons with Camel (7)
Transac6on policy can be defined using class org.apache.camel.spi.TransactedPolicy
Policies can be mixed in Camel routes to use different transac6on propaga6ons (NEW, REQUIRED, …)
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 16 A Progress So3ware Company
Transac6ons with Camel (8)
Example of 2 Camel routes using different Policy Required policy : will use an existing Transaction or request to create a new
Requires-New policy will request a new Transaction
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 17 A Progress So3ware Company
Transac6ons with Camel (9)
Transac6on rollback occurs when a run6me excep6on is raised but you can also manage it using <rollback> DSL
We force a Rollback by generating a org.apache.camel.RollbackExchangeException
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 18 A Progress So3ware Company
Transac6ons with Camel (9)
Using markRollBackOnly generates a Rollback without excep6on in the Camel route
Rollback occurs in BookService (DB), exception is not send to direct (which is not Tx aware) and exception is send to mock endpoint
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 19 A Progress So3ware Company
Transac6ons with Camel (10)
When Apache Camel producer/consumer is not Transac6onal, a compensa6on mechanism can be used.
Uses a synchronisa6on mechanism = callback (mimic Tx) to group tasks as a UnitOfWork
Will react with onComplete (=commit) or onFailure (=rollback)
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 20 A Progress So3ware Company
Transac6ons with Camel (11)
onCompletion + link to the bean implementing the rollback logic
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 21 A Progress So3ware Company
Transac6ons with Camel (12)
Rollback strategy
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 22 A Progress So3ware Company
Transac6ons with Camel (13)
Be carefull about threads, threadPool • Only one should be used, • Don't use seda://, vm://, NMR:// which are async to link Camel routes
Tx async are not supported in Camel 2.x (planned for Camel 3.x)
More info : hLp://fusesource.com/docs/router/2.8/transac6ons/TxnDemarca6on-‐ErrorHandling.html
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 23 A Progress So3ware Company
Table of contents
Transac6ons with Camel JMS and JDBC alone
Fuse ESB Tx Manager
Using a Global Transac6on Manager (XA)
Persistence with EIPs • Idempotent processor • Aggregator
Summary
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 24 A Progress So3ware Company
JMS and JDBC
Integra6on project, most probably, will use not only 1 Tx Manager but a combina6on using JMS + JDBC, JDBC + JDBC, JMS + JMS, …
Such a project can use Spring or Fuse ESB Tx Managers 2 Approaches are available on Fuse ESB
• Spring / Spring DM (Dynamic Modules)
• Aries Blueprint Apache Aries project = implementa6on of Enterprise features of OSGI EE specifica6ons (Transac6on, JPA, JNDI, Web)
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 25 A Progress So3ware Company
JMS and JDBC
Use Case descrip6on
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 26 A Progress So3ware Company
JMS and JDBC (1) -‐ Spring
Example of a project containing a JPA, Datasource and a camel route with Ac6veMQ JMS queues
DataSource is created using Apache Commons DBCP
And is exported as OSGI Service wit h2 filters
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 27 A Progress So3ware Company
JMS and JDBC (2) -‐ Spring
JPA Persistence file – transac6on-‐type = "RESOURCE-‐LOCAL"
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 28 A Progress So3ware Company
JMS and JDBC (3) -‐ Spring
Spring will inject persistence context
Method will be added into an existing Transaction
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 29 A Progress So3ware Company
JMS and JDBC (4) -‐ Spring DAO will be scanned by spring to find @Annotation
Our Spring JPA Tx Manager is exported and will be used by Camel route
DAO is exposed as a service
Spring will add Tx demarcation
Based on @Transactional annotations
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 30 A Progress So3ware Company
JMS and JDBC (5) -‐ Spring Spring JMS Tx Manager
Message rollbacked will be moved directly into the DLQ
Transacted = true, ref to Tx manager, CACHE_NONE to avoid that Spring uses another session (thread))
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 31 A Progress So3ware Company
Camel Spring configura6on
JMS and JDBC (6) -‐ Spring We search for JPA Tx Manager
We configure 2 Transaction Policy (one for JMS, other for JPA)
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 32 A Progress So3ware Company
JMS and JDBC (5) -‐ Spring
JMS + JDBC
Apache Camel routes
JMS + JMS
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 33 A Progress So3ware Company
Table of contents
Transac6ons with Camel JMS and JDBC alone
Fuse ESB Tx Manager
Using Global Transac6on Manager (XA)
Persistence with EIPs • Idempotent processor • Aggregator
Summary
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 34 A Progress So3ware Company
Fuse ESB – Transac6on Manager
Apache ServiceMix 4.x plasorm (Fuse ESB) provides a Tx Manager
Based on Apache Geronimo Tx Manager (JTA 1.1)
Deployed using feature transac6on Contains the following bundle
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 35 A Progress So3ware Company
Fuse ESB – Transac6on Manager (1)
Can be used as a Global JTA Tx Manager with XA Resource
Expose through OSGI service registry interfaces
Proxified objects available
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 36 A Progress So3ware Company
Fuse ESB – Transac6on Manager (2)
Timeout and Tx log file can be customised
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 37 A Progress So3ware Company
Table of contents
Transac6ons with Camel JMS and JDBC alone
Fuse ESB Tx Manager
Using Global Transac6on Manager (XA)
Persistence with EIPs • Idempotent processor • Aggregator
Summary
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 38 A Progress So3ware Company
Global Tx Manager -‐ XA
Using a Global Transac6on Manager on Fuse ESB just requires that we use JTA and configure XA resources
Verify careful regarding to the choice of the "drivers" or "datasource" that you will use (XA aware)
You do not need to use Spring JDBC, JPA, JMS, Hibernate Transac6on Manager Aries Tx Manager
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 39 A Progress So3ware Company
Global Tx Manager -‐ XA
Use Case
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 40 A Progress So3ware Company
Global Tx Manager -‐ XA (1)
What will change : Datasource (JDBC) Add a XA Datasource
Export XA and non XA Datasource as OSGI services (will be used by JPA container)
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 41 A Progress So3ware Company
Global Tx Manager -‐ XA (2)
JPA -‐ persistence.xml Use JTA instead of RESOURCE-LOCAL
Configure JTA and non-JTA datasource
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 42 A Progress So3ware Company
Global Tx Manager -‐ XA (3)
JPA -‐ spring We call OSGI Service to retrieve the PlatformTransactionManager object managed by Aries TX Manager
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 43 A Progress So3ware Company
Global Tx Manager -‐ XA (4)
JMS – XA Connec6on Pool / Resource Manager
ActiveMQ requires JTA Manager and XAPooledConnectionFactory
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 44 A Progress So3ware Company
Global Tx Manager -‐ XA (4)
JMS – Ac6veMQ component
Transacted is false in this configuration as Aries Tx xManager will take care !
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 45 A Progress So3ware Company
Global Tx Manager -‐ XA (1)
Apache Camel Route We use the Global Tx Manager exposed as OSGI Service through Spring TransactionManager to get it for JPA Service
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 46 A Progress So3ware Company
Global Tx Manager -‐ XA (1)
Apache Camel Route Nothing has changed except that you could use XA and non XA connection pool for JMS
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 47 A Progress So3ware Company
Demo Transac6ons in Ac6on on Fuse ESB !
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 48 A Progress So3ware Company
Table of contents
Transac6ons with Camel JMS and JDBC alone
Fuse ESB Tx Manager
Using a Global Transac6on Manager (XA)
Persistence with EIPs • Idempotent processor • Aggregator
Summary
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 49 A Progress So3ware Company
Idempotent processor
Filter out duplicate messages Uses an Expression to calculate a unique message ID, which is looked up in the repository
By default, memory based Supports several persistent mode
• FileIdempotentRepository
• HazelcastIdempotentRepository (Available as of Camel 2.8) • JdbcMessageIdRepository (Available as of Camel 2.7)
• JpaMessageIdRepository
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 50 A Progress So3ware Company
Idempotent processor (1)
To use JPA Repository, simply configure a persistence file
We use as Model MessageProcessed
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 51 A Progress So3ware Company
Idempotent processor (2)
Model contains info about propre6es that we will store into the DB
MessageID = key used to filter out messages
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 52 A Progress So3ware Company
Idempotent processor (3)
Spring Tx and JPA Template must be configured
Spring Template
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 53 A Progress So3ware Company
Idempotent processor (3)
Setup JpaMessageIdRepository
Reference to the JPA template
Ref for the Spring bean jpaStore
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 54 A Progress So3ware Company
Demo Idempotent Consumer with JPA repo
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 55 A Progress So3ware Company
Table of contents
Transac6ons with Camel JMS and JDBC alone
Fuse ESB Tx Manager
Using a Global Transac6on Manager (XA)
Persistence with EIPs • Idempotent processor • Aggregator
Summary
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 56 A Progress So3ware Company
Aggregator
Allows to combine messages together into a single message
Support : • JdbcAggregatorRepository (Available as of Camel 2.7)
• HawtDBAggregatorRepository (in-‐memory DB)
We keep in DB the aggregated object as blob
Allows to restart aggrega6on process from where it stops !
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 57 A Progress So3ware Company
Aggregator (1)
How agregator works
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 58 A Progress So3ware Company
Aggregator (2)
Configura6on of the Persistent Repository
And DB
Configure Repo with DataSource and Tx Manager
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 59 A Progress So3ware Company
Aggregator (3)
MyAggrega6onstrategy simply concatenates the Body using old and new exchange
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 60 A Progress So3ware Company
Aggregator (4)
Class contains Users and set the Key id = FUSE that the aggregator will use on the Exchange
Id = key used
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 61 A Progress So3ware Company
Aggregator (5)
Apache Camel route refers to the Persistent Repository
Persistent repository
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 62 A Progress So3ware Company
Demo Aggrega6on with JDBC repository
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 63 A Progress So3ware Company
Table of contents
Transac6ons with Camel Fuse ESB Tx Manager
JMS and JDBC alone
Using a Global Transac6on Manager (XA)
Persistence with EIPs • Idempotent processor • Aggregator
Summary
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 64 A Progress So3ware Company
Summary
Transac6ons in more detail • Transac6onal client <-‐> Transacted / Policy mode • Propaga6onPolicy • Rollback strategies • UnitOfWork to synchronise
Present a configura6on with JDBC, JMS Tx managers Introduce Fuse ESB Tx Manager Global Transac6on Manager
EIPs persistent • Idempotent processor/Consumer
• Aggregator
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 65 A Progress So3ware Company
For More Informa6on
fusesource.com • Free downloads • Documenta6on • Geyng Star6ng demo, tools and advice
• Webinars on beginner and advanced topics
• White papers
• Customer case studies
Get Help • Forums
• Virtual training • Consul6ng • Enterprise subscrip6ons
Copyright © 2011 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. 66 A Progress So3ware Company
Useful references
Transac6ons with camel / Fuse ESB
• hLp://fusesource.com/docs/router/2.8/transac6ons/index.html
EIPs paLerns • hLp://fusesource.com/docs/router/2.8/eip/MsgEnd-‐Idempotent.html
• hLp://fusesource.com/docs/router/2.8/eip/MsgRout-‐Aggregator.html
Use Case – Devoxx Hands on Lab • hLps://github.com/cmoulliard/Devoxx-‐2011-‐HandsOnLab