SlideShare a Scribd company logo
Swiss Army Knife  Spring Mario Fusco [email_address] Twitter: @mariofusco
Agenda System architecture The big picture How Spring fits all Spring features Decoupling with application  events Implementing a workflow with  jBPM Managing  transactions  declaratively
Web Layer Services Workflow XML Marshaller AuditTrail MailSender Data Access Layer Domain Objects DI DI DI DI Acegi Transaction Hibernate JavaMail Sender jBPM MaintenanceMBean JMX Quartz Aspect The big picture Bean Wiring AOP Services JMX External tools Database Event Autosave E ven t
Dependency injection is the primary way that Spring promotes loose coupling among application objects … Bean A Bean A Bean B Decoupling dependecies with DI Dependency Injection
Decoupling dependecies …  but it isn’t the only way
The loosest coupled way for objects to  interact with one another is to publish  and listen for  application events . An  event publisher   object can communicate with other objects without even knowing which objects are listening Publisher Listener Publisher/ Listener Event A Event B Event C An  event listener  can react to events without knowing which object published the events In Spring, any bean in the container can be either an event listener, a publisher, or  both Decoupling with applications events
Publishing events public class CustomEvent extends ApplicationEvent { public CustomEvent(Object source) { super(source); } } To publish an event first it needs to  define the event   itself: Next you can  publish the event   through the  ApplicationContext : applicationContext.publishEvent(new CustomEvent(this)); This means that, in order to publish events, your beans will need to have access to the  ApplicationContext . The easiest way to achieve it is to  hold a static reference  to the context   in a singleton that’s made aware of the container by implementing  ApplicationContextAware
Listening for application events To allow a bean to listen for application events, it needs to register it within the Spring context and to implement the  ApplicationListener  interface public class CustomListener implements ApplicationListener { public void onApplicationEvent(ApplicationEvent event) { if (event instanceof TypedEvent) { // Reacts the the event ... } // Discards the non interesting event } } Unfortunately, this will make your bean to  listen for ALL the events   triggered inside the container and you’ll have to  discard  the non  Interesting ones (the biggest part) typically by some  instanceof
Listening for a specific event We fixed this issue by  replacing  the Spring’s default  events multicaster   with one that notifies only the interested listener <bean id=&quot;applicationEventMulticaster&quot;  class=&quot;ch.exm.util.event.TypedApplicationEventMulticaster“/>
Overriding the event multicaster public class TypedApplicationEventMulticaster  extends SimpleApplicationEventMulticaster { public void addApplicationListener(ApplicationListener listener) { if (listener instanceof TypedApplicationListener) { listenerMap.put(listener.getListenedEventClass(), listener); } else super.addApplicationListener(listener); } public void multicastEvent(ApplicationEvent event) {   notifyListener(listenerMap.get(event.getClass())); } private void notifyListener(TypedApplicationListener listener) { getTaskExecutor().execute(new Runnable() { public void run() { listener.onTypedEvent(event); }}); } indexes the listeners by listened event type notifies only the interested listeners … …  in a separate thread
Listening for a specific event Through the  TypedApplicationEventMulticaster  your bean can be  notified of just   a specific class of events by implementing this interface interface TypedApplicationListener<T extends ApplicationEvent>  extends ApplicationListener { void onTypedEvent(T event); Class<T> getListenedEventClass(); } or even easier by extending the following abstract class abstract class TypedApplicationListenerAdapter<T>  implements TypedApplicationListener<T> { public void onApplicationEvent(ApplicationEvent event) { onTypedEvent((T) event); } } The event multicaster notifies this listener only for the event of type T Declares the Class of events this bean is listening for Reacts only to events of type T
Implementing a workflow with jBPM
Web Layer Services Workflow Autosave XML Marshaller AuditTrail MailSender Data Access Layer Domain Objects DI DI DI DI Acegi Transaction Event Hibernate JavaMail Sender jBPM MaintenanceMBean JMX Quartz Aspect The big picture Bean Wiring AOP Services JMX External tools Database Integrating jBPM and Spring jBPM
Implementing a workflow with jBPM It has an  extensible engine   that executes process definitions with tasks, fork/join nodes, events, timers, automated actions, etc. jBPM is the JBoss implementation of a BPM ( Business Process Management ) system. Its main  characteristics and feature are: It can be configured with any database and it can be deployed on any application server or used a simple java library It allows to  configure a workflow process   via a simple XML file where workflow’s state and transition are defined as it follows <start-state name=&quot;start&quot;> <transition name=&quot;start&quot; to=&quot;prospect&quot;></transition> </start-state> <state name=&quot;prospect&quot;> <transition name=&quot;request_approval&quot; to=&quot;uw_approval“/> <transition name=&quot;request_quotation&quot; to=&quot;retro_quotation“/> </state>
An insurance p ol icy lifecycle
Integrating jBPM in Spring There's a Spring Module that makes it easy to  wire jBPM   with Spring It allows jBPM's underlying Hibernate  SessionFactory  to be configured through Spring and jBPM actions to access Spring's context … …  and offers  convient ways of working directly with process definitions as well as jBPM API through the  JbpmTemplate  <bean id=&quot;jbpmConfiguration&quot; class=&quot;org.springmodules. workflow.jbpm31.LocalJbpmConfigurationFactoryBean&quot;> <property name=&quot;sessionFactory&quot; ref=&quot;sessionFactory&quot;/> <property name=&quot;configuration&quot; value=&quot;classpath:jbpm.cfg.xml&quot;/> </bean> <bean id=&quot;jbpmTemplate&quot; class=&quot;org.springmodules. workflow.jbpm31.JbpmTemplate&quot;> <property name=&quot; jbpmConfiguration&quot; ref=&quot; jbpmConfiguration&quot;/> </bean>
Calling jBPM API with JbpmTemplate JbpmTemplate  eases to  work with the jBPM API  t aking care of handling exceptions, the underlying Hibernate session and the jBPM context.   For instance to  execute a workflow transition  in a transactional way: jbpmTemplate.signal(processInstance, transactionId); It’s also possible, as with every  Spring-style template , to directly access to the native  JbpmContext  through the  JbpmCallback :  public ProcessInstance createProcessInstance() { return (ProcessInstance) jbpmTemplate. execute(new JbpmCallback(){  public Object doInJbpm(JbpmContext context) {  GraphSession g = context.getGraphSession(); ProcessDefinition   def = g.findLatestProcessDefinition(); ProcessInstance instance = def.createProcessInstance(); jbpmContext.save(instance); return instance; } }); }
Transactions in Spring
Web Layer Services Workflow Autosave XML Marshaller AuditTrail Data Access Layer Domain Objects DI DI DI DI Acegi Transaction Event Hibernate JavaMail Sender jBPM MaintenanceMBean JMX Quartz Aspect The big picture Bean Wiring AOP Services JMX External tools Database MailSender Transactions in Spring nsa Tra ction
Transaction’s attributes In Spring declarative transactions are implemented through its AOP framework and are defined with the following attributes: Isolation level  defines how much a transaction may be impacted by the activities of other concurrent transactions Rollback rules  define what exception prompt a rollback (by default only the runtime ones) and which ones do not Timeout Propagation behavior  defines the boundaries of the transaction Read-only Transaction A -  REQUIRES_NEW Transaction B REQUIRED Propagation Read-only & Timeout Exception? Commit Rollback Transaction C Isolation REPEATABLE_READ
Choosing a transaction manager Spring supports transactions  programmatically  and even  declaratively  by proxying beans with AOP. But unlike EJB, that’s coupled with a JTA (Java Transaction API) implementation, it  employs a callback mechanism  that abstracts the actual transaction implementation from the transactional code. Spring  does not directly manage transactions  but, it comes with a set of transaction managers that  delegate responsibility   for transaction management to a platform-specific transaction implementation.  For example if your application’s persistence is handled by Hibernate then you’ll choose to delegate responsibility for transaction management to an  org.hibernate.Transaction  object with the following manager: <bean id=&quot;transactionManager“ class=&quot;org.springframework. orm.hibernate3.HibernateTransactionManager&quot;> <property name=&quot;sessionFactory&quot; ref=“sessionFactory&quot;/>  <property name=&quot;dataSource&quot; ref=“dataSource&quot;/> </bean>
Managing transactions declaratively Spring 2.0 adds  two new kinds  of declarative transactions that can be configured through the elements in the tx namespace: xmlns:tx=http://www.springframework.org/schema/tx by adding the spring-tx-2.0.xsd schema to the Spring configuration file: http://www.springframework.org/schema/tx/spring-tx-2.0.xsd The first way to declare transactions is with the  <tx:advice>  XML element : <tx:advice id=&quot;txAdvice“ transaction-manager=&quot;transactionManager&quot;> <tx:attributes> <tx:method name=“set*&quot; propagation=&quot;REQUIRED&quot; /> <tx:method name=“get*&quot; propagation=&quot;SUPPORTS“ read-only=&quot;true&quot;/> </tx:attributes></tx:advice> This is only the  transaction advice , but in order  to define where it will be applied, we need a  pointcut  to indicate which beans should be advised: <aop:config> <aop:advisor pointcut=&quot;execution(* *..MyBean.*(..))“  </aop:config>   advice-ref=&quot;txAdvice&quot;/>
Defining annotation-driven transactions The second (and easiest) way to declaratively define transactions in Spring 2.0 is through  annotations . This mechanism can be enabled as simple as adding the following line of XML to the application context: <tx:annotation-driven transaction-manager=&quot;transactionManager&quot;/> This configuration element tells Spring to  automatically advise , with the transaction advice, all the beans in the application context that are annotated with  @Transactional , either at the class level or at the method level. This annotation may also be applied to an interface. The  transaction attributes   of the advice are defined by parameters of the  @Transactional  annotation. For example our previously illustrated   WorkflowService  performs a workflow transition in a transactional way: @Transactional(propagation=Propagation. REQUIRED , readOnly=false) void doWkfTransition(WkfObj wkfObj, String transitionName);
Mario Fusco Head of IT Development [email_address]

More Related Content

PPTX
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Mario Fusco
 
PDF
OOP and FP - Become a Better Programmer
Mario Fusco
 
PDF
From object oriented to functional domain modeling
Mario Fusco
 
PDF
FP in Java - Project Lambda and beyond
Mario Fusco
 
PDF
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 
PDF
Java 8 Workshop
Mario Fusco
 
PDF
If You Think You Can Stay Away from Functional Programming, You Are Wrong
Mario Fusco
 
PDF
Pragmatic functional refactoring with java 8
RichardWarburton
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Mario Fusco
 
OOP and FP - Become a Better Programmer
Mario Fusco
 
From object oriented to functional domain modeling
Mario Fusco
 
FP in Java - Project Lambda and beyond
Mario Fusco
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 
Java 8 Workshop
Mario Fusco
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
Mario Fusco
 
Pragmatic functional refactoring with java 8
RichardWarburton
 

What's hot (20)

PPTX
Java 7, 8 & 9 - Moving the language forward
Mario Fusco
 
PDF
Currying and Partial Function Application (PFA)
Dhaval Dalal
 
PDF
Refactoring to Java 8 (Devoxx BE)
Trisha Gee
 
PDF
Pragmatic functional refactoring with java 8 (1)
RichardWarburton
 
PDF
Jumping-with-java8
Dhaval Dalal
 
PPTX
Typescript barcelona
Christoffer Noring
 
PDF
DRYing to Monad in Java8
Dhaval Dalal
 
PDF
Creating Lazy stream in CSharp
Dhaval Dalal
 
PDF
JavaScript Functions
Colin DeCarlo
 
PDF
Object Design - Part 1
Dhaval Dalal
 
PDF
ReactiveCocoa in Practice
Outware Mobile
 
PPTX
Rxjs ppt
Christoffer Noring
 
PDF
Monadic Java
Mario Fusco
 
PPTX
Intro to Javascript
Anjan Banda
 
PDF
Java Simple Programs
Upender Upr
 
PDF
Booting into functional programming
Dhaval Dalal
 
PPTX
LinkedIn TBC JavaScript 100: Functions
Adam Crabtree
 
PPTX
C++ Functions
Jari Abbas
 
PDF
Java 8 Lambda Built-in Functional Interfaces
Ganesh Samarthyam
 
PDF
Learn You a ReactiveCocoa for Great Good
Jason Larsen
 
Java 7, 8 & 9 - Moving the language forward
Mario Fusco
 
Currying and Partial Function Application (PFA)
Dhaval Dalal
 
Refactoring to Java 8 (Devoxx BE)
Trisha Gee
 
Pragmatic functional refactoring with java 8 (1)
RichardWarburton
 
Jumping-with-java8
Dhaval Dalal
 
Typescript barcelona
Christoffer Noring
 
DRYing to Monad in Java8
Dhaval Dalal
 
Creating Lazy stream in CSharp
Dhaval Dalal
 
JavaScript Functions
Colin DeCarlo
 
Object Design - Part 1
Dhaval Dalal
 
ReactiveCocoa in Practice
Outware Mobile
 
Monadic Java
Mario Fusco
 
Intro to Javascript
Anjan Banda
 
Java Simple Programs
Upender Upr
 
Booting into functional programming
Dhaval Dalal
 
LinkedIn TBC JavaScript 100: Functions
Adam Crabtree
 
C++ Functions
Jari Abbas
 
Java 8 Lambda Built-in Functional Interfaces
Ganesh Samarthyam
 
Learn You a ReactiveCocoa for Great Good
Jason Larsen
 
Ad

Viewers also liked (8)

PDF
Seven Deadly Sins
Markus Eisele
 
PPTX
Scala - where objects and functions meet
Mario Fusco
 
PDF
Why we cannot ignore Functional Programming
Mario Fusco
 
PDF
Hammurabi
Mario Fusco
 
PDF
Reactive Programming for a demanding world: building event-driven and respons...
Mario Fusco
 
PDF
Comparing different concurrency models on the JVM
Mario Fusco
 
PPTX
Real world DSL - making technical and business people speaking the same language
Mario Fusco
 
ODP
Drools 6 deep dive
Mario Fusco
 
Seven Deadly Sins
Markus Eisele
 
Scala - where objects and functions meet
Mario Fusco
 
Why we cannot ignore Functional Programming
Mario Fusco
 
Hammurabi
Mario Fusco
 
Reactive Programming for a demanding world: building event-driven and respons...
Mario Fusco
 
Comparing different concurrency models on the JVM
Mario Fusco
 
Real world DSL - making technical and business people speaking the same language
Mario Fusco
 
Drools 6 deep dive
Mario Fusco
 
Ad

Similar to Swiss army knife Spring (20)

PDF
Getting Started With Spring Framework J Sharma Ashish Sarin
moineittay
 
PDF
Spring.io
Cédric GILLET
 
PPT
Spring Framework
nomykk
 
PPTX
Spring 1 day program
Mohit Kanwar
 
PPT
Spring talk111204
ealio
 
PPTX
Skillwise-Spring framework 1
Skillwise Group
 
PPT
Spring, web service, web server, eclipse by a introduction sandesh sharma
Sandesh Sharma
 
PPT
Spring
s4al_com
 
PPT
Spring talk111204
s4al_com
 
PDF
Lo nuevo en Spring 3.0
David Motta Baldarrago
 
PPT
Spring integration with jBPM4
Andries Inzé
 
ODP
springmvc-150923124312-lva1-app6892
Tuna Tore
 
ODP
Java Spring MVC Framework with AngularJS by Google and HTML5
Tuna Tore
 
ODP
Spring Mvc,Java, Spring
ifnu bima
 
PPT
Spring and DWR
wiradikusuma
 
PPTX
Next stop: Spring 4
Oleg Tsal-Tsalko
 
PPT
Spring frame work
husnara mohammad
 
PDF
Spring 2
Aruvi Thottlan
 
PDF
Spring Start Here Learn What You Need And Learn It Well 1st Edition Laurentiu...
wktamhyv6089
 
PDF
Spring In Action 1 Illustrated Edition Craig Walls Ryan Breidenbach
sapatpaties
 
Getting Started With Spring Framework J Sharma Ashish Sarin
moineittay
 
Spring.io
Cédric GILLET
 
Spring Framework
nomykk
 
Spring 1 day program
Mohit Kanwar
 
Spring talk111204
ealio
 
Skillwise-Spring framework 1
Skillwise Group
 
Spring, web service, web server, eclipse by a introduction sandesh sharma
Sandesh Sharma
 
Spring
s4al_com
 
Spring talk111204
s4al_com
 
Lo nuevo en Spring 3.0
David Motta Baldarrago
 
Spring integration with jBPM4
Andries Inzé
 
springmvc-150923124312-lva1-app6892
Tuna Tore
 
Java Spring MVC Framework with AngularJS by Google and HTML5
Tuna Tore
 
Spring Mvc,Java, Spring
ifnu bima
 
Spring and DWR
wiradikusuma
 
Next stop: Spring 4
Oleg Tsal-Tsalko
 
Spring frame work
husnara mohammad
 
Spring 2
Aruvi Thottlan
 
Spring Start Here Learn What You Need And Learn It Well 1st Edition Laurentiu...
wktamhyv6089
 
Spring In Action 1 Illustrated Edition Craig Walls Ryan Breidenbach
sapatpaties
 

More from Mario Fusco (7)

PDF
Kogito: cloud native business automation
Mario Fusco
 
PDF
Let's make a contract: the art of designing a Java API
Mario Fusco
 
PDF
How and why I turned my old Java projects into a first-class serverless compo...
Mario Fusco
 
PDF
OOP and FP
Mario Fusco
 
PDF
Lazy java
Mario Fusco
 
PDF
Introducing Drools
Mario Fusco
 
PDF
No more loops with lambdaj
Mario Fusco
 
Kogito: cloud native business automation
Mario Fusco
 
Let's make a contract: the art of designing a Java API
Mario Fusco
 
How and why I turned my old Java projects into a first-class serverless compo...
Mario Fusco
 
OOP and FP
Mario Fusco
 
Lazy java
Mario Fusco
 
Introducing Drools
Mario Fusco
 
No more loops with lambdaj
Mario Fusco
 

Recently uploaded (20)

PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 

Swiss army knife Spring

  • 1. Swiss Army Knife Spring Mario Fusco [email_address] Twitter: @mariofusco
  • 2. Agenda System architecture The big picture How Spring fits all Spring features Decoupling with application events Implementing a workflow with jBPM Managing transactions declaratively
  • 3. Web Layer Services Workflow XML Marshaller AuditTrail MailSender Data Access Layer Domain Objects DI DI DI DI Acegi Transaction Hibernate JavaMail Sender jBPM MaintenanceMBean JMX Quartz Aspect The big picture Bean Wiring AOP Services JMX External tools Database Event Autosave E ven t
  • 4. Dependency injection is the primary way that Spring promotes loose coupling among application objects … Bean A Bean A Bean B Decoupling dependecies with DI Dependency Injection
  • 5. Decoupling dependecies … but it isn’t the only way
  • 6. The loosest coupled way for objects to interact with one another is to publish and listen for application events . An event publisher object can communicate with other objects without even knowing which objects are listening Publisher Listener Publisher/ Listener Event A Event B Event C An event listener can react to events without knowing which object published the events In Spring, any bean in the container can be either an event listener, a publisher, or both Decoupling with applications events
  • 7. Publishing events public class CustomEvent extends ApplicationEvent { public CustomEvent(Object source) { super(source); } } To publish an event first it needs to define the event itself: Next you can publish the event through the ApplicationContext : applicationContext.publishEvent(new CustomEvent(this)); This means that, in order to publish events, your beans will need to have access to the ApplicationContext . The easiest way to achieve it is to hold a static reference to the context in a singleton that’s made aware of the container by implementing ApplicationContextAware
  • 8. Listening for application events To allow a bean to listen for application events, it needs to register it within the Spring context and to implement the ApplicationListener interface public class CustomListener implements ApplicationListener { public void onApplicationEvent(ApplicationEvent event) { if (event instanceof TypedEvent) { // Reacts the the event ... } // Discards the non interesting event } } Unfortunately, this will make your bean to listen for ALL the events triggered inside the container and you’ll have to discard the non Interesting ones (the biggest part) typically by some instanceof
  • 9. Listening for a specific event We fixed this issue by replacing the Spring’s default events multicaster with one that notifies only the interested listener <bean id=&quot;applicationEventMulticaster&quot; class=&quot;ch.exm.util.event.TypedApplicationEventMulticaster“/>
  • 10. Overriding the event multicaster public class TypedApplicationEventMulticaster extends SimpleApplicationEventMulticaster { public void addApplicationListener(ApplicationListener listener) { if (listener instanceof TypedApplicationListener) { listenerMap.put(listener.getListenedEventClass(), listener); } else super.addApplicationListener(listener); } public void multicastEvent(ApplicationEvent event) { notifyListener(listenerMap.get(event.getClass())); } private void notifyListener(TypedApplicationListener listener) { getTaskExecutor().execute(new Runnable() { public void run() { listener.onTypedEvent(event); }}); } indexes the listeners by listened event type notifies only the interested listeners … … in a separate thread
  • 11. Listening for a specific event Through the TypedApplicationEventMulticaster your bean can be notified of just a specific class of events by implementing this interface interface TypedApplicationListener<T extends ApplicationEvent> extends ApplicationListener { void onTypedEvent(T event); Class<T> getListenedEventClass(); } or even easier by extending the following abstract class abstract class TypedApplicationListenerAdapter<T> implements TypedApplicationListener<T> { public void onApplicationEvent(ApplicationEvent event) { onTypedEvent((T) event); } } The event multicaster notifies this listener only for the event of type T Declares the Class of events this bean is listening for Reacts only to events of type T
  • 13. Web Layer Services Workflow Autosave XML Marshaller AuditTrail MailSender Data Access Layer Domain Objects DI DI DI DI Acegi Transaction Event Hibernate JavaMail Sender jBPM MaintenanceMBean JMX Quartz Aspect The big picture Bean Wiring AOP Services JMX External tools Database Integrating jBPM and Spring jBPM
  • 14. Implementing a workflow with jBPM It has an extensible engine that executes process definitions with tasks, fork/join nodes, events, timers, automated actions, etc. jBPM is the JBoss implementation of a BPM ( Business Process Management ) system. Its main characteristics and feature are: It can be configured with any database and it can be deployed on any application server or used a simple java library It allows to configure a workflow process via a simple XML file where workflow’s state and transition are defined as it follows <start-state name=&quot;start&quot;> <transition name=&quot;start&quot; to=&quot;prospect&quot;></transition> </start-state> <state name=&quot;prospect&quot;> <transition name=&quot;request_approval&quot; to=&quot;uw_approval“/> <transition name=&quot;request_quotation&quot; to=&quot;retro_quotation“/> </state>
  • 15. An insurance p ol icy lifecycle
  • 16. Integrating jBPM in Spring There's a Spring Module that makes it easy to wire jBPM with Spring It allows jBPM's underlying Hibernate SessionFactory to be configured through Spring and jBPM actions to access Spring's context … … and offers convient ways of working directly with process definitions as well as jBPM API through the JbpmTemplate <bean id=&quot;jbpmConfiguration&quot; class=&quot;org.springmodules. workflow.jbpm31.LocalJbpmConfigurationFactoryBean&quot;> <property name=&quot;sessionFactory&quot; ref=&quot;sessionFactory&quot;/> <property name=&quot;configuration&quot; value=&quot;classpath:jbpm.cfg.xml&quot;/> </bean> <bean id=&quot;jbpmTemplate&quot; class=&quot;org.springmodules. workflow.jbpm31.JbpmTemplate&quot;> <property name=&quot; jbpmConfiguration&quot; ref=&quot; jbpmConfiguration&quot;/> </bean>
  • 17. Calling jBPM API with JbpmTemplate JbpmTemplate eases to work with the jBPM API t aking care of handling exceptions, the underlying Hibernate session and the jBPM context. For instance to execute a workflow transition in a transactional way: jbpmTemplate.signal(processInstance, transactionId); It’s also possible, as with every Spring-style template , to directly access to the native JbpmContext through the JbpmCallback : public ProcessInstance createProcessInstance() { return (ProcessInstance) jbpmTemplate. execute(new JbpmCallback(){ public Object doInJbpm(JbpmContext context) { GraphSession g = context.getGraphSession(); ProcessDefinition def = g.findLatestProcessDefinition(); ProcessInstance instance = def.createProcessInstance(); jbpmContext.save(instance); return instance; } }); }
  • 19. Web Layer Services Workflow Autosave XML Marshaller AuditTrail Data Access Layer Domain Objects DI DI DI DI Acegi Transaction Event Hibernate JavaMail Sender jBPM MaintenanceMBean JMX Quartz Aspect The big picture Bean Wiring AOP Services JMX External tools Database MailSender Transactions in Spring nsa Tra ction
  • 20. Transaction’s attributes In Spring declarative transactions are implemented through its AOP framework and are defined with the following attributes: Isolation level defines how much a transaction may be impacted by the activities of other concurrent transactions Rollback rules define what exception prompt a rollback (by default only the runtime ones) and which ones do not Timeout Propagation behavior defines the boundaries of the transaction Read-only Transaction A - REQUIRES_NEW Transaction B REQUIRED Propagation Read-only & Timeout Exception? Commit Rollback Transaction C Isolation REPEATABLE_READ
  • 21. Choosing a transaction manager Spring supports transactions programmatically and even declaratively by proxying beans with AOP. But unlike EJB, that’s coupled with a JTA (Java Transaction API) implementation, it employs a callback mechanism that abstracts the actual transaction implementation from the transactional code. Spring does not directly manage transactions but, it comes with a set of transaction managers that delegate responsibility for transaction management to a platform-specific transaction implementation. For example if your application’s persistence is handled by Hibernate then you’ll choose to delegate responsibility for transaction management to an org.hibernate.Transaction object with the following manager: <bean id=&quot;transactionManager“ class=&quot;org.springframework. orm.hibernate3.HibernateTransactionManager&quot;> <property name=&quot;sessionFactory&quot; ref=“sessionFactory&quot;/> <property name=&quot;dataSource&quot; ref=“dataSource&quot;/> </bean>
  • 22. Managing transactions declaratively Spring 2.0 adds two new kinds of declarative transactions that can be configured through the elements in the tx namespace: xmlns:tx=http://www.springframework.org/schema/tx by adding the spring-tx-2.0.xsd schema to the Spring configuration file: http://www.springframework.org/schema/tx/spring-tx-2.0.xsd The first way to declare transactions is with the <tx:advice> XML element : <tx:advice id=&quot;txAdvice“ transaction-manager=&quot;transactionManager&quot;> <tx:attributes> <tx:method name=“set*&quot; propagation=&quot;REQUIRED&quot; /> <tx:method name=“get*&quot; propagation=&quot;SUPPORTS“ read-only=&quot;true&quot;/> </tx:attributes></tx:advice> This is only the transaction advice , but in order to define where it will be applied, we need a pointcut to indicate which beans should be advised: <aop:config> <aop:advisor pointcut=&quot;execution(* *..MyBean.*(..))“ </aop:config> advice-ref=&quot;txAdvice&quot;/>
  • 23. Defining annotation-driven transactions The second (and easiest) way to declaratively define transactions in Spring 2.0 is through annotations . This mechanism can be enabled as simple as adding the following line of XML to the application context: <tx:annotation-driven transaction-manager=&quot;transactionManager&quot;/> This configuration element tells Spring to automatically advise , with the transaction advice, all the beans in the application context that are annotated with @Transactional , either at the class level or at the method level. This annotation may also be applied to an interface. The transaction attributes of the advice are defined by parameters of the @Transactional annotation. For example our previously illustrated WorkflowService performs a workflow transition in a transactional way: @Transactional(propagation=Propagation. REQUIRED , readOnly=false) void doWkfTransition(WkfObj wkfObj, String transitionName);
  • 24. Mario Fusco Head of IT Development [email_address]