Top Banner
NOT CONFIDENTIAL - TELL EVERYONE EAI Patterns with Spring Integration Oliver Gierke, Engineer SpringSource, a division of VMware
52

Spring integration

Dec 01, 2014

Download

Documents

Oliver Gierke

Slides of my talk at JUG Ostfalia, August 18th…
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Spring integration

NOT CONFIDENTIAL - TELL EVERYONE

EAI Patterns with Spring IntegrationOliver Gierke, EngineerSpringSource, a division of VMware

Page 2: Spring integration

About Me

2

Oliver Gierke

SpringSource Spring Data

[email protected]

www.olivergierke.deolivergierke

Page 3: Spring integration

3

Agenda

• What is EAI?

• Messaging

• Pipes and Filters

• EAI Patterns

• Spring Integration 2.0

• Spring Integration 2.1

Page 4: Spring integration

What is EAI?

4

Page 5: Spring integration

(Don’t try this at home)

5

Page 6: Spring integration

A Better Solution: Spring Integration

• Spring Integration Brings EAI To Your Application

• not the other way ‘round

• Provides the best support for “Enterprise Integration Patterns”

• patterns are built right into the API

• Heavy Emphasis on “channels”

• other solutions omit this key concept

6

Page 7: Spring integration

Messaging

• Integrates two different systems

• Different parties need to share the same data contract

• Not the same service contract

• Fault tolerant, since requests are enqueued and delivered as possible

• AMQP, JMS provide powerful, robust options

7

Page 8: Spring integration

Messaging

8

Page 9: Spring integration

Data Processing with Spring: Integration

• Messaging works by decoupling systems

• The Spring messaging machinery lets you react to messages, declaratively

• Spring Integration takes this approach to the next level

9

?

?

Events

Page 10: Spring integration

Data Processing with Spring: Messaging

• JMS

• Standard in the Java space

• Lots of open source options

• Provided with Java EE application servers

• Spring JMS

10

Page 11: Spring integration

Data Processing with Spring: Messaging

11

Java producers

C

C

C

P

P

queue

Java consumers

queue

Message broker

Page 12: Spring integration

• Sending JMS Messages

• Inject an instance of Spring's JmsTemplate.

• Provide the JMS ConnectionFactory in the JmsTemplate bean definition.

12

JMS

Page 13: Spring integration

@Component public class MessageSender {

@Autowired private JmsTemplate jmsTemplate;

public void send(String message) { this.jmsTemplate.convertAndSend("example.queue", message); }}

@Bean public ConnnectionFactory cf (){ return new CachingConnectionFactory(...); }@Bean public JmsTemplate template(){ return new JmsTemplate(this.cf()) ;}

13

JMS

Page 14: Spring integration

Data Processing with Spring: Messaging

• AMQP

• Real standard

• A product of the the companies with the most mission critical requirements

• Spring AMQP

14

Page 15: Spring integration

Data Processing with Spring: Messaging

15

AMQP producers

C

C

C

P queue

AMQP consumers

queue

Message broker

X

P X

exchanges

Page 16: Spring integration

• Sending AMQP Messages

• Use AmqpTemplate instead of JmsTemplate (accepts exchange and routingKey).

• Nothing changes on the listener side (just a POJO).

@Component public class MessageSender {

@Autowired private AmqpTemplate amqpTemplate;

public void send(String message) { this.amqpTemplate.convertAndSend( "myExchange", "some.routing.key", message); }}

16

AMQP

Page 17: Spring integration

• HTTP Messaging (Request/Reply)

• Use RestTemplate, passing URI to methods based on HTTP methods

• Configure HttpMessageConverters if out-of-the-box support is insufficient

17

HTTP

Page 18: Spring integration

public class HttpClient {

private final String uri = “http://localhost/demo/{name}”; private final RestTemplate template = new RestTemplate();

public String getResource(String name) { this.template.getForObject(uri, String.class, name); }

public URI postResource(String name, Object resource) { this.template.postForLocation(uri, resource, name); }}

18

HTTP

Page 19: Spring integration

• Sending Mail Messages

• Create a SimpleMailMessage instance (or JavaMail MimeMessage).

• Use MailSender (or JavaMailSender) configured with host/user/password, etc.

19

Mail

@Component public class MailClient {

@Autowired private MailSender mailSender;

public void send(String subject, String to, String text) { SimpleMailMessage message = new SimpleMailMessage(); message.setSubject(subject); message.setTo(to); message.setText(text); this.mailSender.send(message); }}

Page 20: Spring integration

Pipes and Filters

20

Page 21: Spring integration

Pipes and Filters

• Messaging

• Payload can be any object

• Header values are stored in a Map

21

Page 22: Spring integration

• Message and Headers

Pipes and Filters

22

public interface Message<T> { MessageHeaders getHeaders(); T getPayload();}

Message<String> m1 = MessageBuilder.withPayload(“foo”) .setHeader(“itemId”, 123).build();Message<String> m2 = MessageBuilder.fromMessage(m1) .setHeader(“itemId”, 456).build();

MessageHeaders headers = message.getHeaders();long timestamp = headers.getTimestamp();String value = headers.get(“someKey”, String.class);

Page 23: Spring integration

• Message channel

• Decouples Producers from Consumers

• Provides extension point for interceptors

•May be Point-to-Point

•Or Publish/Subscribe

Pipes and Filters

23

Page 24: Spring integration

Pipes and Filters

24

<channel id="sync-p2p"/><channel id="async-p2p"> <dispatcher task-executor="someThreadPool" /></channel><channel id="async-buffering-p2p"> <queue capacity="50" /></channel>

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

<publish-subscribe-channel id="async-pubsub" task-executor="someThreadPool" />

• Message channel Types

Page 25: Spring integration

Pipes and Filters

25

• Sending Messages

public interface MessageChannel { boolean send(Message<?> message); boolean send(Message<?> message, long timeout);}

MessagingTemplate template = new MessagingTemplate();template.send(someChannel, message);template.send(“fooChannel”, message);template.convertAndSend(someChannel, “hello”);template.convertAndSend(“fooChannel”, someObject);template.setSendTimeout(5000);template.setDefaultChannel(someChannel);template.convertAndSend(someObject);

Page 26: Spring integration

Pipes and Filters

26

• Receiving Messages

• Inversion of Control

• Endpoints delegate to Spring-managed objects

• Framework handles message reception and method invocation (including conversion)

• Similar but more abstract than Spring JMS

• Clean separation of Code and Configuration

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

@Component public class LoanBroaker { public Message<Quote> processRequest( Message<Loan> loan) { }}

Page 27: Spring integration

„Hello World“

27

Page 28: Spring integration

• Message Endpoint

• Producers send Messages to a MessageChannel

• Depending on their type, MessageChannels may have PollingConsumers

• Or Event-Driven Consumers

28

Pipes and Filters

Page 29: Spring integration

Enterprise Integration Patterns

29

Page 30: Spring integration

Message Endpoint Types

30

• Transformer

• Convert payload or modify headers

• Filter

• Discard messages based on boolean evaluation

• Router

• Determine next channel based on content

• Splitter

• Generate multiple messages from one

• Aggregator

• Assemble a single message from multiple

Page 31: Spring integration

Filtering and Routing

• Filter returns a boolean

• Router returns a channel name (or map key)

• Other routers included out of the box:

• recipient-list

• payload-type

• header-value,

• xpath, ...

31

<filter input-channel="customers" ref="customerRegistry" method="isVip" output-channel="vips" discard-channel="nonVips"/>

<router input-channel="customers" ref="customerRegistry" method="getStatus"> <mapping value="1" channel="platinum"/></router>

Page 32: Spring integration

OddEven

32

Page 33: Spring integration

• Splitter returns a Collection or Array

• Aggregator accepts a Collection or Array

• Default Splitter and Aggregator require no ref/method

• Aggregator also has ReleaseStrategy and CorrelationStrategy

Splitting and Aggregating

33

<splitter input-channel="orders" ref="orderRepository" method="getLineItems" output-channel="lineItems"/>

<aggregator input-channel="processedItems" ref="orderRepository" method="generateConfirmation" output-channel="confirmations"/>

Page 34: Spring integration

Annotation Support

• Alternative to XML

34

@ServiceActivator(inputChannel=“accounts”)public void createAccount(Account account) {…}

@Filter(inputChannel=“customers”, outputChannel=“vips”)public boolean isVip(Customer customer) {…}

@Splitter(inputChannel=“orders”, outputChannel=“lineItems”)public List<LineItem> getLineItems(Order order) {…}

Page 35: Spring integration

Expression Language Support

• Alternative option for ref/method in endpoints

• Mapping between Messages and Methods

35

<filter input-channel="customers" expression="payload.vip" output-channel="vips" discard-channel="nonVips"/>

public void createAccount( @Payload("firstName") String firstName, @Payload("lastName") String lastName, @Header("userInfo.account.id") String accountId) { …}

Page 36: Spring integration

Book orders

36

Page 37: Spring integration

Channel Adapters and Messaging Gateways

• Many, many Adapters

37

• HTTP (REST)

• WS (SOAP/POX)

• Mail (POP3/IMAP/SMTP)

• JDBC

• XMPP(S)

• Twitter

• Spring Events

• RSS/ATOM

• JMS

• AMQP

• TCP

• UDP

• File

• FTP(S)

• SFTP

• RMI

• Activiti BPMN 2

• MongoDB

• Redis

• Native File System Adapters

• SMPP(-S) (SMS gateways)

Page 38: Spring integration

Feed

38

Page 39: Spring integration

Channel Adapters (one-way)

39

<file:inbound-channel-adapter channel="fromFile" directory="${java.io.tmpdir}/input" filename-pattern="[a-z]+.txt"> <si:poller fixed-delay="5000" /></file:inbound-channel-adapter>

<jms:outbound-channel-adapter channel="toJms" destination="exampleQueue"/>

Page 40: Spring integration

Gateways (request-reply)

40

<http:outbound-gateway request-channel="httpRequests" url="http://trafficexample.org/{zipCode}"> <http:uri-variable name="zipCode" expression="payload.address.zip" /></http:outbound-gateway>

<ws:outbound-gateway request-channel="weatherRequests" uri="http://weatherexample.org" marshaller="jaxb2Marshaller" />

Page 41: Spring integration

Spring Integration 2.0

41

Page 42: Spring integration

Major Themes

• Spring 3.0 support

• JDK 5 Support

• New Message Stores

• New Adapters

• Misc.

42

Page 43: Spring integration

Spring 3.0

• ConversionService

• Spring Integration takes advantage of a configured conversion service whenever it wants to perform certain conversions, from a certain primitives to complex objects in message parameters, for example.

• default Spring Integration conversion service bean ID: integrationConversionService

• must implement org.springframework.core.convert.converter.Converter

43

<int:converter> <bean class="….MyConverterImplementation" /> </int:converter>

Page 44: Spring integration

Spring 3.0

• Spring Expression Language

• through XML or Java

44

<int:recipient-list-router id="customRouter" input-channel="routingChannel"> <int:recipient channel="channel1" selector-expression="payload.equals('foo')"/> <int:recipient channel="channel2" selector-expression="headers.contains('bar')"/></int:recipient-list-router>

<int:router input-channel="inChannel" expression="payload + 'Channel'"/>

<filter input-channel="input" expression="payload.equals('nonsense')"/>

Page 45: Spring integration

Spring 3.0

• Spring Expression Language

• through XML or Java

45

<int:transformer input-channel="inChannel" output-channel="outChannel" expression="payload.toUpperCase() + '- [' + T(java.lang.System).currentTimeMillis() + ']'" />

<filter input-channel="input" expression="payload.matches( #{filterPatterns.nonsensePattern})" />

Page 46: Spring integration

Spring Integration 2.0

• New Adapters

• Drastically improved file system support

• JDBC inbound and outbound adapters

• RSS / ATOM

• Twitter

• XMPP

• TCP/

• Misc

• new JMS channel

• revised HTTP support

46

Page 47: Spring integration

Spring Integration 2.1

47

Page 48: Spring integration

Spring Integration 2.0

• Spring 3.1 support

• JDK 7 support

• Conversations

• New message stores

• New adapters

• AMQP / Redis / SMPP / Gemfire

• Misc

• Activiti

• Flex 1.5 / Comet

• Feedback

• What‘s valuable to you?

48

Page 49: Spring integration

Productivity boosters: STS Visual Editor

49

Page 50: Spring integration

Productivity Boosters: Forthcoming

• Productivity boosters

• STS template project

• Spring Roo addon

50

Page 51: Spring integration

Links

• Spring Framework Documentation

• http://static.springsource.org/spring/docs/3.0.x

• Spring Integration Sandbox

• git.springsource.org/spring-integration/sandbox

• Spring Integration

• http://www.springsource.org/spring-integration

• Enterprise Integration Patterns

• http://enterpriseintegrationpatterns.com

• Sample Code

• http://git.springsource.org/spring-integration/samples

• Getting Started with Spring Integration

• http://blog.springsource.com/category/green-beans/

51

Page 52: Spring integration

Credits

• Slides

• Mark Fisher, Josh Long, Adam Fitzgerald, Oliver Gierke

• Demos

• Spring Integration team

52