Wednesday, December 12, 2007

How to add Messaging to your application

Some simple examples for adding messaging to your own applications from the EAI patterns bible.

Request/Reply Example

This is a simple but powerful example, transmitting a request and transmitting back a reply. It consists of two main classes:

  • Requestor — The object that sends the request message and expects to receive the reply message.
  • Replier — The object that receives the request message and sends a reply message in response.

These two simple classes sending simple messages illustrate a number of the patterns:

The example code also demostrates a couple of patterns from the Messaging Endpoints chapter later in the book:

While this book is technology-, product-, and language-neutral, code cannot be. So we’ve choosen two messaging programming platforms to implement this example:

  • The JMS API in Java J2EE
  • The MSMQ API in Microsoft .NET using C#

The same request/reply example is implemented in both platforms. So choose your favorite platform as an example of how messaging works. If you’d like to see how messaging works on the other platform, even if you don’t know how to write code for that platform, you should be able to figure out how that code works by comparing it to the code in the language you already know.

Publish/Subscribe Example

This example explores how to implement the Observer pattern using a Publish-Subscribe Channel. It considers distribution and threading issues and discusses how messaging greatly simplifies these issues. The example shows how to implement both the push and pull models of notification and compares the consequences of each. It also explores how to design an adaquate set of channels needed for a complex enterprise with numerous subjects notifing numerous observers.

The discussion and sample code will illustrate several patterns:

  • Publish-Subscribe Channel — The channel that provides publish/subscribe notification.
  • Event Message — The message type used to send notifications.
  • Request-Reply — The technique used as part of the pull model for an observer to request state from the subject.
  • Command Message — The message type used by an observer to request state from the subject.
  • Document Message — The message type used by a subject to send its state to an observer.
  • Return Address — Tells the subject how to send the state to the observer.
  • Datatype Channel — The main guideline for whether two unrelated subjects can use the same channel to update the same group of observers.

The example code also demostrates a couple of patterns from the Messaging Endpoints chapter later in the book:

  • Messaging Gateway — How the subject and observer encapsulate the messaging code so that they are not messaging-specific.
  • Event-Driven Consumer — How the observers consume notification messages.
  • Durable Subscriber — An observer that does not want to miss notifications, even if the observer is temporarily disconnected when the notification is sent.

This example is implemented in Java using JMS because JMS supports Publish-Subscribe Channel as an explicit feature of the API through its Topic interface. .NET does not provide a similar level of support for using the publish/subscribe semantics in MSMQ; when it does, the techniques in the JMS example should be readily applicable to .NET programs as well.

Enterprise Integration Patterns - Introduction to Simple Messaging Examples

Enterprise Integration Patterns and messaging

We can't talk about Java messaging without talking about Enterprise integration. Below are some EAI patterns from the EAI patterns bible.

Messaging Systems

Introduction to Messaging Systems


Message Channel
How does one application communicate with another using messaging?


Message
How can two applications connected by a message channel exchange a piece of information?


Pipes and Filters
How can we perform complex processing on a message while maintaining independence and flexibility?


Message Router
How can you decouple individual processing steps so that messages can be passed to different filters depending on a set of conditions?


Message Translator
How can systems using different data formats communicate with each other using messaging?


Message Endpoint
How does an application connect to a messaging channel to send and receive messages?

Messaging Channels

Introduction to Messaging Channels


Point-to-Point Channel
How can the caller be sure that exactly one receiver will receive the document or perform the call?


Publish-Subscribe Channel
How can the sender broadcast an event to all interested receivers?


Datatype Channel
How can the application send a data item such that the receiver will know how to process it?


Invalid Message Channel
How can a messaging receiver gracefully handle receiving a message that makes no sense?


Dead Letter Channel
What will the messaging system do with a message it cannot deliver?


Guaranteed Delivery
How can the sender make sure that a message will be delivered, even if the messaging system fails?


Channel Adapter
How can you connect an application to the messaging system so that it can send and receive messages?


Messaging Bridge
How can multiple messaging systems be connected so that messages available on one are also available on the others?


Message Bus
What is an architecture that enables separate applications to work together, but in a decoupled fashion such that applications can be easily added or removed without affecting the others?

Message Construction

Introduction to Message Construction


Command Message
How can messaging be used to invoke a procedure in another application?


Document Message
How can messaging be used to transfer data between applications?


Event Message
How can messaging be used to transmit events from one application to another?


Request-Reply
When an application sends a message, how can it get a response from the receiver?


Return Address
How does a replier know where to send the reply?


Correlation Identifier
How does a requestor that has received a reply know which request this is the reply for?


Message Sequence
How can messaging transmit an arbitrarily large amount of data?


Message Expiration
How can a sender indicate when a message should be considered stale and thus shouldn’t be processed?

Format Indicator
How can a message’s data format be designed to allow for possible future changes?

Interlude: Simple Messaging

Introduction to Simple Messaging Examples

JMS Request/Reply Example

.NET Request/Reply Example

JMS Publish/Subscribe Example

Message Routing

Introduction to Message Routing


Content-Based Router
How do we handle a situation where the implementation of a single logical function (e.g., inventory check) is spread across multiple physical systems?


Message Filter
How can a component avoid receiving uninteresting messages?


Dynamic Router
How can you avoid the dependency of the router on all possible destinations while maintaining its efficiency?


Recipient List
How do we route a message to a list of dynamically specified recipients?


Splitter
How can we process a message if it contains multiple elements, each of which may have to be processed in a different way?


Aggregator
How do we combine the results of individual, but related messages so that they can be processed as a whole?


Resequencer
How can we get a stream of related but out-of-sequence messages back into the correct order?


Composed Message Processor
How can you maintain the overall message flow when processing a message consisting of multiple elements, each of which may require different processing?

Scatter-Gather
How do you maintain the overall message flow when a message needs to be sent to multiple recipients, each of which may send a reply?


Routing Slip
How do we route a message consecutively through a series of processing steps when the sequence of steps is not known at design-time and may vary for each message?


Process Manager
How do we route a message through multiple processing steps when the required steps may not be known at design-time and may not be sequential?


Message Broker
How can you decouple the destination of a message from the sender and maintain central control over the flow of messages?

Message Transformation

Introduction to Message Transformation


Envelope Wrapper
How can existing systems participate in a messaging exchange that places specific requirements on the message format, such as message header fields or encryption?


Content Enricher
How do we communicate with another system if the message originator does not have all the required data items available?


Content Filter
How do you simplify dealing with a large message, when you are interested only in a few data items?


Claim Check
How can we reduce the data volume of message sent across the system without sacrificing information content?


Normalizer
How do you process messages that are semantically equivalent, but arrive in a different format?

Canonical Data Model
How can you minimize dependencies when integrating applications that use different data formats?

Interlude: Composed Messaging

Introduction to Composed Messaging Examples

Synchronous Implementation using Web Services

Asynchronous Implementation with MSMQ

Asynchronous Implementation with TIBCO ActiveEnterprise

Messaging Endpoints

Introduction to Messaging Endpoints


Messaging Gateway
How do you encapsulate access to the messaging system from the rest of the application?

Messaging Mapper
How do you move data between domain objects and the messaging infrastructure while keeping the two independent of each other?


Transactional Client
How can a client control its transactions with the messaging system?


Polling Consumer
How can an application consume a message when the application is ready?


Event-Driven Consumer
How can an application automatically consume messages as they become available?


Competing Consumers
How can a messaging client process multiple messages concurrently?


Message Dispatcher
How can multiple consumers on a single channel coordinate their message processing?


Selective Consumer
How can a message consumer select which messages it wishes to receive?


Durable Subscriber
How can a subscriber avoid missing messages while it’s not listening for them?

Idempotent Receiver
How can a message receiver deal with duplicate messages?


Service Activator
How can an application design a service to be invoked both via various messaging technologies and via non-messaging techniques?

Enterprise Integration Patterns - Table of Contents