Functor Pattern

Skip to end of metadata
Go to start of metadata

The Functor Pattern

This is an example implementation of Function Objects (Wikipedia) or as it is also known, the Functor Pattern, built with Coherence.

The Functor Pattern is an extension to the Command Pattern. In fact the semantics are identical with the exception that the Functor Pattern additionally provides a mechanism to return values (or re-throw exceptions) to the Submitter (using Java 5+ Futures) where as the Command Pattern does not provide such capabilities. Consequently in order to use Functor Pattern it is highly recommended that you understand the Command Pattern.

Dependencies

This project (like other Coherence Incubator projects) uses Apache Ivy for dependency specification and management. While a standard ivy.xml definition file ships with the source and documentation distribution, the following diagram visually indicates the current dependencies.

Rationale

The Functor Pattern provides a useful alternative to Oracle Coherence EntryProcessors with the advantage that Functors are executed asynchronously, instead of synchronously as is the case with EntryProcessors.

What's New?

Changes made as a part of version 1.4.3:

  • Migrated to use coherence-common-1.7.3, coherence-commandpattern-2.7.3

Changes made as a part of version 1.4.2:

  • Migrated to use coherence-common-1.7.2, coherence-commandpattern-2.7.2

The following changes have been made since the Functor Pattern 1.4.0 release:

The following changes have been made since the Functor Pattern 1.3.0 release:

Coherence 3.6 Support

Release Name: Version 1.4.3: November 22, 2010
Target Platforms: Java Standard Edition 6+
Requires Coherence: 3.6+
Dependencies: Coherence Common
Command Pattern
Download: coherence-3.6-functorpattern-1.4.3.20019.jar
MD5:bbda5fa7133e72891e152d1aa99dbdd3
Build Instructions: Building the Coherence Incubator Projects
Source Code and
Documentation:
coherence-3.6-functorpattern-1.4.3.20019-src.zip
MD5:e7a1051b675b4f0864f53423d2e117cb

Coherence 3.5 Support

Release Name: Version 1.4.3: November 22, 2010
Target Platforms: Java Standard Edition 6+
Requires Coherence: 3.5.3 Patch 3+
Dependencies: Coherence Common
Command Pattern
Download: coherence-3.5-functorpattern-1.4.3.20019.jar
MD5:ec4425c850a5a56edbf9ef4e93953185
Build Instructions: Building the Coherence Incubator Projects
Source Code and
Documentation:
coherence-3.5-functorpattern-1.4.3.20019-src.zip
MD5:7cf22e2d09236ed9cecb208105f984a6

Previous Releases

The following releases are provided for historical purposes only.
Functor Pattern 1.3.0, Functor Pattern 1.2.0, Functor Pattern 1.1.0, Functor Pattern 1.0.0

Example

Let's start with a simple example where we use the Functor Pattern to asynchronously increment and return the value of a Counter

First, we'll start by writing a simple Counter:

Counter.java

Second, let's write a Functor that will increment the value of a Counter

NextValueFunctor.java

Lastly, let's write a simple example to test our Functor.

FunctorPatternExample.java

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)

Can Functors and Commands be submitted to the same Context?
Yes. Functors and Commands may be submitted for execution against the same Context. They will be executed in the order of submission from the Submitter. Further, FunctorSubmitters may be used instead of CommandSubmitters (as the FunctorSubmitter interface is a sub-interface of the CommandSubmitter interface)

How do I monitor the execution of Functors ?

As the Functor Pattern uses the Command Pattern for the internal implementation (and that is JMX enabled), all you need to do is simply enable JMX.

References and Additional Information

Labels:
current current Delete
pattern pattern Delete
project project Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.