Using a Declarative Chain of Responsibility Pattern to Write Robust, Self- Correcting Distributed Applications

Similar documents
Object-Oriented Design

CHAPTER 6: CREATIONAL DESIGN PATTERNS

DESIGN PATTERN - INTERVIEW QUESTIONS

Model-View-Controller

As a programmer, you know how easy it can be to get lost in the details

Using Design Patterns in Java Application Development

Advanced Object Oriented PHP

C++ INTERFACE CLASSES STRENGTHENING ENCAPSULATION

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Wrapping a complex C++ library for Eiffel. FINAL REPORT July 1 st, 2005

INCORPORATING ADVANCED PROGRAMMING TECHNIQUES IN THE COMPUTER INFORMATION SYSTEMS CURRICULUM

An Introduction to Patterns

Topics in Object-Oriented Design Patterns

The Object Recursion Pattern

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

Exception Handling Alternatives (Part 2)

Bugsquashing: Command - Pattern. Andreas Fetzer

developer.* The Independent Magazine for Software Professionals Factory Chain: A Design Pattern for Factories with Generics by Hugo Troche

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Design Patterns. CSC207 Fall 2017

Second Midterm Review

Applying the Observer Design Pattern

APPLYING DESIGN PATTERNS TO SCA IMPLEMENTATIONS

X-S Framework Leveraging XML on Servlet Technology

Software Engineering I (02161)

James Newkirk

Learning patterns of application architecture by looking at code

Crash course on design patterns

Cocoa Design Patterns. Erik M. Buck October 17, 2009

Object-Oriented Design

Programação de Sistemas Distribuidos

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Design Patterns. Gunnar Gotshalks A4-1

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

SSJ User s Guide. Package stat Tools for Collecting Statistics. Version: December 21, 2006

Evictor. Prashant Jain Siemens AG, Corporate Technology Munich, Germany

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Patterns. Erich Gamma Richard Helm Ralph Johnson John Vlissides

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

Control Message. Abstract. Microthread pattern?, Protocol pattern?, Rendezvous pattern? [maybe not applicable yet?]

Chapter 12 (revised by JAS)

Automating Regression Testing of Java Programs the JSnoopy Way

JDBC Today C HAPTER 1 INTRODUCTION

25.1 Introduction Façade Design Pattern Identification Problem Structure Participants...

Design Patterns. CSC207 Winter 2017

A Novel Data Mining Platform Design with Dynamic Algorithm Base

Idioms for Building Software Frameworks in AspectJ

4.1 Introduction Programming preliminaries Constructors Destructors An example... 3

CPSC 310 Software Engineering. Lecture 11. Design Patterns

Software Reengineering Refactoring To Patterns. Martin Pinzger Delft University of Technology

Configuration Provider: A Pattern for Configuring Threaded Applications

Design Patterns. Observations. Electrical Engineering Patterns. Mechanical Engineering Patterns

What is Design Patterns?

Microthread. An Object Behavioral Pattern for Managing Object Execution. 1.0 Intent. 2.0 Also Known As. 3.0 Classification. 4.0 Motivation/Example

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

Design Patterns: Part 2

SDC Design patterns GoF

Software Engineering - I An Introduction to Software Construction Techniques for Industrial Strength Software

Design Patterns. CSC207 Fall 2017

Integrated Simulation of Communication Networks and Logistical Networks

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Review Software Engineering October, 7, Adrian Iftene

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

Description of CORE Implementation in Java

Introduction to Programming Using Java (98-388)

CS 349 / SE 382 Design Patterns. Professor Michael Terry January 21, 2009

Command. Comp-303 : Programming Techniques Lecture 22. Alexandre Denault Computer Science McGill University Winter 2004

Applying the Decorator Design Pattern

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Lecture 13: Design Patterns

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Outline. Design Patterns. Observer Pattern. Definitions & Classifications

security model. The framework allowed for quickly creating applications that examine nancial data stored in a database. The applications that are gene

Towards a Java Framework for Knowledge Representation and Inference

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

Patterns for polymorphic operations

Design Patterns. An introduction

CSC7203 : Advanced Object Oriented Development. J Paul Gibson, D311. Design Patterns

26.1 Introduction Programming Preliminaries... 2

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

Applying the Factory Method Design Pattern

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Using the Bridge Design Pattern for OSGi Service Update

AN OPC DATA ACCESS SERVER DESIGNED FOR LARGE NUMBER OF ITEMS

6.170 Lecture 15 Design Patterns

Outline. Logistics. Logistics. Principles of Software (CSCI 2600) Spring Logistics csci2600/

Deriving Generic Functions by Example

Painless Persistence. Some guidelines for creating persistent Java applications that work

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

The GoF Design Patterns Reference

An Introduction to Patterns

GOF Patterns Applied

Pattern Examples Behavioural

L23.1 Introduction... 2

Tackling Design Patterns Chapter 3: Template Method design pattern and Public Inheritance. 3.1 Introduction... 2

The WebShop E-Commerce Framework

Transcription:

Using a Declarative Chain of Responsibility Pattern to Write Robust, Self- Correcting Distributed Applications Dan Stieglitz Principal Consultant Stieglitech, LLC dan@stieglitech.com Abstract Businesses are in a constant struggle to improve application reliability, a fight that is made more difficult by the increasing complexity of the business application environment. New distributed components, databases and other moving parts are added to enterprise systems in a never-ending stream. Many application architects embed application-level fail over into applications in an ad hoc manner, which leads to compatibility and consistency issues for current and future developers. This paper presents a strategy for building a more easily maintainable, componentized exception handing subsystem using a declarative chain of responsibility pattern. Introduction Subsystems specifically designed for exception handling (an aspect found in every project) can be strangely rare in the business world. There are few if any vendor products available that are generic, rich-featured exception handling suites. Furthermore, when exception handling frameworks are home-built, they are construed to contain much difficult logic and to be fraught with peril. There seems to be a wide variety of monitoring software that act as excellent exception detection and alerting tools, but in practice, few frameworks that provide application developers with structured, robust error correction tools for use within their software packages. Native Java 1 Exception Handling The lowest-level exception scenario that developers encounter in Java is the plain try/catch/finally block in Java, as in: try { dosomething(); catch (Exception anexception) { 1 Examples and code fragments are given in the Java programming language but the concepts presented can be applied to any object-oriented language. 1

handleexception(); finally { finishup(); For simple error handling, such as checking some variables or attempting to access an I/O device, this rudimentary system works fine. When an error occurs, there is usually no alternative but to handle the low level exception inline and return some status to the calling stack frame. However, in modern distributed systems, there are more advanced requirements for error handling. Robust applications are required to retry failed steps, notify other components of failures and produce logging statements, among others. It can be argued that a more advanced, generic, pluggable exception handling framework would be a valuable tool for building distributed applications. To this end, a declarative chain of responsibility pattern can act as the foundation for building such a system. Chain of Responsibility (a.k.a. Chain of Command) The GoF chain of responsibility pattern s intent is to avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request [Gamma]. Essentially, a message object is passed through a chain of objects implementing a common interface. The interface includes methods to chain the handler together; i.e., get- and setsuccessor methods: public interface Handler { public void handle(object message); public void setsuccessor(handler successor); public Handler getsuccessor(); Implementations of this interface use the successor property to pass the message through the chain, as in: public class HandlerImplementation { public void handle(object message) { // local handling code here if (getsuccessor()!=null) { successor.handle(message); 2

In the case of an exception handling framework, we wish to construct an object that encapsulates all of the information about the state of our application when an error occurred, as well as information about what to do for specific errors. For example, if we have implemented a Handler to retry database calls it s necessary to provide information as to where the secondary database instances are (see the The Problem Interface, below). ChainFactory The chain of responsibility pattern does not really specify where the construction of the chain should occur; this, presumably, is left up to the application developer. Often it occurs in the head of the chain, but if the head is hard-coded into the program it immediately limits the flexibility of the software. To circumvent this problem, a ChainFactory can be built. A ChainFactory produces the head of a particular pre-built chain that can be mapped to an identifier. For example, if a String catalogues ChainFactories, they could be requested from the factory, as in ChainLink specificfactory = ChainFactory.create( chainforsqlexceptions ); Depending on the type of error, a specific set of logic can be applied. This creates the ability to write context-sensitive error handling code, and in so doing, create an opportunity to write intelligent, self-correcting code. Declarative Programming Declarative programming refers to the practice of refactoring application logic out of the code and into a reference file. The Jakarta Struts framework is an excellent example of a declarative system: application execution path is determined by a configuration file that describes which pages to load upon specific user actions [Jakarta]. A declarative approach facilitates quick system maintenance and the ability to hot swap features of a deployed application. Putting a declarative slant on chain of responsibility, it s possible to define the specific chains, catalogue them by key and have the system load these serialized values into a table upon command. Any number of formats could be used to store this information, an example in XML could be: 3

<?xml version= 1.0, encoding= UTF-8?> <error-chains> <error-chain name= chainforsqlexceptions > <chain-link class= com.myapp.loggingchainlink /> <chain-link class= com.myapp.pagemechainlink /> <chain-link class= com.myapp.retrydatabasechainlink /> </error-chain> <error-chain name= defaultchain > <chain-link class= com.myapp.reporttocommandcenterchainlink /> </error-chain> </error-chains> A load() method could be provided to the ChainFactory which reads and parses such a file, building a Map of information. Additionally, keys need not be strings, they can be any object. The Problem Interface An important part of using chain of responsibility is standardizing the interface of the message passed down the links. If a wrapper around the underlying exception, error or application-generated warning is applied; information can be shared among nodes. An example interface could be: public interface Problem { public void setattribute(string name, Object obj); public Object getattribute[(string name); public boolean hasattribute(string name); Implementations of Problem could be created for specific recurring situations, such as: public class DatabaseAccessProblem implements Problem { public class ProblemInRequestProcessor implements Problem { public class SecurityProblem implements Problem { Each specific implementation can store information that allows for some logic to occur. Coupled with a declarative handler chain, a framework evolves that allows for robust, 4

extensible generic exception processing subsystems to be designed and quickly adapted to any project. Self-Correcting Code Consider a set of implementations of Problem that contain context-specific callback methods on their source, as defined in an interface such as: public interface Retryable { public boolean retry(); // returns true for success public int getmaxretrycount(); public void fail(); public List getproblems(); public Problem getlastproblem(); // etc. An Object that implements Retryable has methods that allow external objects to trigger specific events and query as to the results of those events. For example, 1 public class RetryableDAO 2 implements Retryable { 2 3 public boolean makecall() { 4 try { 5 // JDBC code 6 catch (java.sql.sqlexception sqle) { 7 Problem newproblem = new 8 DatabaseAccessProblem(this); 9 addproblem(newproblem); 10 return false; 11 12 return true; 13 14 15 public boolean retry() { 16 retrycount++; 17 return makecall(); 18 19 // remaining methods skipped for brevity 20 2 DAO denotes a Data Access Object 5

Note how the problem is constructed passing the this keyword as a parameter to the constructor (line 8). As the DatabaseAccessProblem passes through the chain, one of the links can be given the opportunity to make callbacks to the Retryable methods of the DAO. Advanced Problem Handling Knowledgebase Building Once a problem handing chain has been devised, the more ambitious developer might build handlers that interface with a knowledgebase. The knowledgebase exposes methods like registerproblem(problem), getsolutionsfor(problems), etc. Applications, while being developed or run in a production environment, could interface with this knowledgebase and potentially save developers and control room operators time and energy. In order for an interactive problem-solution conversation to take place, the Solution interface is defined: public interface Solution { public String getcomments(); public void applysolution(object problemcontext); In the simplest case, a developer working on a Struts application receives an exception containing a pithy, useless message (sound familiar?). If a problem handling chain is executed as part of the application framework, a KnowledgebaseHandler could be defined and invoked as part of the chain. The Problem is serialized and sent to the KnowledgebaseHandler, which checks to see if this problem has occurred before, retrieves solutions, and returns the previous developer s comments in the comments property. If no such problem has occurred before, and the developer solves the issue, s/he can access a web application to enter a solution so other developers can benefit. References [Gamma] [Jakarta] Design Patterns - Elements of Reusable Object-Oriented Software, Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides. Addison Wesley. 1995. http://jakarta.apache.org/struts 6

Copyright 2004 Stieglitech, LLC. All rights reserved. Portions of this document contain material copyrighted by other parties. No portion of this document may be reproduced for commercial purposes without express written consent of the copyright holder. Reproduction for personal or academic use is permitted. All source code is distributed under the GNU General Public License (GPL). See http://www.gnu.org/gpl for a copy of the GNU GPL. 7