You are on page 1of 87

Enterprise Integration

with Spring Integration

Agim Emruli
SpringSource
8100
2

Presentation Goal

Learn how Spring Integration helps


to solve common Enterprise
Integration challenges
3

Integration
Challenges
System Failures 4
5

Intolerant
Systems
6

Enterprise
Integration
Patterns
Pattern Catalog Overview 7

Message Construction Message Transformation Message Routing

Message Envelope Message


Request Content Based
Message Translator Wrapper
Reply Router
Router

Command
Message Content FIlter Normalizer
Return Composed Splitter
Address Message

D
A B
Document
Message Content Process
Correlation ID Claim Check Aggregator
Enricher Manager

Event
Message Message Message Recipient List
Sequence FIlter
8

How does Spring help ?


9

Spring Big Picture

Aspect Enterprise
Dependency
oriented Service
Injection
Programming Abstraction

Inversion of Control
10

Focus on
Business
domain
11

Spring Powered Applications

Testable Robust

Application

Maintain-
Flexible
able
Layered Architecture 12
Layered Architecture 12

Domain
Objects
Layered Architecture 12

Domain
Objects

Database Email Messaging


Layered Architecture 12

Domain
Objects

Infrastructure

Database Email Messaging


Layered Architecture 12

Domain
Data Access Objects

Infrastructure

Database Email Messaging


Layered Architecture 12

Services
Domain
Data Access Objects

Infrastructure

Database Email Messaging


Layered Architecture 12

AOP Services
Domain
Data Access Objects

Infrastructure

Database Email Messaging


13

Layered Architecture

Spring Application
13

Layered Architecture

MVC

Spring Application
13

Layered Architecture

MVC RMI

Spring Application
13

Layered Architecture

Webservice MVC RMI

Spring Application
13

Layered Architecture

Batch

Webservice MVC RMI

Spring Application
13

Layered Architecture

Batch JMS

Webservice MVC RMI

Spring Application
14

Event Driven Architecture

Framework

Event Application
15

Spring JMS Support

<jms:listener-container transaction-manager="txManager">
<jms:listener ref="orderService"
method="order"
destination="queue.orders"
response-destination="queue.confirmation"/>
</jms:listener-container>
15

Spring JMS Support


<jms:listener-container transaction-manager="txManager">
<jms:listener ref="orderService"
method="order"
destination="queue.orders"
response-destination="queue.confirmation"/>
</jms:listener-container>

public class OrderService {



public OrderConfirmation order(Order o) {..}

}
16

Introducing
Spring
Integration
17

Goals
18

Reuse
Service
Layer
19

Incremental
Extension
20

Spring Integration Architecture


20

Spring Integration Architecture

Services
20

Spring Integration Architecture


Webservice

Services
20

Spring Integration Architecture


Webservice File

Services
20

Spring Integration Architecture


Webservice File

Services
20

Spring Integration Architecture


Webservice File

Transformer

Services
20

Spring Integration Architecture


Webservice File

Transformer

Router

Services
21

Message
Construction
22

Message Structure

Message Header
Sequence Number
Sequence Size
Expiration Date
Correlation Identifier

Message Body

C D E

Command Document Event


Message Message Message
23

Message Interface

public interface Message<T> {

MessageHeaders getHeaders();

T getPayload();
}
Message Headers 24
Message Headers 24

MessageHeaders headers = message.getHeaders();

String value =
headers.get("key", String.class);

Object id = headers.getId();

Long timestamp = headers.getTimestamp();

MessagePriority priority =
headers.getPriority();
Message Builder 25
Message Builder 25

Message<String> message =
MessageBuilder.withPayload("test")
.setHeader("foo", 123)
.setPriority(MessagePriority.HIGHEST)
.build();

Message<String> copy =
MessageBuilder.fromMessage(message)
.setHeader("foo", 456)
.setHeaderIfAbsent("bar", 789)
.build();
Channels
26

And
Endpoints
27

Message Channel

Message Message

Message Endpoint Channel Message Endpoint


28

Direct Channels

Event-Driven
Message Endpoint Channel
Consumer
28

Direct Channels

Event-Driven
Message Endpoint Channel
Consumer

<channel id="sync-p2p" />


29

Queue Channel

Polling
Message Endpoint Channel
Consumer
29

Queue Channel

Polling
Message Endpoint Channel
Consumer

<channel id="async-p2p">
<queue capacity="50" />
</channel>
30

Publish-Subscribe Channel

Event-Driven
Consumer

Publish Subscribe
Message Endpoint
Channel

Event-Driven
Consumer
30

Publish-Subscribe Channel

Event-Driven
Consumer

Publish Subscribe
Message Endpoint
Channel

Event-Driven
Consumer

<publish-subscribe-channel
id="pubsub" />
31

Priority Channel

Polling
Message Endpoint Channel
Consumer

Resequencer
31

Priority Channel

Polling
Message Endpoint Channel
Consumer

Resequencer

<channel id="priorityChannel">
<priority-queue comparator="someComp" />
</channel>
Message 32

Transformation
33

Message Translator

Channel Message Translator Channel


33

Message Translator

Channel Message Translator Channel

<transformer input-channel="input"
output-channel="output" ref="transformer"
method="transform"/>
34

Annotation Based Message Translator

@MessageEndpoint
public class MessageTransformer{

@Transformer(inputChannel="in",
outputChannel="out")
public LoanRequest transform(Loan loan){
return ...;
}
}
Service Activator 35
36

Service Activator

Message Endpoint Channel

Service Activator

Message Endpoint Channel


37

Service Activator

<channel id="requests"/>
<channel id="quotes"/>

<service-activator input-channel="requests"
ref="loanBroker"
method="processRequest"
output-channel="quotes"/>

<beans:bean id="loanBroker"
class="example.LoanBroker"/>
38

Annotation Based Service Activator


@MessageEndpoint
public class LoanBroker {

@ServiceActivator(inputChannel="x",
outputChannel="y")
public LoanQuote processRequest(
LoanRequest request) {
LoanQuote quote ...;
return quote;
}
}
39

Polling and Transactions


<service-activator ref="loanBroker"
method="processRequest"
input-channel="requests"
output-channel="quotes">
<poller task-executor="pool1">
<interval-trigger interval="5000"/>
<transactional propagation="REQUIRES_NEW"/>
</poller>
</service-activator>
<pool-executor id="pool1" max-size="25"/>
<beans:bean id="transactionManager" ... />
Message Routing 40
41

Content Based Router

Standard Service
Channel

Channel Content Based Router

VIP Service Channel


42

MethodInvokingRouter
<channel id="even"/>

<channel id="odd"/>

<router ref="parityResolver" input-


channel="numbers"/>
42

MethodInvokingRouter
<channel id="even"/>

<channel id="odd"/>

<router ref="parityResolver" input-


channel="numbers"/>

@Router
public String getParity(int i) {
return (i % 2 == 0) ? "even" : "odd";
}
43

PayloadtypeRouter

String Channel

Channel Content Based Router

Integer Channel
43

PayloadtypeRouter

String Channel

Channel Content Based Router

Integer Channel

typeMap .put(String.class, stringChannel);


typeMap.put(Integer.class, integerChannel);

PayloadTypeRouter router = new PayloadTypeRouter();


router.setPayloadTypeChannelMap(typeMap);

router.handleMessage(new StringMessage("test"));
router.handleMessage(new GenericMessage(123));
44

ReceipientListRouter

Channel

Channel Recipient List

Channel
44

ReceipientListRouter
Channel

Channel Recipient List

Channel

channels.add(channel1);
channels.add(channel2);

RecipientListRouter router = new RecipientListRouter();


router.setChannels(channels);
Message<String> message = new StringMessage("test");

router.handleMessage(message);
45

Splitter And Aggregator

Channel Splitter

Channel
Aggregator
46

Splitter And Aggregator

@Splitter
public List<OrderItem> splitOrder(PurchaseOrder order,
@Header("customerId") String customerId) {
// split the purchase order into order items…
}
46

Splitter And Aggregator

@Splitter
public List<OrderItem> splitOrder(PurchaseOrder order,
@Header("customerId") String customerId) {
// split the purchase order into order items…
}

@Aggregator
public PurchaseOrder aggregateOrder(List<OrderItem> items) {
// aggregate the items into a single order object...
}
47

Channel
Adapter
48

Channel Adapters
Inbound

Message

External System Channel Adapter Channel


48

Channel Adapters
Inbound

Message

External System Channel Adapter Channel

Outbound

Message

Channel Channel Adapter External System


49

File Adapter

<file:inbound-channel-adapter channel="filesIn"
directory="${java.io.tmpdir}/test-input">
<poller max-messages-per-poll="5">
<cron-trigger expression="*/10 * * * * MON-FRI"/>
</poller>
</file:inbound-channel-adapter>

<file:outbound-channel-adapter channel="filesOut"
directory="${java.io.tmpdir}/test-output"/>
50

JMS Adapter

<jms:inbound-channel-adapter channel="input"
connection-factory="connectionFactory"
destination-name="sourceQueueName"/>

<jms:outbound-channel-adapter channel="output"
destination="targetQueue"/>

<jms:inbound-gateway request-channel="inRequests"
destination="inboundRequestQueue"/>

<jms:outbound-gateway request-channel="outRequests"
reply-channel="replies" jms-queue="outQueue"/>
51

Method Invoking Adapter

<channel id="channel"/>

<inbound-channel-adapter channel="channel"
ref="reader" method="read">
<poller max-messages-per-poll="1">
<interval-trigger interval="1000"/>
</poller>
</inbound-channel-adapter>

<outbound-channel-adapter channel="channel"
ref="writer" method="write"/>
52

Webservice Adapter
<ws:outbound-gateway uri="http://..."
marshaller="someMarshaller"
unmarshaller="someMarshaller"
request-channel="req" reply-channel="rep"/>

<ws:inbound-gateway request-channel="req"
reply-channel="rep"
marshaller="someMarshaller"
unmarshaller="someMarshaller" />
53

Other Adapters

> HTTP
> Mail
> RMI
> springsource.org/extensions
54

Q&A
Agim Emruli
agim.emruli@springsource.com
55

THANK
YOU!
56

Picture Credits

http://www.flickr.com/photos/scraplab/2612334635/

http://www.flickr.com/photos/mylifestory/527847004/

http://www.flickr.com/photos/alex-s/116511016/

http://www.flickr.com/photos/jdan/2324469981/
57

Picture Credits
http://www.flickr.com/photos/global-jet/2125557004/

You might also like