Spring integration

Oliver Gierke
Oliver GierkeSenior Member Technical Staff at Spring Source - a division of VMware
EAI Patterns with Spring Integration
Oliver Gierke, Engineer
SpringSource, a division of VMware




                                     NOT CONFIDENTIAL - TELL EVERYONE
About Me



           Oliver Gierke

           SpringSource
           Spring Data

           ogierke@vmware.com
           www.olivergierke.de
           olivergierke




                                 2
Agenda

• What is EAI?
 • Messaging
 • Pipes and Filters
 • EAI Patterns
• Spring Integration 2.0
• Spring Integration 2.1




                           3
What is EAI?




               4
(Don’t try this at home)




                           5
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
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
Messaging




            8
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




                                                   ?

                             Events
                                                    ?




                                                                9
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
Data Processing with Spring: Messaging




        Java producers    Message broker   Java consumers



                                             C
              P          queue

                                             C

              P          queue
                                             C




                                                            11
JMS

• Sending JMS Messages
 • Inject an instance of Spring's JmsTemplate.
 • Provide the JMS ConnectionFactory in the JmsTemplate bean
  definition.




                                                               12
JMS

@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
Data Processing with Spring: Messaging

• AMQP
 • Real standard
 • A product of the the companies with the most mission critical
  requirements
 • Spring AMQP




                                                                   1
Data Processing with Spring: Messaging




AMQP producers   exchanges    Message broker   AMQP consumers



                                                    C
          P          X       queue

                                                    C

          P          X       queue
                                                    C




                                                                1
AMQP

• 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
HTTP

• 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


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
Mail

• Sending Mail Messages
 • Create a SimpleMailMessage instance (or JavaMail MimeMessage).
 • Use MailSender (or JavaMailSender) configured with host/user/
     password, etc.

 @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);
      }
 }

                                                                    19
Pipes and Filters




                    20
Pipes and Filters

• Messaging
 • Payload can be any object
 • Header values are stored in a Map




                                       21
Pipes and Filters

• Message and Headers

 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);


                                                          22
Pipes and Filters

• Message channel
 • Decouples Producers from Consumers
 • Provides extension point for interceptors
 • May be Point-to-Point




 • Or Publish/Subscribe




                                               23
Pipes and Filters

• Message channel Types
 <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" />




                                                      24
Pipes and Filters

• 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);




                                                         25
Pipes and Filters

• 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) {
   }
 }

                                                                            26
„Hello World“




                27
Pipes and Filters

• Message Endpoint
• Producers send Messages to a MessageChannel
• Depending on their type, MessageChannels may have
 PollingConsumers




• Or Event-Driven Consumers




                                                      28
Enterprise
Integration Patterns



                   29
Message Endpoint Types

     • 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




                                                        30
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     <filter input-channel="customers"
                            ref="customerRegistry"
 • header-value,
                            method="isVip"
 • xpath, ...               output-channel="vips"
                            discard-channel="nonVips"/>

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

                                                           31
OddEven




          32
Splitting and Aggregating

• Splitter returns a Collection or Array
• Aggregator accepts a Collection or Array
  <splitter input-channel="orders"
            ref="orderRepository"
            method="getLineItems"
            output-channel="lineItems"/>

• Default Splitter and Aggregator require no ref/method
• Aggregator also has ReleaseStrategy and CorrelationStrategy
 <aggregator input-channel="processedItems"
             ref="orderRepository"
             method="generateConfirmation"
             output-channel="confirmations"/>



                                                            33
Annotation Support

• Alternative to XML
 @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) {…}




                                                           34
Expression Language Support

• Alternative option for ref/method in endpoints
 <filter input-channel="customers"
         expression="payload.vip"
         output-channel="vips"
         discard-channel="nonVips"/>



• Mapping between Messages and Methods
 public void createAccount(
   @Payload("firstName") String firstName,
   @Payload("lastName") String lastName,
   @Header("userInfo.account.id") String accountId) {
    …
 }


                                                        35
Book orders




              36
Channel Adapters and Messaging Gateways

• Many, many Adapters
 • JMS      • HTTP (REST)           • Activiti BPMN 2
 • AMQP     • WS (SOAP/POX)         • MongoDB
 • TCP      • Mail (POP3/IMAP/SMTP) • Redis
 • UDP      • JDBC                  • Native File System
                                      Adapters
 • File     • XMPP(S)
 • FTP(S)   • Twitter               • SMPP(-S) (SMS gateways)
 • SFTP     • Spring Events
 • RMI      • RSS/ATOM




                                                            37
Feed




       38
Channel Adapters (one-way)



<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"/>




                                                             39
Gateways (request-reply)



<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" />




                                                            40
Spring Integration 2.0



                    41
Major Themes

• Spring 3.0 support
 • JDK 5 Support
• New Message Stores
• New Adapters
• Misc.




                       42
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

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




                                                                      43
Spring 3.0

• Spring Expression Language
  • through XML or Java

<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')"/>

                                                           44
Spring 3.0

• Spring Expression Language
 • through XML or Java

 <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}
 )" />




                                                           45
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
Spring Integration 2.1




                    47
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
Productivity boosters: STS Visual Editor




                                           49
Productivity Boosters: Forthcoming

• Productivity boosters
 • STS template project
 • Spring Roo addon




                                     50
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
Credits

• Slides
 • Mark Fisher, Josh Long, Adam Fitzgerald, Oliver Gierke
• Demos
 • Spring Integration team




                                                            52
1 of 52

Recommended

Spring integration by
Spring integrationSpring integration
Spring integrationDominik Strzyżewski
2.8K views52 slides
S2GX 2012 - Introduction to Spring Integration and Spring Batch by
S2GX 2012 - Introduction to Spring Integration and Spring BatchS2GX 2012 - Introduction to Spring Integration and Spring Batch
S2GX 2012 - Introduction to Spring Integration and Spring BatchGunnar Hillert
8.3K views56 slides
Spring integration by
Spring integrationSpring integration
Spring integrationZülfikar Karakaya
1.9K views33 slides
Messaging with Spring Integration by
Messaging with Spring IntegrationMessaging with Spring Integration
Messaging with Spring IntegrationVadim Mikhnevych
5.7K views39 slides
Integration Patterns With Spring integration by
Integration Patterns With Spring integrationIntegration Patterns With Spring integration
Integration Patterns With Spring integrationEldad Dor
2.7K views39 slides
Syer Monitoring Integration And Batch by
Syer Monitoring Integration And BatchSyer Monitoring Integration And Batch
Syer Monitoring Integration And BatchDave Syer
2.3K views44 slides

More Related Content

What's hot

Mule overview by
Mule overviewMule overview
Mule overviewPraneethchampion
499 views40 slides
Spring Integration by
Spring IntegrationSpring Integration
Spring IntegrationSrinivas Kumar R
3.6K views16 slides
Integration and Batch Processing on Cloud Foundry by
Integration and Batch Processing on Cloud FoundryIntegration and Batch Processing on Cloud Foundry
Integration and Batch Processing on Cloud FoundryJoshua Long
4.4K views54 slides
Mule generic connector by
Mule generic connectorMule generic connector
Mule generic connectorAnkush Sharma
544 views10 slides
Mule enterprise service bus by
Mule enterprise service busMule enterprise service bus
Mule enterprise service busThang Loi
139 views40 slides
Mule esb by
Mule esbMule esb
Mule esbSon Nguyen
800 views27 slides

What's hot(18)

Viewers also liked

Atlanta JUG - Integrating Spring Batch and Spring Integration by
Atlanta JUG - Integrating Spring Batch and Spring IntegrationAtlanta JUG - Integrating Spring Batch and Spring Integration
Atlanta JUG - Integrating Spring Batch and Spring IntegrationGunnar Hillert
17.5K views48 slides
Spring Web Service, Spring Integration and Spring Batch by
Spring Web Service, Spring Integration and Spring BatchSpring Web Service, Spring Integration and Spring Batch
Spring Web Service, Spring Integration and Spring BatchEberhard Wolff
11.1K views43 slides
Camel vs Spring EIP JAVA DSL by
Camel vs Spring EIP JAVA DSLCamel vs Spring EIP JAVA DSL
Camel vs Spring EIP JAVA DSLJonathan Livingston
1.5K views7 slides
Sophisticated JPA with Spring & Hades by
Sophisticated JPA with Spring & HadesSophisticated JPA with Spring & Hades
Sophisticated JPA with Spring & HadesOliver Gierke
684 views6 slides
REST based web applications with Spring 3 by
REST based web applications with Spring 3REST based web applications with Spring 3
REST based web applications with Spring 3Oliver Gierke
4.8K views27 slides
Whoops! Where did my architecture go? by
Whoops! Where did my architecture go?Whoops! Where did my architecture go?
Whoops! Where did my architecture go?Oliver Gierke
973 views62 slides

Viewers also liked(20)

Atlanta JUG - Integrating Spring Batch and Spring Integration by Gunnar Hillert
Atlanta JUG - Integrating Spring Batch and Spring IntegrationAtlanta JUG - Integrating Spring Batch and Spring Integration
Atlanta JUG - Integrating Spring Batch and Spring Integration
Gunnar Hillert17.5K views
Spring Web Service, Spring Integration and Spring Batch by Eberhard Wolff
Spring Web Service, Spring Integration and Spring BatchSpring Web Service, Spring Integration and Spring Batch
Spring Web Service, Spring Integration and Spring Batch
Eberhard Wolff11.1K views
Sophisticated JPA with Spring & Hades by Oliver Gierke
Sophisticated JPA with Spring & HadesSophisticated JPA with Spring & Hades
Sophisticated JPA with Spring & Hades
Oliver Gierke684 views
REST based web applications with Spring 3 by Oliver Gierke
REST based web applications with Spring 3REST based web applications with Spring 3
REST based web applications with Spring 3
Oliver Gierke4.8K views
Whoops! Where did my architecture go? by Oliver Gierke
Whoops! Where did my architecture go?Whoops! Where did my architecture go?
Whoops! Where did my architecture go?
Oliver Gierke973 views
Spring Data and MongoDB by Oliver Gierke
Spring Data and MongoDBSpring Data and MongoDB
Spring Data and MongoDB
Oliver Gierke1.5K views
Spring Roo 1.0.0 Technical Deep Dive by Ben Alex
Spring Roo 1.0.0 Technical Deep DiveSpring Roo 1.0.0 Technical Deep Dive
Spring Roo 1.0.0 Technical Deep Dive
Ben Alex5.6K views
Increasing developer procutivity with Mylyn (Devoxx 2010) by Oliver Gierke
Increasing developer procutivity with Mylyn (Devoxx 2010)Increasing developer procutivity with Mylyn (Devoxx 2010)
Increasing developer procutivity with Mylyn (Devoxx 2010)
Oliver Gierke832 views
Generic DAOs With Hades by Oliver Gierke
Generic DAOs With HadesGeneric DAOs With Hades
Generic DAOs With Hades
Oliver Gierke1.5K views
Whoops! where did my architecture go? by Oliver Gierke
Whoops! where did my architecture go?Whoops! where did my architecture go?
Whoops! where did my architecture go?
Oliver Gierke41.2K views
Mylyn - Increasing developer productivity by Oliver Gierke
Mylyn - Increasing developer productivityMylyn - Increasing developer productivity
Mylyn - Increasing developer productivity
Oliver Gierke962 views
Coding & Music Passion And Profession by Oliver Gierke
Coding & Music   Passion And ProfessionCoding & Music   Passion And Profession
Coding & Music Passion And Profession
Oliver Gierke992 views
Spring in action - Hades & Spring Roo by Oliver Gierke
Spring in action - Hades & Spring RooSpring in action - Hades & Spring Roo
Spring in action - Hades & Spring Roo
Oliver Gierke833 views
Data Access 2.0? Please welcome, Spring Data! by Oliver Gierke
Data Access 2.0? Please welcome, Spring Data!Data Access 2.0? Please welcome, Spring Data!
Data Access 2.0? Please welcome, Spring Data!
Oliver Gierke881 views
An introduction into Spring Data by Oliver Gierke
An introduction into Spring DataAn introduction into Spring Data
An introduction into Spring Data
Oliver Gierke4.5K views

Similar to Spring integration

Enterprise Messaging With ActiveMQ and Spring JMS by
Enterprise Messaging With ActiveMQ and Spring JMSEnterprise Messaging With ActiveMQ and Spring JMS
Enterprise Messaging With ActiveMQ and Spring JMSBruce Snyder
12.2K views73 slides
Architecture | The Future of Messaging: RabbitMQ and AMQP | Eberhard Wolff by
Architecture | The Future of Messaging: RabbitMQ and AMQP | Eberhard WolffArchitecture | The Future of Messaging: RabbitMQ and AMQP | Eberhard Wolff
Architecture | The Future of Messaging: RabbitMQ and AMQP | Eberhard WolffJAX London
9.3K views56 slides
Red Hat Open Day JBoss Fuse by
Red Hat Open Day JBoss FuseRed Hat Open Day JBoss Fuse
Red Hat Open Day JBoss FuseAdrian Gigante
3.3K views64 slides
EIP In Practice by
EIP In PracticeEIP In Practice
EIP In PracticeBruce Snyder
3.4K views45 slides
Messaging with RabbitMQ and AMQP by
Messaging with RabbitMQ and AMQPMessaging with RabbitMQ and AMQP
Messaging with RabbitMQ and AMQPEberhard Wolff
13.5K views47 slides
The Future of Messaging: RabbitMQ and AMQP by
The Future of Messaging: RabbitMQ and AMQP The Future of Messaging: RabbitMQ and AMQP
The Future of Messaging: RabbitMQ and AMQP Eberhard Wolff
13.4K views48 slides

Similar to Spring integration(20)

Enterprise Messaging With ActiveMQ and Spring JMS by Bruce Snyder
Enterprise Messaging With ActiveMQ and Spring JMSEnterprise Messaging With ActiveMQ and Spring JMS
Enterprise Messaging With ActiveMQ and Spring JMS
Bruce Snyder12.2K views
Architecture | The Future of Messaging: RabbitMQ and AMQP | Eberhard Wolff by JAX London
Architecture | The Future of Messaging: RabbitMQ and AMQP | Eberhard WolffArchitecture | The Future of Messaging: RabbitMQ and AMQP | Eberhard Wolff
Architecture | The Future of Messaging: RabbitMQ and AMQP | Eberhard Wolff
JAX London9.3K views
Red Hat Open Day JBoss Fuse by Adrian Gigante
Red Hat Open Day JBoss FuseRed Hat Open Day JBoss Fuse
Red Hat Open Day JBoss Fuse
Adrian Gigante3.3K views
Messaging with RabbitMQ and AMQP by Eberhard Wolff
Messaging with RabbitMQ and AMQPMessaging with RabbitMQ and AMQP
Messaging with RabbitMQ and AMQP
Eberhard Wolff13.5K views
The Future of Messaging: RabbitMQ and AMQP by Eberhard Wolff
The Future of Messaging: RabbitMQ and AMQP The Future of Messaging: RabbitMQ and AMQP
The Future of Messaging: RabbitMQ and AMQP
Eberhard Wolff13.4K views
Developing real-time data pipelines with Spring and Kafka by marius_bogoevici
Developing real-time data pipelines with Spring and KafkaDeveloping real-time data pipelines with Spring and Kafka
Developing real-time data pipelines with Spring and Kafka
marius_bogoevici14.1K views
Rabbit MQ introduction by Sitg Yao
Rabbit MQ introductionRabbit MQ introduction
Rabbit MQ introduction
Sitg Yao2.1K views
TS 4839 - Enterprise Integration Patterns in Practice by aegloff
TS 4839 - Enterprise Integration Patterns in PracticeTS 4839 - Enterprise Integration Patterns in Practice
TS 4839 - Enterprise Integration Patterns in Practice
aegloff3.7K views
An introduction to Apache Camel by Kapil Kumar
An introduction to Apache CamelAn introduction to Apache Camel
An introduction to Apache Camel
Kapil Kumar760 views
Life in a Queue - Using Message Queue with django by Tareque Hossain
Life in a Queue - Using Message Queue with djangoLife in a Queue - Using Message Queue with django
Life in a Queue - Using Message Queue with django
Tareque Hossain15K views
Jms deep dive [con4864] by Ryan Cuprak
Jms deep dive [con4864]Jms deep dive [con4864]
Jms deep dive [con4864]
Ryan Cuprak2.2K views
Mihalache catalin eip with spring integration by Codecamp Romania
Mihalache catalin   eip with spring integrationMihalache catalin   eip with spring integration
Mihalache catalin eip with spring integration
Codecamp Romania390 views
Discover the Capabilities of Windows Azure Service Bus to Power Agile Busines... by Sergio Compean
Discover the Capabilities of Windows Azure Service Bus to Power Agile Busines...Discover the Capabilities of Windows Azure Service Bus to Power Agile Busines...
Discover the Capabilities of Windows Azure Service Bus to Power Agile Busines...
Sergio Compean1.5K views
HHM-3540: The IBM MQ Light API: From Developer Laptop to Enterprise Data Cen... by Matt Leming
 HHM-3540: The IBM MQ Light API: From Developer Laptop to Enterprise Data Cen... HHM-3540: The IBM MQ Light API: From Developer Laptop to Enterprise Data Cen...
HHM-3540: The IBM MQ Light API: From Developer Laptop to Enterprise Data Cen...
Matt Leming1.1K views
quickguide-einnovator-11-spring-integration by jorgesimao71
quickguide-einnovator-11-spring-integrationquickguide-einnovator-11-spring-integration
quickguide-einnovator-11-spring-integration
jorgesimao71371 views

Spring integration

  • 1. EAI Patterns with Spring Integration Oliver Gierke, Engineer SpringSource, a division of VMware NOT CONFIDENTIAL - TELL EVERYONE
  • 2. About Me Oliver Gierke SpringSource Spring Data ogierke@vmware.com www.olivergierke.de olivergierke 2
  • 3. Agenda • What is EAI? • Messaging • Pipes and Filters • EAI Patterns • Spring Integration 2.0 • Spring Integration 2.1 3
  • 5. (Don’t try this at home) 5
  • 6. 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
  • 7. 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
  • 9. 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 ? Events ? 9
  • 10. 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
  • 11. Data Processing with Spring: Messaging Java producers Message broker Java consumers C P queue C P queue C 11
  • 12. JMS • Sending JMS Messages • Inject an instance of Spring's JmsTemplate. • Provide the JMS ConnectionFactory in the JmsTemplate bean definition. 12
  • 13. JMS @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
  • 14. Data Processing with Spring: Messaging • AMQP • Real standard • A product of the the companies with the most mission critical requirements • Spring AMQP 1
  • 15. Data Processing with Spring: Messaging AMQP producers exchanges Message broker AMQP consumers C P X queue C P X queue C 1
  • 16. AMQP • 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
  • 17. HTTP • 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
  • 18. HTTP 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
  • 19. Mail • Sending Mail Messages • Create a SimpleMailMessage instance (or JavaMail MimeMessage). • Use MailSender (or JavaMailSender) configured with host/user/ password, etc. @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); } } 19
  • 21. Pipes and Filters • Messaging • Payload can be any object • Header values are stored in a Map 21
  • 22. Pipes and Filters • Message and Headers 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); 22
  • 23. Pipes and Filters • Message channel • Decouples Producers from Consumers • Provides extension point for interceptors • May be Point-to-Point • Or Publish/Subscribe 23
  • 24. Pipes and Filters • Message channel Types <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" /> 24
  • 25. Pipes and Filters • 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); 25
  • 26. Pipes and Filters • 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) { } } 26
  • 28. Pipes and Filters • Message Endpoint • Producers send Messages to a MessageChannel • Depending on their type, MessageChannels may have PollingConsumers • Or Event-Driven Consumers 28
  • 30. Message Endpoint Types • 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 30
  • 31. 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 <filter input-channel="customers" ref="customerRegistry" • header-value, method="isVip" • xpath, ... output-channel="vips" discard-channel="nonVips"/> <router input-channel="customers" ref="customerRegistry" method="getStatus"> <mapping value="1" channel="platinum"/> </router> 31
  • 32. OddEven 32
  • 33. Splitting and Aggregating • Splitter returns a Collection or Array • Aggregator accepts a Collection or Array <splitter input-channel="orders" ref="orderRepository" method="getLineItems" output-channel="lineItems"/> • Default Splitter and Aggregator require no ref/method • Aggregator also has ReleaseStrategy and CorrelationStrategy <aggregator input-channel="processedItems" ref="orderRepository" method="generateConfirmation" output-channel="confirmations"/> 33
  • 34. Annotation Support • Alternative to XML @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) {…} 34
  • 35. Expression Language Support • Alternative option for ref/method in endpoints <filter input-channel="customers" expression="payload.vip" output-channel="vips" discard-channel="nonVips"/> • Mapping between Messages and Methods public void createAccount( @Payload("firstName") String firstName, @Payload("lastName") String lastName, @Header("userInfo.account.id") String accountId) { … } 35
  • 37. Channel Adapters and Messaging Gateways • Many, many Adapters • JMS • HTTP (REST) • Activiti BPMN 2 • AMQP • WS (SOAP/POX) • MongoDB • TCP • Mail (POP3/IMAP/SMTP) • Redis • UDP • JDBC • Native File System Adapters • File • XMPP(S) • FTP(S) • Twitter • SMPP(-S) (SMS gateways) • SFTP • Spring Events • RMI • RSS/ATOM 37
  • 38. Feed 38
  • 39. Channel Adapters (one-way) <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"/> 39
  • 40. Gateways (request-reply) <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" /> 40
  • 42. Major Themes • Spring 3.0 support • JDK 5 Support • New Message Stores • New Adapters • Misc. 42
  • 43. 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 <int:converter> <bean class="….MyConverterImplementation" /> </int:converter> 43
  • 44. Spring 3.0 • Spring Expression Language • through XML or Java <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')"/> 44
  • 45. Spring 3.0 • Spring Expression Language • through XML or Java <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} )" /> 45
  • 46. 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
  • 48. 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
  • 49. Productivity boosters: STS Visual Editor 49
  • 50. Productivity Boosters: Forthcoming • Productivity boosters • STS template project • Spring Roo addon 50
  • 51. 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
  • 52. Credits • Slides • Mark Fisher, Josh Long, Adam Fitzgerald, Oliver Gierke • Demos • Spring Integration team 52