44
Spring Integration as Integration Patterns provider Blynov Viacheslav Dnepropetrovsk Thursday, June 16, 2022

«Spring Integration as Integration Patterns Provider»

Embed Size (px)

DESCRIPTION

by Vyacheslav Blinnov

Citation preview

Page 1: «Spring Integration as Integration Patterns Provider»

April 12, 2023

Spring Integrationas Integration Patterns provider

Blynov Viacheslav

Dnepropetrovsk

Page 2: «Spring Integration as Integration Patterns Provider»

April 12, 2023 2

Introduction

Page 3: «Spring Integration as Integration Patterns Provider»

3 April 12, 2023

Spring Integration Goals Provide a simple model for implementing complex enterprise

integration solutions.

Facilitate asynchronous, message-driven behavior within a Spring-based application.

Promote intuitive, incremental adoption for existing Spring users.

Spring Integration Introduction

Page 4: «Spring Integration as Integration Patterns Provider»

4 April 12, 2023

Spring Integration Features Implementation of most of the Enterprise Integration Patterns

– Endpoint

– Channel

– Aggregator

– Filter

– …

Integration with External Systems

– REST/HTTP

– FTP

– Twitter

– JMS

– …

The framework has extensive JMX support

– exposing framework components as MBeans

– adapters to obtain attributes from MBeans, invoke operations, send/receive notifications

Spring Integration Introduction

Page 5: «Spring Integration as Integration Patterns Provider»

5 April 12, 2023

Spring Integration Endpoints

AMQP

Spring Application Events

File System

FTP

Gemfire

HTTP/REST

JDBC

JMX

JPA

Mail

MongoDB

Redis

RMI

TCP

Twitter

UDP

XMPP

Spring Integration Introduction

Page 6: «Spring Integration as Integration Patterns Provider»

April 12, 2023 6

Main Components

Page 7: «Spring Integration as Integration Patterns Provider»

7 April 12, 2023

Main Components Message Message Channel Message Endpoint

– Transformer

– Filter

– Router

– Splitter

– Aggregator

– Service Activator

– Channel Adapter

– Gateway

Spring Integration Main Components

Page 8: «Spring Integration as Integration Patterns Provider»

8 April 12, 2023

Spring Integration Main Components

Message Message is a generic

wrapper for any Java object combined with metadata used by the framework while handling that object

Page 9: «Spring Integration as Integration Patterns Provider»

9 April 12, 2023

Spring Integration Main Components

Message Channel Point-To-Point Channel Publish/Subscribe Channel Pollable Channels Message-Driven Channels

Page 10: «Spring Integration as Integration Patterns Provider»

10 April 12, 2023

Spring Integration Main Components

Message Endpoint

Encapsulates communication-specific details:

converts/extracts business data to/from message

sends/receives messages to/from message channel

Page 11: «Spring Integration as Integration Patterns Provider»

11 April 12, 2023

Spring Integration Main Components

Transformer Message Transformer is

responsible for converting a Message's content or structure and returning the modified Message

Page 12: «Spring Integration as Integration Patterns Provider»

12 April 12, 2023

Spring Integration Main Components

Filter Message Filter determines

whether a Message should be passed to an output channel at all

Page 13: «Spring Integration as Integration Patterns Provider»

13 April 12, 2023

Spring Integration Main Components

Splitter Splitter is another type of

Message Endpoint whose responsibility is to accept a Message from its input channel, split that Message into multiple Messages, and then send each of those to its output channel.

Page 14: «Spring Integration as Integration Patterns Provider»

14 April 12, 2023

Spring Integration Main Components

Service Activator

Service Activator is a generic endpoint for connecting a service instance to the messaging system.

The input Message Channel must be configured, and if the service method to be invoked is capable of returning a value, an output Message Channel may also be provided.

Page 15: «Spring Integration as Integration Patterns Provider»

15 April 12, 2023

Spring Integration Main Components

Gateway

The Gateway encapsulates messaging-specific code (e.g., the code required to send or receive a message) and separates it from the rest of the application code. The Messaging Gateway exposes a business function to the rest of the application so that instead of requiring the application to set properties like Message.

Page 16: «Spring Integration as Integration Patterns Provider»

April 12, 2023 16

Example

Page 17: «Spring Integration as Integration Patterns Provider»

17 April 12, 2023

Task We receive JMS messages with some CompoundObject in JSON format

CompoundObject is-a list of some Objects

We need to extract all Objects from CompoundObject and save them to database (possibly performing some other business logic)

Those Objects which are considered “good” (verified against some rule) should be sent via JMS to another queue in JSON format

Spring Integration Example

Page 18: «Spring Integration as Integration Patterns Provider»

18 April 12, 2023

Spring Integration Example

We need to add the following dependencies in pom.xml

Page 19: «Spring Integration as Integration Patterns Provider»

19 April 12, 2023

Spring Integration Example

Specify JMS connection factory

Page 20: «Spring Integration as Integration Patterns Provider»

20 April 12, 2023

Spring Integration Example

Configure Spring Integration context

Page 21: «Spring Integration as Integration Patterns Provider»

21 April 12, 2023

Spring Integration Example

Configure Spring Integration context

CompObjectDTO parseCompoundObject(String payload)

Page 22: «Spring Integration as Integration Patterns Provider»

22 April 12, 2023

Spring Integration Example

Configure Spring Integration context

CompObjectDTO parseCompoundObject(String payload)

List<ObjectDTO> splitCompound(CompObjectDTO dto)

Page 23: «Spring Integration as Integration Patterns Provider»

23 April 12, 2023

Spring Integration Example

Configure Spring Integration context

CompObjectDTO parseCompoundObject(String payload)

List<ObjectDTO> splitCompound(CompObjectDTO dto)

Object convertToEntity(ObjectDTO dto)

Page 24: «Spring Integration as Integration Patterns Provider»

24 April 12, 2023

Spring Integration Example

Configure Spring Integration context

CompObjectDTO parseCompoundObject(String payload)

List<ObjectDTO> splitCompound(CompObjectDTO dto)

Object convertToEntity(ObjectDTO dto)

Object saveEntity(Object obj)

Page 25: «Spring Integration as Integration Patterns Provider»

25 April 12, 2023

Spring Integration Example

Configure Spring Integration context

CompObjectDTO parseCompoundObject(String payload)

List<ObjectDTO> splitCompound(CompObjectDTO dto)

Object convertToEntity(ObjectDTO dto)

Object saveEntity(Object obj)

boolean isGoodObject(Object obj)

Page 26: «Spring Integration as Integration Patterns Provider»

26 April 12, 2023

Spring Integration Example

Configure Spring Integration context

CompObjectDTO parseCompoundObject(String payload)

List<ObjectDTO> splitCompound(CompObjectDTO dto)

Object convertToEntity(ObjectDTO dto)

Object saveEntity(Object obj)

boolean isGoodObject(Object obj)

ObjectDTO convertToDTO(Object obj)

Page 27: «Spring Integration as Integration Patterns Provider»

27 April 12, 2023

Spring Integration Example

Configure Spring Integration context

CompObjectDTO parseCompoundObject(String payload)

List<ObjectDTO> splitCompound(CompObjectDTO dto)

Object convertToEntity(ObjectDTO dto)

Object saveEntity(Object obj)

boolean isGoodObject(Object obj)

ObjectDTO convertToDTO(Object obj)

String serializeObjectDTO(ObjectDTO obj)

Page 28: «Spring Integration as Integration Patterns Provider»

28 April 12, 2023

Spring Integration Example

Receiving messages via HTTP REST

Page 29: «Spring Integration as Integration Patterns Provider»

29 April 12, 2023

Another option: JMS backed message channels

Channel adapter are intended for applications that are integrating with other external systems. There are cases where both the producer and consumer for a given JMS Destination are intended to be part of the same application, running within the same process.

<int-jms:channel id="jmsChannel" queue="exampleQueue"/>

<int-jms:publish-subscribe-channel id="jmsChannel" topic="exampleTopic"/>

Support of transactions (“transaction-manager” atrribute) Support for connection (session, consumer) cache Concurrency (number of concurrent sessions/consumers to start for

each listener)

Spring Integration Example

Page 30: «Spring Integration as Integration Patterns Provider»

30 April 12, 2023

Task 2 An exteranl system feeds us with stream of JSON objects putting them

to JMS queue

We need to make some calculations (business logic) using these objects in the order as they come

Depending on calculationd results we could change the persistent state of our domain objects

Incoming objects should be saved to our DB

Finally, the results of calculation should be sent in JSON format to another JMS queue

Spring Integration Example

Page 31: «Spring Integration as Integration Patterns Provider»

31 April 12, 2023

Basic FlowSpring Integration Example

Receive objects

Validationconvertion to entity

Calculation

Analyze and save results

Send to outbound JMS

Save entities to DB

Page 32: «Spring Integration as Integration Patterns Provider»

32 April 12, 2023

RouterSpring Integration Example

• Payload Type Router• Header Value Router• Recipient List Router• XPath Router (Part of the XML Module)• Error Message Exception Type Router• (Generic) Router

Page 33: «Spring Integration as Integration Patterns Provider»

33 April 12, 2023

ProblemSpring Integration Example

The incoming messages could arrive very frequently

Page 34: «Spring Integration as Integration Patterns Provider»

34 April 12, 2023

AggregatorSpring Integration Example

• Message Store• Correlation Strategy (delaults to grouping be MessageHeader.CORRELATION_ID)• Release Strategy• Expiration policy

Page 35: «Spring Integration as Integration Patterns Provider»

35 April 12, 2023

Back to task 2: receive and aggregateSpring Integration Example

Page 36: «Spring Integration as Integration Patterns Provider»

36 April 12, 2023

Back to task 2: receive and aggregateSpring Integration Example

boolean canReleaseMessages(List<IncObject>)

List<IncObject> aggregate(List<IncObject>)

Object getCorrelationKey(IncObject)

Page 37: «Spring Integration as Integration Patterns Provider»

37 April 12, 2023

Back to task 2: routingSpring Integration Example

Page 38: «Spring Integration as Integration Patterns Provider»

38 April 12, 2023

Back to task 2: via publish-subscribe channelSpring Integration Example

Page 39: «Spring Integration as Integration Patterns Provider»

39 April 12, 2023

Task summary Built on the top of other Spring modules (e. g. Spring JMS)

Every element of the chain – just a simple POJO

High cohesion

Easy to cover with unit-tests

“Convention over configuration” – bean with name “connectionFactory” will be found automatically

Change the whole business flow only by configuration

Spring Integration Example

Page 40: «Spring Integration as Integration Patterns Provider»

April 12, 2023 40

Spring Integration VS Apache Camel

Page 41: «Spring Integration as Integration Patterns Provider»

41 April 12, 2023

Both projects aim to fill similar need: light-weight integration library with full implementations of EIP

Apache Camel introduces DSL (Java, Scala, Groovy). Spring Integration - only XML

Apache Camel supports longer list of technologies

Apache Camel offers rich support for both integration and unit-testing. Spring Integration supports just generic Spring Test

Apache Camel Community is larger, documentation is better

Spring Integration Spring Integration VS Apache Camel

Page 42: «Spring Integration as Integration Patterns Provider»

42 April 12, 2023

Apache Camel Java DSL Example

final JaxbDataFormat jaxbDataFormat = new JaxbDataFormat();

jaxbDataFormat.setContextPath(“test.ns.oxm");

jaxbDataFormat.setPartClass(“test.ns.oxm.ABSPartnerChangesEventType");

from("activemq:incoming.partner")

.log("log:test.log")

.choice() .when(header("EVENTTYPE").isEqualTo(“TESTVALUE")).to("direct:createPartner“) .when(header("EVENTTYPE").isEqualTo(“TESTVALUE2")).to("direct:updatePartner");

from("direct:createPartner")

.errorHandler(noErrorHandler())

.unmarshal(jaxbDataFormat)

.beanRef(“partnerChangeHandler", "createPartner");

from("direct:updatePartner")

.errorHandler(noErrorHandler())

.unmarshal(jaxbDataFormat)

.beanRef(“partnerChangeHandler", "updatePartner");

Spring Integration Spring Integration VS Apache Camel

Page 43: «Spring Integration as Integration Patterns Provider»

43 April 12, 2023

Recent changes Spring EL support (3.0.2)

HTTP request mapping (based on Spring MVC infrastructure) (3.0.2)

Enhanced support for MongoDB, Redis and JPA (3.0.2)

@EnableIntegration, @IntegrationComponentScan (4.0.2)

Requires Spring 4.0 (4.0.2)

Spring Integration Example

Page 44: «Spring Integration as Integration Patterns Provider»

April 12, 2023 44

Questions?