SlideShare a Scribd company logo
1 of 24
Download to read offline
Swiss Army Knife  Spring Mario Fusco [email_address] Twitter: @mariofusco
Agenda ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 . ,[object Object],Publisher Listener Publisher/ Listener Event A Event B Event C ,[object Object],[object Object],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 ,[object Object],jBPM is the JBoss implementation of a BPM ( Business Process Management ) system. Its main  characteristics and feature are: ,[object Object],[object Object],<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: ,[object Object],[object Object],[object Object],[object Object],[object Object],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

What's hot

Java 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardJava 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardMario Fusco
 
Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)Dhaval Dalal
 
Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)Trisha Gee
 
Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)RichardWarburton
 
Jumping-with-java8
Jumping-with-java8Jumping-with-java8
Jumping-with-java8Dhaval Dalal
 
DRYing to Monad in Java8
DRYing to Monad in Java8DRYing to Monad in Java8
DRYing to Monad in Java8Dhaval Dalal
 
Creating Lazy stream in CSharp
Creating Lazy stream in CSharpCreating Lazy stream in CSharp
Creating Lazy stream in CSharpDhaval Dalal
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript FunctionsColin DeCarlo
 
Object Design - Part 1
Object Design - Part 1Object Design - Part 1
Object Design - Part 1Dhaval Dalal
 
ReactiveCocoa in Practice
ReactiveCocoa in PracticeReactiveCocoa in Practice
ReactiveCocoa in PracticeOutware Mobile
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to JavascriptAnjan Banda
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple ProgramsUpender Upr
 
Booting into functional programming
Booting into functional programmingBooting into functional programming
Booting into functional programmingDhaval Dalal
 
LinkedIn TBC JavaScript 100: Functions
 LinkedIn TBC JavaScript 100: Functions LinkedIn TBC JavaScript 100: Functions
LinkedIn TBC JavaScript 100: FunctionsAdam Crabtree
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesGanesh Samarthyam
 

What's hot (20)

Java 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardJava 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forward
 
Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)
 
Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)
 
Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)
 
Jumping-with-java8
Jumping-with-java8Jumping-with-java8
Jumping-with-java8
 
Typescript barcelona
Typescript barcelonaTypescript barcelona
Typescript barcelona
 
DRYing to Monad in Java8
DRYing to Monad in Java8DRYing to Monad in Java8
DRYing to Monad in Java8
 
Creating Lazy stream in CSharp
Creating Lazy stream in CSharpCreating Lazy stream in CSharp
Creating Lazy stream in CSharp
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript Functions
 
Object Design - Part 1
Object Design - Part 1Object Design - Part 1
Object Design - Part 1
 
ReactiveCocoa in Practice
ReactiveCocoa in PracticeReactiveCocoa in Practice
ReactiveCocoa in Practice
 
Rxjs ppt
Rxjs pptRxjs ppt
Rxjs ppt
 
Monadic Java
Monadic JavaMonadic Java
Monadic Java
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
Booting into functional programming
Booting into functional programmingBooting into functional programming
Booting into functional programming
 
Pragmatic sbt
Pragmatic sbtPragmatic sbt
Pragmatic sbt
 
LinkedIn TBC JavaScript 100: Functions
 LinkedIn TBC JavaScript 100: Functions LinkedIn TBC JavaScript 100: Functions
LinkedIn TBC JavaScript 100: Functions
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
 

Viewers also liked

Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meetMario Fusco
 
Why we cannot ignore Functional Programming
Why we cannot ignore Functional ProgrammingWhy we cannot ignore Functional Programming
Why we cannot ignore Functional ProgrammingMario Fusco
 
Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...Mario Fusco
 
Comparing different concurrency models on the JVM
Comparing different concurrency models on the JVMComparing different concurrency models on the JVM
Comparing different concurrency models on the JVMMario Fusco
 
Real world DSL - making technical and business people speaking the same language
Real world DSL - making technical and business people speaking the same languageReal world DSL - making technical and business people speaking the same language
Real world DSL - making technical and business people speaking the same languageMario Fusco
 
Drools 6 deep dive
Drools 6 deep diveDrools 6 deep dive
Drools 6 deep diveMario Fusco
 

Viewers also liked (8)

Seven Deadly Sins
Seven Deadly Sins Seven Deadly Sins
Seven Deadly Sins
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
Why we cannot ignore Functional Programming
Why we cannot ignore Functional ProgrammingWhy we cannot ignore Functional Programming
Why we cannot ignore Functional Programming
 
Hammurabi
HammurabiHammurabi
Hammurabi
 
Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...
 
Comparing different concurrency models on the JVM
Comparing different concurrency models on the JVMComparing different concurrency models on the JVM
Comparing different concurrency models on the JVM
 
Real world DSL - making technical and business people speaking the same language
Real world DSL - making technical and business people speaking the same languageReal world DSL - making technical and business people speaking the same language
Real world DSL - making technical and business people speaking the same language
 
Drools 6 deep dive
Drools 6 deep diveDrools 6 deep dive
Drools 6 deep dive
 

Similar to Swiss army knife Spring

JSF 2.0 (JavaEE Webinar)
JSF 2.0 (JavaEE Webinar)JSF 2.0 (JavaEE Webinar)
JSF 2.0 (JavaEE Webinar)Roger Kitain
 
jBPM5 in action - a quickstart for developers
jBPM5 in action - a quickstart for developersjBPM5 in action - a quickstart for developers
jBPM5 in action - a quickstart for developersKris Verlaenen
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxudithaisur
 
DWR, Hibernate and Dojo.E - A Tutorial
DWR, Hibernate and Dojo.E - A TutorialDWR, Hibernate and Dojo.E - A Tutorial
DWR, Hibernate and Dojo.E - A Tutorialjbarciauskas
 
FIWARE Complex Event Processing
FIWARE Complex Event ProcessingFIWARE Complex Event Processing
FIWARE Complex Event ProcessingMiguel González
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 appletsraksharao
 
Ruslan Platonov - Transactions
Ruslan Platonov - TransactionsRuslan Platonov - Transactions
Ruslan Platonov - TransactionsDmitry Buzdin
 
A Deep Dive into Spring Application Events
A Deep Dive into Spring Application EventsA Deep Dive into Spring Application Events
A Deep Dive into Spring Application EventsVMware Tanzu
 
EventBus for Android
EventBus for AndroidEventBus for Android
EventBus for Androidgreenrobot
 
jBPM5 Community Training Module 4: jBPM5 APIs Overview + Hands On
jBPM5 Community Training Module 4: jBPM5 APIs Overview + Hands OnjBPM5 Community Training Module 4: jBPM5 APIs Overview + Hands On
jBPM5 Community Training Module 4: jBPM5 APIs Overview + Hands OnMauricio (Salaboy) Salatino
 
Events: The Object Oriented Hook System.
Events: The Object Oriented Hook System.Events: The Object Oriented Hook System.
Events: The Object Oriented Hook System.Nida Ismail Shah
 
Event and signal driven programming techniques
Event and signal driven programming techniquesEvent and signal driven programming techniques
Event and signal driven programming techniquesElizabeth Smith
 
How to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdfHow to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdfKaty Slemon
 
Azure Durable Functions (2019-03-30)
Azure Durable Functions (2019-03-30) Azure Durable Functions (2019-03-30)
Azure Durable Functions (2019-03-30) Paco de la Cruz
 
Declarative presentations UIKonf
Declarative presentations UIKonfDeclarative presentations UIKonf
Declarative presentations UIKonfNataliya Patsovska
 

Similar to Swiss army knife Spring (20)

JSF 2.0 (JavaEE Webinar)
JSF 2.0 (JavaEE Webinar)JSF 2.0 (JavaEE Webinar)
JSF 2.0 (JavaEE Webinar)
 
jBPM5 in action - a quickstart for developers
jBPM5 in action - a quickstart for developersjBPM5 in action - a quickstart for developers
jBPM5 in action - a quickstart for developers
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptx
 
Event
EventEvent
Event
 
DWR, Hibernate and Dojo.E - A Tutorial
DWR, Hibernate and Dojo.E - A TutorialDWR, Hibernate and Dojo.E - A Tutorial
DWR, Hibernate and Dojo.E - A Tutorial
 
Os Johnson
Os JohnsonOs Johnson
Os Johnson
 
FIWARE Complex Event Processing
FIWARE Complex Event ProcessingFIWARE Complex Event Processing
FIWARE Complex Event Processing
 
FIWARE Complex Event Processing
FIWARE Complex Event ProcessingFIWARE Complex Event Processing
FIWARE Complex Event Processing
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 applets
 
WPF Fundamentals
WPF FundamentalsWPF Fundamentals
WPF Fundamentals
 
Ruslan Platonov - Transactions
Ruslan Platonov - TransactionsRuslan Platonov - Transactions
Ruslan Platonov - Transactions
 
A Deep Dive into Spring Application Events
A Deep Dive into Spring Application EventsA Deep Dive into Spring Application Events
A Deep Dive into Spring Application Events
 
EventBus for Android
EventBus for AndroidEventBus for Android
EventBus for Android
 
jBPM5 Community Training Module 4: jBPM5 APIs Overview + Hands On
jBPM5 Community Training Module 4: jBPM5 APIs Overview + Hands OnjBPM5 Community Training Module 4: jBPM5 APIs Overview + Hands On
jBPM5 Community Training Module 4: jBPM5 APIs Overview + Hands On
 
Events: The Object Oriented Hook System.
Events: The Object Oriented Hook System.Events: The Object Oriented Hook System.
Events: The Object Oriented Hook System.
 
Event and signal driven programming techniques
Event and signal driven programming techniquesEvent and signal driven programming techniques
Event and signal driven programming techniques
 
How to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdfHow to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdf
 
Azure Durable Functions (2019-03-30)
Azure Durable Functions (2019-03-30) Azure Durable Functions (2019-03-30)
Azure Durable Functions (2019-03-30)
 
Declarative presentations UIKonf
Declarative presentations UIKonfDeclarative presentations UIKonf
Declarative presentations UIKonf
 
SAP workflow events
SAP workflow eventsSAP workflow events
SAP workflow events
 

More from Mario Fusco

Kogito: cloud native business automation
Kogito: cloud native business automationKogito: cloud native business automation
Kogito: cloud native business automationMario Fusco
 
Let's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APILet's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APIMario Fusco
 
How and why I turned my old Java projects into a first-class serverless compo...
How and why I turned my old Java projects into a first-class serverless compo...How and why I turned my old Java projects into a first-class serverless compo...
How and why I turned my old Java projects into a first-class serverless compo...Mario Fusco
 
Introducing Drools
Introducing DroolsIntroducing Drools
Introducing DroolsMario Fusco
 
No more loops with lambdaj
No more loops with lambdajNo more loops with lambdaj
No more loops with lambdajMario Fusco
 

More from Mario Fusco (7)

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

Recently uploaded

Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarPrecisely
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxGDSC PJATK
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UbiTrack UK
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintMahmoud Rabie
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?IES VE
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfJamie (Taka) Wang
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IES VE
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopBachir Benyammi
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostMatt Ray
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxUdaiappa Ramachandran
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDELiveplex
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Will Schroeder
 

Recently uploaded (20)

Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity Webinar
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptx
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership Blueprint
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
20230104 - machine vision
20230104 - machine vision20230104 - machine vision
20230104 - machine vision
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 Workshop
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptx
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
 

Swiss army knife Spring

  • 1. Swiss Army Knife Spring Mario Fusco [email_address] Twitter: @mariofusco
  • 2.
  • 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.
  • 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.
  • 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.
  • 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]