Mihalache catalin eip with spring integration

Embed Size (px)

DESCRIPTION

CodeCampIasi25Oct2014

Citation preview

  • 1. Enterprise Integration Patternswith Spring Integration[Mihalache Catalin][Pentalog][25th of October 2014]

2. Agenda Spring Integration 10.000 foot view Enterprise Integration Patterns - WGRUS What is integration? How to address integration challenges Messaging / what are EIP Patterns ? EIP with Spring Integration (SI) WGRUS flows Study resources 3. Spring Integration 10.000 foot view Enterprise Application Integration solution integration of systems and applications across an enterprise Spring Integration pillars (axioms): Spring programming model probably the most popular Java framework Enterprise Integration Patterns Gregor Hohpe , Bobby Woolf - 2003 Foreward by Martin Fowler(Patterns of Enterprise Application Architecture) Asynchronous, message-driven behaviorwithin a Spring-based application Intuitive, incremental adoption for existing Spring users Default integration with HTTP, JMS, AMQP, FTP, JDBC, JPAMail, Mongo, RMI, Web Services, Twitter, XMPP, File systems 4. EIP What is integration?Widgets & Gadgets R Us (WGRUS) - an online retailer that buys widgets and gadgets frommanufacturers and resells them to customers on multiple channels (by phone, fax orders, webinterface).WGRUS & external systems WGRUS's internal systems Call center:C++ desktop application / Oracle database server it allows customers to place orders via phone. Appssources cannot / may not be modified. Inbound fax:The inbound fax system requires manual data entry into a small Microsoft Access application. Web Interface:JEE custom built web app allowing customers to browse widgets & gadgets, place orders, monitor ordersstatus. Apps sources may / can be modified. 5. EIP Why integration is necessary ?What is neededTake OrdersCustomers can place orders on multiplechannels - via Web, phone or faxProcessing OrdersProcessing an order involves multiple steps,including verifying inventory, shipping the goods andinvoicing the customer (integrate all backendsystems)New CatalogThe suppliers update their catalogperiodically. WGRUS needs to update its pricing andavailability based in the new catalogs.ChallengesNetworks are unreliableNetworks are slowAny two applications are differentChange is inevitable 6. EIP How to address integration challenges1. File Transfer - the MS Officedatabase from the Inbound Fax System iscopied periodically to a specific network filelocation; the Integration Solution will recoverit from that location, process it by importingany new order and later delete it.2. Shared Database - the Call Centerapplication's sources cannot / may notchanged. However, since it uses an Oracledatabase server, the creation of new ordersmight be monitored with a trigger on insert(on the orders table). That trigger wouldcopy new order details into a new createdtable orders_si. A separate applicationwould connect to these database,monitoring the orders_si table and eachtime a new record is created, it will notify theIntegration Solution with order details; lateron, that new orders_si record would bedeleted.3. Remote Procedure Invocation - WGRUS needs to update its pricingand availability based in the new catalogs by running some HTTP SOAP webservice calls to Widget Co and Gadget Co external systems; those systemsrespond back with their current catalogs & prices. 7. EIP How to address integration challenges4. Messaging - after a new order isplaced, the web app detects that orderand publishes a new message on a specificJMS queue. This message contains neworder's details. And that's it, the web app'forgets' about that message returning toits job.The messaging system is notified by theJMS queue with an event about the neworder. It can start processing that order. 8. EIP Messaging / what are EIP Patterns ? 9. EIP with Spring Integration - Messagepublic interface Message {MessageHeaders getHeaders();T getPayload();}public final class MessageHeadersimplements Map, Serializable{...}Message message1 = MessageBuilder.withPayload("test").setHeader("foo", "bar").build();Spring Integration Messages are immutable!Best practice: the payload / headers would have to be immutable and serializable. 10. EIP with Spring Integration - Channel 11. EIP with Spring Integration - Channel EIP name: Point-to-Point Channel Message consumer: Event-Driven Consumer A channel that invokes a single subscriber foreach sent Message. The invocation will occur inthe sender's thread. It uses anUnicastingDispatcher without a thread executor.Failover support. 12. EIP with Spring Integration - Channel EIP name: Point-to-Point Channel Message consumer: Event-Driven Consumer A channel that invokes a single subscriber foreach sent Message. The invocation will occur onexecutor's threads. It uses anUnicastingDispatcher with a thread executor.Failover support. 13. EIP with Spring Integration - Channel EIP name: Publish-Subscribe Channel Message consumer: Event-Driven Consumer A channel that sends the Message to each one of itssubscribers. The invocation occurs on the sender'sthread (if no executor is specified) or on separatethreads for each receiver (if an executor is specified).It uses an BroadcastingDispatcher with an optionalthread executor. 14. EIP with Spring Integration - Channel EIP name: Point-to-Point Channel Message consumer: Polling Consumer Simple implementation of a message channel. EachMessage is placed in a BlockingQueue whose capacitymay be specified upon construction. By default, it usesa LinkedBlockingQueue but a MessageGroupQueuemight be used to store messages on a persistentcontext (like a relational database) basically, using aMessageStore 15. EIP with Spring Integration - Channel Sending messages on a channel:@Autowired@Qualifier(myChannelID);private DirectChannel channel;public void sendMessage(String msgPayload) {final MessagingTemplate mt = new MessagingTemplate();final Message msg =MessageBuilder.withPayload(msgPayload).build();mt.send(channel, msg);} 16. EIP with SI Polling Consumer Polling consumers: a consumer with a PollableChannel as its input channel Sync / Async polling consumers 17. EIP with SI Event-driven Consumers Event driven consumers: a consumer with a SubscribableChannel as its input channel 18. EIP with SI Channel Adapter A Channel Adapter is a component that connects a single sender or receiver to a Message Channel.(outside of the sender/receiver; one way communication) JMS inbound channel adapter: JMS outbound channel adapter: Predefined channel adapters: JMS, AMQP, JDBC, JPA, Mongo, Redis, FTP, Twitter 19. EIP with SI Gateways A Gateway is a Message Endpoint that connects an application that is both a sender and receiver to aMessage Channel. (inside the application; two-way communication). The 'default' gateway allows Java code to call a Spring Integration flow as a Spring service: Async gateways:public interface MyGateway {public String process(String thing, Predefined gateways: JMS, AMQP, HTTP, Web Services, Redis, JDBC@Header(FileHeaders.FILENAME)String fileName);}public interface MyGateway {public Feature process(String thing,@Header(FileHeaders.FILENAME)String fileName);} 20. EIP with SI Service Activator A service activator in Spring Integration simply connects any existing Spring-managed bean to a channel.These Spring service beans may or may not return a result. @Service("barista")public class Barista {@Transactionalpublic Drink prepareHotDrink(OrderItem orderItem) {...}} 21. EIP with SI Transformerpublic class CallCenterOrderTransformer {public CanonicalOrder transform(CallCenterOrder order) {...;}} 22. EIP with SI Claim Check 23. EIP with SI Filterpublic class PetFilter {public boolean dogsOnly(String input) {return input.toLowerCase().contains("dog");}} 24. EIP with SI Splitterpublic class OrdersSplitter {public List splitOrder(CanonicalOrder order) {return null;}}The Splitter adds following headers in Item messages (for an Order with 2 items):{correlationId=d7c96f28-b9b5-1c8b-1881-8d4b09d83c6b, sequenceSize=2, sequenceNumber=1}{ correlationId=d7c96f28-b9b5-1c8b-1881-8d4b09d83c6b, sequenceSize=2, sequenceNumber=2} 25. EIP with SI Aggregatorpublic class ItemsAggregator {public CanonicalOrder splitOrder(List items) {...}}CorrelationId, SequenceSize, SequenceNumberStatefull component - it might use a message store to keep messages upon completion.Correlation Strategy how to identify thow messages belong to the same groupReleaseStrategy how to detect that a group of messages is completeTimeouts time to wait a group of messages upon completionDiscard channel where expired messages will be publishedPartial Aggregation if not all expected messages are to come 26. EIP with SI Routerpublic class OrderRouter {public String route(CanonicalOrderItem order) {...}} 27. Spring Integration Error HandlingDefault channels:errorChannelused internally for sending error messages and may be overridden with a custom configurationnullChannelacts like /dev/null, simply logging any Message sent to it at DEBUG level and returning immediately 28. WGRUS Taking Orders 29. WGRUS Processing Orders 30. Study resourcesSpring Integration in Action written by SI's creatorsEnterprise Integration Patterns 31. Please fill the online evaluation form after event[Enterprise Integration Patterns with Spring Integration][Mihalache Catalin][Pentalog][25th of October 2014]