[ Team LiB ] Previous Section Next Section

A.3 Scalability Patterns

Asynchronous Page

Goal

Cache remote data as it is generated.

Participants

Publishers

Remote entities that generate new data at some interval. May also notify subscribers when new data is available.

Subscriber

A singleton object responsible for retrieving, formatting, and caching remote data either as it is generated or at some interval.

Models

Store state data for applications.

Views

When requests are received, translate model data into a format appropriate for display to the user (Figure A-12).

Figure A-12. Interactions in the Asynchronous Page pattern
figs/j2ee_aa12.gif

Interactions

The subscriber retrieves data from the publisher either when it has changed or at some predefined interval. The subscriber processes the data and updates all appropriate models. When requests are received, the view translates current model data into a format suitable for the user.

Notes

The most effective use of this pattern is when a single subscriber downloads dynamic data and uses it to generate static HTML pages on each web server. It is also useful in situations where multiple web servers use separate data models that must be kept up to date.

Caching Filter

Goal

Minimize repeated page generation by caching dynamic pages when they are generated (Figure A-13).

Figure A-13. Classes in the Caching Filter pattern
figs/j2ee_aa13.gif

Participants

Filter

A common interface implemented by the cache, decorators and target.

Cache filter

Intercepts all requests and replies efficiently with cached pages if available. If no cached data is available, caches the results of generating the page.

Decorators

Encapsulate a piece of common functionality, while presenting the same interface as the target.

Target

The final object in the request processing chain. Coordinates all specific activities, such as input handling. Typically the front controller (Figure A-14).

Figure A-14. Interactions in the Caching Filter pattern
figs/j2ee_aa14.gif

Interactions

The cache filter intercepts all requests destined for target and determines if the page has been cached. If it is cached, data is returned from the cache. If not, the remaining decorators in the chain and the target are executed to generate the page, and the results are cached.

Notes

The caching filter is a variation of the decorator pattern. To ease implementation of filters such as the caching filter, the servlet API allows you to decorate the HTTPServletResponse object in order to store the results of request processing.

Resource Pool

Goal

Decrease costs of instantiating and maintaining large objects using a pool of pre-generated objects (Figure A-15).

Figure A-15. Classes in the Resource Pool pattern
figs/j2ee_aa15.gif

Participants

Pool

A limited collection of pre-generated objects that can be loaned out and returned.

Factory

Used by the pool to generate new instances of a resource or validate a returned resource.

Resource

An object that will be loaned out by the pool (Figure A-16).

Figure A-16. Interactions in the Resource Pool pattern
figs/j2ee_aa16.gif

Interactions

The client requests objects from the pool, which returns a resource if one is available. If no resources are available, but the pool is not at capacity, a new instance is generated using the factory. If all resources are loaned out, the client waits until one becomes available. When the client has finished using the resource, it returns it to the pool, which uses the factory to validate the returned resource.

Notes

A resource pool can easily be implemented based on the Java collections APIs.

    [ Team LiB ] Previous Section Next Section