Developing a Message Driven Architecture with Spring – an Overview Silicon India Java Conference, Hyderabad October 15, 2011 • Technology Specialist with Deloitte Consulting India, practicing Software Engineering for more than a decade now • Defined Technical architecture for multiple strategic projects for clients across domains, but predominantly in Financial Services Industry • Deep experience in creating application development frameworks and solution sets using Java • Leads the competency development of Java Community of Practice at Deloitte Consulting India • Trainer and a speaker - conducted multiple tech-talks predominantly around Architecture, Design and Software Engineering best practices. • Member of Scrum Alliance and a certified Scrum Master -2- Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Introduction – Karthik Banda Agenda • Architectural Context for Integration • Architectural Patterns in Discussion • Event Driven Architecture (EDA) • Spring as EDA enabler • Message Driven Architecture Java Message Service (JMS) Message Exchange Patterns Message Driven Beans in EJB 3 and Message Driven POJO’s Realizing Message driven designs with Spring • Spring Task Execution and Scheduling • Spring Integration • Q&A -3- Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx • • • • Common Architectural contexts and respective Choices Integration and communication styles– • Online / Real Time Integration – SOA Enablers – ESB / MOM / JMS / XML/RPC/JCA and Web Services • Offline Integration – Scheduled Batch processes, Polling • Synchronous Communication – RMI / RPC/JCA /Web Services • Asynchronous Communication – Service Activation / JMS / Asynchronous webservice calls • Message Channeling, Routing, Aggregation – ESB or could be Spring Integration -4- Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx • Event Driven Architecture (EDA) – in a Nutshell • One of the key design principle is - Low Coupling and High Cohesion • EDA gives the flexibility to define clearly the boundaries and responsibilities of classes in collaboration but keeping their level of dependency to the minimum • Example : “What Should I do on successful booking of a ticket” • EDA is all about transmitting events between different components in a system and clearly defining a way to handle them • An “Event” generally represents a change in the state of a system • EDA avoids any coupling between subsystems as it takes away the normal command and control scheme i.e. one class calling a method of another “known” class and waiting for it to respond. -5- Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx • Example : “ Booked a Ticket Successfully” • Events are broadcasted whenever they occur and they are fine grained • Events typically are handled Asynchronously and the Caller is not aware of Callee • Fundamental principle of “Services” to be stateless can be easily achieved • No blocking times – Events no longer takes place one at a time, and the order of events is no longer important. -6- Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx EDA – Characteristics EDA – Spring - Sending Application Events 1. Publisher injected by the container – Setter Injection in this case -7- Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx 2. Use the publisher to send events at runtime. EDA – Spring - Handling Application Events 1. Implement the ApplicationListener interface with a parameterized type. -8- Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx 2. Define the bean, and it will be invoked at runtime when that type of Event occurs. • One of the common architectural choice to integrate disparate systems in a distributed environment is Messaging • Uses a Middleware to facilitate messaging i.e. to queue sent messages and handle them – Message Oriented Middleware (MOM) • Message Producers and Consumers are not aware of each other • JMS is a MOM standard and is a vendor neutral specification for messaging • Features of JMS include • Point to Point Messaging (Queues) (1sender-1receiver) • Publish and Subscribe (Topics) (1publisher – N subscribers) • Reliable Message Delivery • Message Persistence • Transactional • Structured Messaging -9- Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Message Driven Architecture (MeDA)- JMS - in a Nutshell • Point to Point Messaging Domain – Messages are sent to queues and are delivered only once and that too to a single consumer. Queues retain messages until consumed or expired • Publish and Subscribe Messaging Domain – Messages are sent to topics and are delivered to all the subscribers. Ideal for publishing business events and it is one flavor of implementing EDA. - 10 - Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx MeDA – Messaging Domains / Models MeDA – Implementation choice – Message Driven Beans Characteristics of Message Driven Beans • Message Driven beans are state less server side Java objects for handling JMS Messages and processing them asynchronously • Developed as part of EJB 2.0 specification • Highly scalable server side components as they are easily managed and/or distributed • Mandates the need for an EJB container / JEE application Server • Heavy life cycle as part of EJB specification (Nothing changed in EJB 3 ) • Each MDB listens to a single destination • Cannot be part of domain model as they are otherwise are not reusable • Normal leverage is only to the extent of “Services Activation” - 11 - Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Disadvantages of Message Driven Beans MeDA – Implementation choice – Message Driven POJO Characteristics of Message Driven POJO • These can be part of domain model as they are normal java classes with no additional baggage • These classes are JMS unaware • Developer need not worry about JMS • Easily testable • Developers think in terms of Command – Control instead of Messaging and Asynchronous communication as these POJO’s are not treated separately as “Special Objects” • Needs plumbing code to leverage JMS resources. Needs more attention to “Separation of Concerns”. • No standard interfaces are implemented by limiting their power of interface injection - 12 - Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Disadvantages of Message Driven POJO’s MeDA – Spring – Sending JMS Messages 1. Abstract JMS Administered objects into a JmsTemplate 2. Inject an instance of Spring's JmsTemplate into the sending class - 13 - Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx 3. Provide the JMS ConnectionFactory in the JmsTemplate bean definition MeDA – Spring – Listening for JMS Messages 1. Define a “listener-container” within the context and specify the destination that needs to be looked at for receiving messages - 14 - Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx 2. Point to any POJO and specify a listening method to implicitly create a MessageListenerAdapter • AMQP is an open standard application layer protocol for message-oriented middleware • One level ahead of JMS – unlike JMS, AMQP is a wired protocol • Dictates the format of messages during data transfer across networks - 15 - Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx MeDA – Spring – Advanced Message Queuing Protocol MeDA – Spring – Sending/Receiving AMQP Messages 1. Use AmqpTemplate which accepts Exchange and Routing Key - 16 - Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx 2. Point to any POJO and specify a listening method to implicitly create a MessageListenerAdapter for AMQP messages. • Better to use events/messaging and only poll when you have to • Lifecycle interface – start(), stop() and isRunning() - This supports any basic back ground task and a common usage is polling • SmartLifecycle does those three plus autostartup, phases and adds a callback on stop – gives a handle on the running thread to the application to end gracefully. - 17 - Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Polling – An architectural pattern (Better avoid) • The Spring Framework provides abstractions for asynchronous execution of tasks with the TaskExecutor interface • The TaskExecutor interface has a single method execute(Runnable task) • The TaskExecutor gives Spring components an abstraction for thread pooling where needed • Multiple pre-built implementations of TaskExecutor are included in Spring 3 which covers most of the patterns of task execution - 18 - Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Task Execution using Executors - 19 - • Important thing to note is that the execute method here adds a Runnable thread to the queue and the TaskExecutor uses its internal rules to decide when the task gets executed, depending on the Implementation class we choose to inject. • We can easily change a particular behavior or pattern of task execution by changing the Implementation strategy. Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Task Execution – by configuration • @Async – adds an implicit TaskExecutor Support to a method • Spring internally uses a Dynamic Proxy for @Async annotated method so that the method is actually executed by the implementation class of TaskExecutor - 20 - Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Task Execution – @Async Annotation • The Spring Framework provides abstractions for asynchronous scheduling of tasks with the TaskScheduler interface supported by Trigger and TriggerContext interfaces • TaskScheduler supports recurring and cancelable tasks and they can be easily controlled - 21 - Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Task Scheduling • The Example below shows a POJO which can be turned into a business service that can be executed as a Cron Job with basic configuration. • This Example uses Cron Expression “3/10 ****?” which is nothing but a 3 seconds offset and runs every 10 secs in a minute. • Other Possible Triggers are “fixed-delay” and “fixed-rate” which are configured in milliseconds. - 22 - Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Task Scheduling – by configuration • The @Scheduled annotation can be added to a method along with trigger metadata • Start at Every Five seconds after completion of each run • Start at Every Five seconds after starting each run • Cron Job that executes on week days - 23 - Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Task Scheduling – @Scheduled Annotation • Enterprise Application Integration (EAI) is an application of technology defined as the integration of data and services between applications. • Spring Integration is an API from the creators of the Spring Framework that's geared towards Enterprise Application Integration (EAI). • Spring Integration offers improvement on every integration solution that exists in Java world. • Can be a powerful alternative to an ESB • Spring Integration addresses this concern – “As the number of integration points increases the applications will have to maintain and manage many point to point channels of communication and the convergence of these channels at various end points create “spaghetti architecture”. Key Parts of Spring Integration : Message–Pay - 24 - Load–Header–Channel–Gateway Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Spring Integration – A bird-eye view Spring Integration – A Simple Example This example uses Spring Integration to process a book order and appropriately route the message depending on if it's a pickup from the store or if it should be delivered by post. - 25 - Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx • - 26 Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Spring Integration – A Simple Example – @Gateway - 27 Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Spring Integration – A Simple Example – @Router - 28 Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Spring Integration – A Simple Example – Bridge - 29 Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Spring Integration – A Simple Example – @Transformer - 30 Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Spring Integration – Outbound Channel Adapter - 31 Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Annotations – Dynamic Language Support – Spring EL Channel Adapters and Message Gateways Supported • JMS • AMQP • TCP • UDP • File/FTP/SFTP • RMI • RSS • HTTP (REST) • WS (SOAP/POX) • JDBC • XMPP • Twitter • Spring Events - 32 - Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx • Mail (POP3/IMAP/SMTP) - 33 Karthik-Banda-Deloitte-Silicon-India-Java-Conference-Hyderabad_Final.pptx Questions and Answers – [email protected] Copyright © 2011 Deloitte Development LLC. All rights reserved.