34
Developing a Message Driven Architecture with Spring – an Overview Silicon India Java Conference, Hyderabad October 15, 2011

Developing a Message Driven Architecture with Spring – an Overview Silicon India Java Conference, Hyderabad October 15, 2011

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Developing a Message Driven Architecture with Spring – an Overview

Silicon India Java Conference, HyderabadOctober 15, 2011

- 2 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Introduction – Karthik Banda

• Technology Specialist with Deloitte Consulting India, practicing Software Engineering for more than a decade now

• Defined Technical architecture for multiple strategic projects for clients across domains, but predominantly in Financial Services Industry

• Deep experience in creating application development frameworks and solution sets using Java

• Leads the competency development of Java Community of Practice at Deloitte Consulting India

• Trainer and a speaker - conducted multiple tech-talks predominantly around Architecture, Design and Software Engineering best practices.

• Member of Scrum Alliance and a certified Scrum Master

- 3 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Agenda

• Architectural Context for Integration

• Architectural Patterns in Discussion• Event Driven Architecture (EDA)

• Spring as EDA enabler

• Message Driven Architecture • Java Message Service (JMS)• Message Exchange Patterns• Message Driven Beans in EJB 3 and Message Driven POJO’s • Realizing Message driven designs with Spring

• Spring Task Execution and Scheduling

• Spring Integration

• Q&A

- 4 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Common Architectural contexts and respective Choices

• Integration and communication styles–

• Online / Real Time Integration – SOA Enablers – ESB / MOM / JMS / XML/RPC/JCA and Web Services

• Offline Integration – Scheduled Batch processes, Polling

• Synchronous Communication – RMI / RPC/JCA /Web Services

• Asynchronous Communication – Service Activation / JMS / Asynchronous web-service calls

• Message Channeling, Routing, Aggregation – ESB or could be Spring Integration

- 5 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Event Driven Architecture (EDA) – in a Nutshell

• One of the key design principle is - Low Coupling and High Cohesion

• EDA gives the flexibility to define clearly the boundaries and responsibilities of classes in collaboration but keeping their level of dependency to the minimum

• Example : “What Should I do on successful booking of a ticket”

• EDA is all about transmitting events between different components in a system and clearly defining a way to handle them

• An “Event” generally represents a change in the state of a system

• Example : “ Booked a Ticket Successfully”

• EDA avoids any coupling between subsystems as it takes away the normal command and control scheme i.e. one class calling a method of another “known” class and waiting for it to respond.

- 6 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

EDA – Characteristics

• Events are broadcasted whenever they occur and they are fine grained

• Events typically are handled Asynchronously and the Caller is not aware of Callee

• Fundamental principle of “Services” to be stateless can be easily achieved

• No blocking times – Events no longer takes place one at a time, and the order of events is no longer important.

- 7 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

EDA – Spring - Sending Application Events

1. Publisher injected by the container – Setter Injection in this case

2. Use the publisher to send events at runtime.

- 8 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

EDA – Spring - Handling Application Events

1. Implement the ApplicationListener interface with a parameterized type.

2. Define the bean, and it will be invoked at runtime when that type of Event occurs.

- 9 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Message Driven Architecture (MeDA)- JMS - in a Nutshell

• One of the common architectural choice to integrate disparate systems in a distributed environment is Messaging

• Uses a Middleware to facilitate messaging i.e. to queue sent messages and handle them – Message Oriented Middleware (MOM)

• Message Producers and Consumers are not aware of each other

• JMS is a MOM standard and is a vendor neutral specification for messaging

• Features of JMS include

• Point to Point Messaging (Queues) (1sender-1receiver)

• Publish and Subscribe (Topics) (1publisher – N subscribers)

• Reliable Message Delivery

• Message Persistence

• Transactional

• Structured Messaging

- 10 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

MeDA – Messaging Domains / Models

• Point to Point Messaging Domain – Messages are sent to queues and are delivered only once and that too to a single consumer. Queues retain messages until consumed or expired

• Publish and Subscribe Messaging Domain – Messages are sent to topics and are delivered to all the subscribers. Ideal for publishing business events and it is one flavor of implementing EDA.

- 11 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

MeDA – Implementation choice – Message Driven Beans

Characteristics of Message Driven Beans

• Message Driven beans are state less server side Java objects for handling JMS Messages and processing them asynchronously

• Developed as part of EJB 2.0 specification

• Highly scalable server side components as they are easily managed and/or distributed

Disadvantages of Message Driven Beans

• Mandates the need for an EJB container / JEE application Server

• Heavy life cycle as part of EJB specification (Nothing changed in EJB 3 )

• Each MDB listens to a single destination

• Cannot be part of domain model as they are otherwise are not reusable

• Normal leverage is only to the extent of “Services Activation”

- 12 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

MeDA – Implementation choice – Message Driven POJO

Characteristics of Message Driven POJO

• These can be part of domain model as they are normal java classes with no additional baggage

• These classes are JMS unaware

• Developer need not worry about JMS

• Easily testable

Disadvantages of Message Driven POJO’s

• Developers think in terms of Command – Control instead of Messaging and Asynchronous communication as these POJO’s are not treated separately as “Special Objects”

• Needs plumbing code to leverage JMS resources. Needs more attention to “Separation of Concerns”.

• No standard interfaces are implemented by limiting their power of interface injection

- 13 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

MeDA – Spring – Sending JMS Messages

1. Abstract JMS Administered objects into a JmsTemplate

2. Inject an instance of Spring's JmsTemplate into the sending class

3. Provide the JMS ConnectionFactory in the JmsTemplate bean definition

- 14 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

MeDA – Spring – Listening for JMS Messages

1. Define a “listener-container” within the context and specify the destination that needs to be looked at for receiving messages

2. Point to any POJO and specify a listening method to implicitly create a MessageListenerAdapter

- 15 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

MeDA – Spring – Advanced Message Queuing Protocol

• AMQP is an open standard application layer protocol for message-oriented middleware

• One level ahead of JMS – unlike JMS, AMQP is a wired protocol

• Dictates the format of messages during data transfer across networks

- 16 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

MeDA – Spring – Sending/Receiving AMQP Messages

1. Use AmqpTemplate which accepts Exchange and Routing Key

2. Point to any POJO and specify a listening method to implicitly create a MessageListenerAdapter for AMQP messages.

- 17 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Polling – An architectural pattern (Better avoid)

• Better to use events/messaging and only poll when you have to

• Lifecycle interface – start(), stop() and isRunning() - This supports any basic back ground task and a common usage is polling

• SmartLifecycle does those three plus autostartup, phases and adds a callback on stop – gives a handle on the running thread to the application to end gracefully.

- 18 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Task Execution using Executors

• The Spring Framework provides abstractions for asynchronous execution of tasks with the TaskExecutor interface

• The TaskExecutor interface has a single method execute(Runnable task)

• The TaskExecutor gives Spring components an abstraction for thread pooling where needed

• Multiple pre-built implementations of TaskExecutor are included in Spring 3 which covers most of the patterns of task execution

- 19 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Task Execution – by configuration• Important thing to

note is that the execute method here adds a Runnable thread to the queue and the TaskExecutor uses its internal rules to decide when the task gets executed, depending on the Implementation class we choose to inject.

• We can easily change a particular behavior or pattern of task execution by changing the Implementation strategy.

- 20 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Task Execution – @Async Annotation

• @Async – adds an implicit TaskExecutor Support to a method

• Spring internally uses a Dynamic Proxy for @Async annotated method so that the method is actually executed by the implementation class of TaskExecutor

- 21 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Task Scheduling

• The Spring Framework provides abstractions for asynchronous scheduling of tasks with the TaskScheduler interface supported by Trigger and TriggerContext interfaces

• TaskScheduler supports recurring and cancelable tasks and they can be easily controlled

- 22 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Task Scheduling – by configuration

• The Example below shows a POJO which can be turned into a business service that can be executed as a Cron Job with basic configuration.

• This Example uses Cron Expression “3/10 ****?” which is nothing but a 3 seconds offset and runs every 10 secs in a minute.

• Other Possible Triggers are “fixed-delay” and “fixed-rate” which are configured in milliseconds.

- 23 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Task Scheduling – @Scheduled Annotation

• The @Scheduled annotation can be added to a method along with trigger metadata

• Start at Every Five seconds after completion of each run

• Start at Every Five seconds after starting each run

• Cron Job that executes on week days

- 24 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Spring Integration – A bird-eye view

• Enterprise Application Integration (EAI) is an application of technology defined as the integration of data and services between applications.

• Spring Integration is an API from the creators of the Spring Framework that's geared towards Enterprise Application Integration (EAI).

• Spring Integration offers improvement on every integration solution that exists in Java world.

• Can be a powerful alternative to an ESB

• Spring Integration addresses this concern – “As the number of integration points increases the applications will have to maintain and manage many point to point channels of communication and the convergence of these channels at various end points create “spaghetti architecture”.

Key Parts of Spring Integration : Message–Pay Load–Header–Channel–Gateway

- 25 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Spring Integration – A Simple Example

• This example uses Spring Integration to process a book order and appropriately route the message depending on if it's a pickup from the store or if it should be delivered by post.

- 26 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Spring Integration – A Simple Example – @Gateway

- 27 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Spring Integration – A Simple Example – @Router

- 28 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Spring Integration – A Simple Example – Bridge

- 29 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Spring Integration – A Simple Example – @Transformer

- 30 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Spring Integration – Outbound Channel Adapter

- 31 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Annotations – Dynamic Language Support – Spring EL

- 32 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Channel Adapters and Message Gateways Supported• JMS

• AMQP

• TCP

• UDP

• File/FTP/SFTP

• RMI

• RSS

• HTTP (REST)

• WS (SOAP/POX)

• Mail (POP3/IMAP/SMTP)

• JDBC

• XMPP

• Twitter

• Spring Events

- 33 - Kar

thik

-Ban

da-D

eloi

tte-S

ilico

n-In

dia-

Java

-Con

fere

nce-

Hyd

erab

ad_F

inal

.ppt

x

Questions and Answers – [email protected]

Copyright © 2011 Deloitte Development LLC. All rights reserved.