| Deprecation Announcement This release of the Coherence Incubator is now deprecated. For the latest supported release, help and documentation, please refer to the Coherence Incubator Project on java.net at http://cohinc.java.net. |
|
OverviewThis is an example implementation of the Command Pattern (Wikipedia) built using Oracle Coherence. The Command Pattern advocates that;
This implementation of the Command Pattern additionally advocates that;
Revision HistoryVersion 2.8.5 (changes since 2.8.3)Version 2.8.5 is a patch release to upgrade dependencies to Coherence Common. It also adds the ability to specify the number of threads provided for executing commands, using the command line argument com.oracle.coherence.commandpattern.threadpoolsize. The default size is 5.
Version 2.8.3 (changes since 2.8.2)Version 2.8.3 is a patch release to upgrade dependencies to Coherence Common. Version 2.8.2 (changes since 2.8.1)Version 2.8.2 is a patch release to upgrade dependencies to Coherence Common. Version 2.8.1 (changes since 2.8.0)Version 2.8.1 is a patch release to upgrade dependencies to Coherence Common and support the out and coming Coherence release. Version 2.8.0 (changes since 2.7.3)The most significant upgrade for this release is the adoption of Coherence 3.7. Overtime this will allow the pattern to take advantage of the lastest features, like "lite transactions" to ensure atomicity of multi-cache updates, without the overhead of "heavy" transactions.
|
* Previous releases are available from the attachments page |
Examples
Example source code and uses of this pattern can be found in the Coherence Incubator Examples project.
Structure
The following diagram outlines the core classes and interfaces defined in this implementation of the Command Pattern.

The Command interface specifies the minimum requirements for Command objects. It defines a single method called execute that is invoked by the internal CommandExecutor when a Command is to be executed. The execute method requires a single parameter, called the ExecutionEnvironment. The ExecutionEnvironment provides valuable information to the execute method, including the Context in which the Command is being executed. To submit Commands for execution by CommandExecutor s, you must use an implementation of the CommandSubmitter interface (the DefaultCommandSubmitter class provides the standard implementation).
To define and register a Context about which you may submit Command s for execution, use an implementation of the ContextsManager interface (the DefaultContextsManager class provides the standard implementation). Once a Context is registered, you may use the returned Identifier to submit Command s for execution with a CommandSubmitter.
| Be Careful As Context and Command instances are stored in Coherence cluster members within the Data Grid, they must be Serializable or better still, implement ExternalizableLite or PortableObject |
Command Pattern Usage Walkthrough
Step 9: Commands Automatically Cleaned-up when Executed.
|
Example
Let's start with a simple example where we use the Command Pattern to setValue(T) on the GenericContext.
First, we'll start by writing a simple SetValueCommand that extends AbstractCommand as seen here:
Second, let's write a quick test that submits a number (iCount) of SetValueCommand s on "mycontext" as seen here:
To run this test you will need to start a Coherence cache server, in this example we use the DefaultCacheServer that ships with Coherence as seen here:
Once the cache server is running you can start the CommandPatternExample as a cache client as seen here:
| The SetValueCommand.java, CommandPatternExample.java and coherence-commandpattern-cache-config.xml files are included in the source code distribution. |
Frequently Asked Questions
What is the relationship between the Functor Pattern, Command Pattern, Entry Processors and the Invocation Service for performing work in a cluster?
The following table outlines the main differences.
| Functor Pattern | Command Pattern | EntryProcessors | InvocationService | |
|---|---|---|---|---|
| Processing Target | A Context | A Context | A Cache Entry | A Cluster Member |
| Submission Behavior | Non-Blocking | Non-Blocking | Blocking | Blocking and Non-Blocking |
| Execution Order | Order of Submission | Order of Submission | Order of Submission (unless using PriorityTasks) | Order of Submission |
| Supports Return Values? (and re-throwing Exceptions) |
Yes | No | Yes | Yes |
| Guaranteed to Execute? (if Submitter terminates after submission) |
Yes | Yes | Yes (submitter blocks) | Yes (for blocking), No (for non-blocking) |
| Guaranteed to Execute? (if Target JVM terminates during execution) |
Yes | Yes | Yes (submitter automatically retries) | No (retry up to developer) |
| Submitted requests recoverable from disk? | Yes (when using Cache Store) | Yes (when using Cache Store) | No | No |
| Request may be cancelled? | Yes | Yes | No | Yes (for non-blocking) |
What is a ManagementStrategy ?
A ManagementStrategy defines how Commands are managed in a Coherence Cluster. By default Commands are always co-located in the JVM that is hosting the Context associated with the said Commands. This provides instant access to the Commands for a CommandExecutor but has the disadvantage of limiting the number of Commands that may be queued for execution (before a JVM may run out of memory). The alternative is to use the DISTRIBUTED strategy, where the Commands are distributed across the cluster and only retrieved for execution when required. This provides the advantage of enabling massive capacity (scaling to the size of the cluster and beyond when disk storage is configured), but has the disadvantage that retrieving Commands for execution may take longer.
How do I monitor the execution of Commands ?
The Command Pattern CommandExecutors are JMX enabled by default. In order to monitor Command execution for a Context, simply enable JMX for Coherence and look for the "CommandExecutors" in your JMX monitoring application (ie: Something like JConsole). All kinds of JMX information is available, including Min, Max and Average Execution time, together with the number of current Commands pending to be executed and those that have been executed.
References and Additional Information
- Wikipedia - Command Pattern









