001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.async.render;
035:
036: import edu.emory.mathcs.backport.java.util.concurrent.ScheduledExecutorService;
037: import edu.emory.mathcs.backport.java.util.concurrent.ScheduledFuture;
038: import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041:
042: /**
043: * The IntervalRenderer is type of {@link GroupAsyncRenderer} that is used to
044: * request a single render pass on a group of Renderables. The render pass is
045: * executed repeatedly on a set interval. The interval is measured from the
046: * start of one render pass to the start of the next render pass so does not
047: * take the time to complete the render pass into account. If the time to
048: * complete the render pass exceeds the interval then the next render pass is
049: * done on a best effort basis. IntervalRenderers can be created and used
050: * directly but it is recommended to use the RenderManager to create and managed
051: * named render groups.
052: *
053: * @author ICEsoft Technologies, Inc.
054: * @see RenderManager, GroupAsyncRenderer
055: */
056: public class IntervalRenderer extends OnDemandRenderer implements
057: Runnable {
058:
059: private static Log log = LogFactory.getLog(IntervalRenderer.class);
060:
061: private long interval = 60000;
062: private boolean isStarted = false;
063: private ScheduledFuture future;
064:
065: /**
066: * Get the currently specified interval. If no interval has been explicitly
067: * set, then the default interval value (60000 ms) is used.
068: *
069: * @return The current interval value in ms.
070: */
071: public long getInterval() {
072: return interval;
073: }
074:
075: /**
076: * Set the interval to wait before executing the next render pass. Once the
077: * rendering has been started, setting the interval has no effect.
078: *
079: * @param interval The time in ms to wait before starting the next render
080: * pass.
081: */
082: public void setInterval(long interval) {
083: this .interval = interval;
084: }
085:
086: /**
087: * Schedules a render pass on the group of Renderables using the interval
088: * value specified using {@link #setInterval}.
089: */
090: public void requestRender() {
091:
092: if (isStarted) {
093: return;
094: }
095:
096: isStarted = true;
097:
098: ScheduledExecutorService scheduleService = renderManager
099: .getScheduledService();
100: future = scheduleService.scheduleAtFixedRate(this , interval,
101: interval, TimeUnit.MILLISECONDS);
102:
103: if (log.isDebugEnabled()) {
104: log.debug("interval render started: interval is "
105: + interval + " ms");
106: }
107: }
108:
109: public void requestStop() {
110: super .requestStop();
111: if (future != null && !future.isDone()) {
112: future.cancel(false);
113: }
114: isStarted = false;
115: }
116:
117: public void run() {
118: super .requestRender();
119: }
120:
121: public void dispose() {
122: super.dispose();
123: future = null;
124: }
125: }
|