Russell 2012 introduction to spring integration and spring batch

50
Introduction to Spring Integration and Spring Batch © 2012 SpringOne 2GX. All rights reserved. Do not distribute without permission. Gary Russell, Staff Engineer, SpringSource; @gprussell Tuesday, December 18, 12

description

 

Transcript of Russell 2012 introduction to spring integration and spring batch

Page 1: Russell 2012   introduction to spring integration and spring batch

Introduction toSpring Integration and Spring Batch

© 2012 SpringOne 2GX. All rights reserved. Do not distribute without permission.

Gary Russell, Staff Engineer, SpringSource; @gprussell

Tuesday, December 18, 12

Page 2: Russell 2012   introduction to spring integration and spring batch

What we will cover...

• Spring Integration• Spring Batch• Using Spring Batch and Spring Integration together

2Tuesday, December 18, 12

Page 3: Russell 2012   introduction to spring integration and spring batch

Spring Integration

3Tuesday, December 18, 12

Page 4: Russell 2012   introduction to spring integration and spring batch

Integration Styles

• Business to Business Integration (B2B)• Inter Application Integration (EAI)• Intra Application Integration

4

JVM JVM

EAI

External Business Partner

B2B

Core Messaging

Tuesday, December 18, 12

Page 5: Russell 2012   introduction to spring integration and spring batch

Integration Styles

• File Transfer• Shared Database• Remoting• Messaging

5Tuesday, December 18, 12

Page 6: Russell 2012   introduction to spring integration and spring batch

Common Patterns

6

Retrieve Parse Transform Transmit

Tuesday, December 18, 12

Page 7: Russell 2012   introduction to spring integration and spring batch

Enterprise Integration Patterns

• By Gregor Hohpe & Bobby Woolf• Published 2003• Collection of well-known patterns• http://www.eaipatterns.com/eaipatterns.html

7Tuesday, December 18, 12

Page 8: Russell 2012   introduction to spring integration and spring batch

8

“Spring Integration provides an extension of the Spring programming model

to support the well-known enterprise integration patterns.

Tuesday, December 18, 12

Page 9: Russell 2012   introduction to spring integration and spring batch

What is Spring Integration?

• Light-weight messaging framework• Provides an adapter-based platform for communicating with external

systems

• Pipes and Filters at the core of Spring Integration’s architecture–Endpoint (Filter)–Channel (Pipe)–Message

9Tuesday, December 18, 12

Page 10: Russell 2012   introduction to spring integration and spring batch

Advantages

• Provides building blocks to implement systems that are:– are loosely Coupled (Logically or Physically)– are Event Driven (EDA)– have a staged event-driven architecture (SEDA)

• Sophisticated support for synchronous / asynchronous messaging

10Tuesday, December 18, 12

Page 11: Russell 2012   introduction to spring integration and spring batch

Configuration

• XML Namespace Support• Annotation Support (e.g. @Transformer, @Router, @ServiceActivator)

11

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration" xsi:schemaLocation="..">... <int:channel id="orders"/> <int:splitter input-channel="orders" expression="payload.items" output-channel="drinks"/> <int:channel id="drinks"/> <int:router input-channel="drinks" expression="payload.iced ? 'coldDrinks' : 'hotDrinks'"/>...</beans>

Tuesday, December 18, 12

Page 12: Russell 2012   introduction to spring integration and spring batch

Spring Integration DSLs

• Scala

• Groovy

12

val messageFlow = filter {payload: String => payload == "World"} --> transform { payload: String => "Hello " + payload} --> handle { payload: String => println(payload) } messageFlow.send("World")

def builder = new IntegrationBuilder()

def flow = builder.messageFlow { transform {payload->payload.toUpperCase()} filter {payload-> payload =="HELLO"} handle {payload->payload}}

assert flow.sendAndReceive("hello") == "HELLO"assert flow.sendAndReceive("world") == null

Tuesday, December 18, 12

Page 13: Russell 2012   introduction to spring integration and spring batch

What is in a Message?

• Unit of information• Encapsulates data• Passed between endpoints• Consists of headers

– contains data relevant to the messaging system• and a payload

– actual data for the receiver– depending on use-cases: POJO instances or serialized data

13Tuesday, December 18, 12

Page 14: Russell 2012   introduction to spring integration and spring batch

What is in a Message?

14

package org.springframework.integration;

public interface Message<T> {

MessageHeaders getHeaders();

T getPayload();

}

Tuesday, December 18, 12

Page 15: Russell 2012   introduction to spring integration and spring batch

Message Headers

• Message ID (automatically generated UUID)• Timestamp• Correlation Id• Reply Channel• Error Channel• Expiration Date• Priority• ...

• Add your own headers using a Header Enricher

15Tuesday, December 18, 12

Page 16: Russell 2012   introduction to spring integration and spring batch

Function of a Message

• Command Message

• Event Message

• Document Message

16

E

D

C

Tuesday, December 18, 12

Page 17: Russell 2012   introduction to spring integration and spring batch

What is a Channel?

• Channels connect producers and consumers (decoupling)• MessageChannel Interface:

–PollableChannel (Polling Consumer)–SubscribableChannel (Event Driven)

• Implementations:–DirectChannel–PublishSubscribeChannel–QueueChannel–PriorityChannel–RendezvousChannel–ExecutorChannel

17

<int:channel id="input"> <int:queue capacity="10"/></int:channel>

Tuesday, December 18, 12

Page 18: Russell 2012   introduction to spring integration and spring batch

What is an Endpoint?

• Polling or event-driven• Inbound or outbound• Unidirectional (Channel Adapter) or bidirectional (Gateway)• Internal or external (application context)

18

<inbound-channel-adapter/><outbound-channel-adapter/><inbound-gateway/><outbound-gateway/><gateway/><service-activator/>

Tuesday, December 18, 12

Page 19: Russell 2012   introduction to spring integration and spring batch

Router

• Message Router• Content-based router• Recipient list router (with selectors)• Payload type router• Header value router• Exception type router

19Tuesday, December 18, 12

Page 20: Russell 2012   introduction to spring integration and spring batch

Transformer

• Delegating via ref/method• Spring Expression Language• Groovy, JRuby, Jython, JavaScript• Object-to-JSON / JSON-to-Object• Payload serializing/deserializing• File-to-bytes, File-to-String• JAXB, JibX, Castor, XMLBeans, Xstream• XPath, XSLT• Object XML Marshalling/Unmarshalling (Spring OXM)• ...

20Tuesday, December 18, 12

Page 21: Russell 2012   introduction to spring integration and spring batch

Spring Integration Components

21

• Claim Check (In/Out)• Content Enricher

–Header Enricher–Payload Enricher

• Control Bus• Delayer• JMX Support• Message Handler Chain• Messaging Bridge• Resequencer

• Service Activator• Scripting support (JSR 223)

– Ruby/JRuby, Javascript ...• Groovy• Message History• Message Store

– JDBC, Redis, MongoDB, Gemfire

• Wire Tap• ...

Tuesday, December 18, 12

Page 22: Russell 2012   introduction to spring integration and spring batch

Adapters

22

• AMQP/RabbitMQ• AWS*• File/Resource• FTP/FTPS/SFTP• GemFire• HTTP (REST)• JDBC• JMS• JMX• JPA

• MongoDB• POP3/IMAP/SMTP• Print*• Redis• RMI• RSS/Atom• SMB*• Splunk*• Spring Application

Events

• Stored Procedures• TCP/UDP• Twitter• Web Services

(SOAP or POX)• XMPP• XPath• XQuery*

• ...* Spring Integration Extensions Project

Tuesday, December 18, 12

Page 23: Russell 2012   introduction to spring integration and spring batch

Tooling - Spring Tool Suite (STS)

• Namespace Support• Visualization• 4 Spring Integration specific STS Templates

–Simple Template (Core Components only)–File Polling Template (File Adapter)–War Template (Uses Twitter Adapter)–Adapter Template (Create your own components)

23Tuesday, December 18, 12

Page 24: Russell 2012   introduction to spring integration and spring batch

Samples

• https://github.com/SpringSource/spring-integration-samples

• Contains 50 Samples and Applications• Several Categories:

–Basic–Intermediate–Advanced–Applications

24Tuesday, December 18, 12

Page 25: Russell 2012   introduction to spring integration and spring batch

Books

• Just Spring Integration• Pro Spring Integration• Spring Integration in Action

25Tuesday, December 18, 12

Page 26: Russell 2012   introduction to spring integration and spring batch

What’s new in Spring Integration 2.2

• JPA support– http://blog.springsource.org/2012/10/05/whats-new-in-spring-integration-2-2-part-3-jpa-support/

• MongoDB adapters– http://blog.springsource.org/2012/09/24/whats-new-in-spring-integration-2-2-rc1-part-1-mongodb/

• Transaction synchronization– http://blog.springsource.org/2012/09/26/whats-new-in-spring-integration-2-2-part-2-transaction-

synchronization/

• Retry, Circuit Breaker and ExpressionEvaluatingRequestHandlerAdvice– http://blog.springsource.org/2012/10/09/spring-integration-2-2-retry-and-more/

• Redis Store Adapters

26Tuesday, December 18, 12

Page 28: Russell 2012   introduction to spring integration and spring batch

Contribute

• Post Question and Answers the Forums–http://forum.springsource.org/forumdisplay.php?42-Integration

• Create Jiras–https://jira.springsource.org/browse/INT

• Submit Pull Requests - Contributor Guidelines:– github.com/SpringSource/spring-integration/wiki/Contributor-Guidelines

• New Spring Integration Extensions Repository

28Tuesday, December 18, 12

Page 29: Russell 2012   introduction to spring integration and spring batch

Spring Batch

29Tuesday, December 18, 12

Page 30: Russell 2012   introduction to spring integration and spring batch

30

Batch JobsDiffer from online/real-time processing applications:

• Long-running– Often outside office hours

• Non-interactive– Often include logic for handling errors or restarts

• Process large volumes of data– More than fits in memory or a single transaction

Tuesday, December 18, 12

Page 31: Russell 2012   introduction to spring integration and spring batch

Batch and offline processing• Close of business processing

– Order processing– Business reporting– Account reconciliation

• Import/export handling– a.k.a. ETL jobs (Extract-Transform-Load)– Instrument/position import– Data warehouse synchronization

• Large-scale output jobs– Loyalty scheme emails– Bank statements

• 31Tuesday, December 18, 12

Page 32: Russell 2012   introduction to spring integration and spring batch

Job and Step

32Tuesday, December 18, 12

Page 33: Russell 2012   introduction to spring integration and spring batch

Chunk-Oriented Processing• Input-output can be grouped together• Input collects Items before outputting:Chunk-Oriented Processing• Optional ItemProcessor

33Tuesday, December 18, 12

Page 34: Russell 2012   introduction to spring integration and spring batch

JobLauncher

34Tuesday, December 18, 12

Page 35: Russell 2012   introduction to spring integration and spring batch

Simple File Load Job

35Tuesday, December 18, 12

Page 36: Russell 2012   introduction to spring integration and spring batch

More Complex Use Cases• It's very common to use an off-the-shelf reader

and writer• More complex jobs often require custom readers

or writers• ItemProcessor is often used if there's a need to

delegate to existing business logic• Use a writer if it's more efficient to process a

complete chunk

36Tuesday, December 18, 12

Page 37: Russell 2012   introduction to spring integration and spring batch

Job and Step in Context

37Tuesday, December 18, 12

Page 38: Russell 2012   introduction to spring integration and spring batch

JobRepository and Batch Metadata

38Tuesday, December 18, 12

Page 39: Russell 2012   introduction to spring integration and spring batch

ExecutionContext• We need to know where a failure occurred to restart a batch

process• Job Repository metadata is used to determine the step at

which the failure occurred• Application Code (in reader/writer) needs to maintain state

within a step (e.g. current chunk)• Spring Batch can supply that data during restart to facilitate

repositioning

39Tuesday, December 18, 12

Page 40: Russell 2012   introduction to spring integration and spring batch

Common Batch Idioms• Batch jobs typically process large amounts of homogeneous

input• Makes iteration a common concern: Repeat• Transient errors during processing may require a Retry of

an input item• Some input may not be valid, may want to Skip it without

failing• Some errors should fail the job execution, allowing one to fix

the problem and Restart the job instance where it left off

40Tuesday, December 18, 12

Page 41: Russell 2012   introduction to spring integration and spring batch

Spring Batch• Spring Batch supports these common concerns

• Abstracts them in the framework– Job business logic doesn't need to care about details

• Allows for simple configuration with pluggable strategies

41Tuesday, December 18, 12

Page 42: Russell 2012   introduction to spring integration and spring batch

Business Logic Delegation – Spring Application

42Tuesday, December 18, 12

Page 43: Russell 2012   introduction to spring integration and spring batch

Spring Batch Admin• Sub project of Spring Batch• Provides Web UI and ReSTFul interface to manage batch

processes

• Manager, Resources, Sample WAR– Deployed with batch job(s) as single app to be able to control &

monitor jobs– Or monitors external jobs only via shared database

43Tuesday, December 18, 12

Page 44: Russell 2012   introduction to spring integration and spring batch

Scaling and Parallel Processing• First Rule:

– Use the simplest technique to get the job done in the required time

– Do not optimize/parallelize unnecessarily

• Options:– Multi-threaded Step (single process)– Parallel Steps (single process) – Remote Chunking of Step (multi process)– Partitioning a Step (single or multi process)

44Tuesday, December 18, 12

Page 45: Russell 2012   introduction to spring integration and spring batch

Using Spring Batch and

Spring Integration Together

45Tuesday, December 18, 12

Page 46: Russell 2012   introduction to spring integration and spring batch

Launching batch jobs through messages

• Event-Driven execution of the JobLauncher• Spring Integration retrieves the data (e.g. file system, FTP, ...)• Easy to support separate input sources simultaneously

46

D C

FTP

Inbound Channel Adapter

JobLauncher

Transformer

FileJobLaunchRequest

Tuesday, December 18, 12

Page 47: Russell 2012   introduction to spring integration and spring batch

Providing feedback with informational messages

• Spring Batch provides support for listeners:– StepListener– ChunkListener– JobExecutionListener

47

<batch:job  id="importPayments">        ...        <batch:listeners>                <batch:listener  ref="notificationExecutionsListener"/>        </batch:listeners></batch:job>

<int:gateway  id="notificationExecutionsListener"        service-­‐interface="o.s.batch.core.JobExecutionListener"        default-­‐request-­‐channel="jobExecutions"/>

Tuesday, December 18, 12

Page 48: Russell 2012   introduction to spring integration and spring batch

Externalizing batch process execution

• Use Spring Integration inside of Batch jobs, e.g.:– ItemProcessor– ItemWriter

• Offload complex processing• Asynchronous processing support:

– AsyncItemProcessor– AsyncItemWriter

• Externalize chunk processing using ChunkMessageChannelItemWriter

48Tuesday, December 18, 12

Page 49: Russell 2012   introduction to spring integration and spring batch

Business Logic Delegation – Spring Integration

49Tuesday, December 18, 12

Page 50: Russell 2012   introduction to spring integration and spring batch

Questions

50

Questions?

Thank You!!

Tuesday, December 18, 12