001: package example;
002:
003: import com.caucho.log.Log;
004: import com.caucho.util.L10N;
005: import java.util.logging.Level;
006: import java.util.logging.Logger;
007:
008: public class PeriodicTask implements PeriodicTaskMBean,
009: javax.resource.spi.work.Work {
010: static protected final Logger log = Logger
011: .getLogger(PeriodicTask.class.getName());
012:
013: private long _estimatedAverageTime = 5000;
014:
015: private boolean _isActive = false;
016: private long _lastActiveTime = -1;
017: private long _totalActiveCount = 0;
018: private long _totalActiveTime = 0;
019:
020: public PeriodicTask() {
021: }
022:
023: /**
024: * {@inheritDoc}
025: */
026: public void setEstimatedAverageTime(long estimatedAverageTime) {
027: _estimatedAverageTime = estimatedAverageTime;
028: }
029:
030: /**
031: * {@inheritDoc}
032: */
033: public long getEstimatedAverageTime() {
034: return _estimatedAverageTime;
035: }
036:
037: public void init() throws Exception {
038: }
039:
040: /**
041: * {@inheritDoc}
042: */
043: public boolean isActive() {
044: synchronized (this ) {
045: return _isActive == true;
046: }
047: }
048:
049: /**
050: * {@inheritDoc}
051: */
052: public long getEstimatedTimeRemaining() {
053: synchronized (this ) {
054: if (_isActive) {
055: long now = System.currentTimeMillis();
056: long activeTime = now - _lastActiveTime;
057: long estimate = getAverageActiveTime() - activeTime;
058: if (estimate < 0)
059: return 1000;
060: else
061: return estimate;
062: } else
063: return 0;
064: }
065: }
066:
067: /**
068: * {@inheritDoc}
069: */
070: public long getLastActiveTime() {
071: return _lastActiveTime;
072: }
073:
074: /**
075: * {@inheritDoc}
076: */
077: public long getTotalActiveCount() {
078: return _totalActiveCount;
079: }
080:
081: /**
082: * {@inheritDoc}
083: */
084: public long getTotalActiveTime() {
085: return _totalActiveTime;
086: }
087:
088: /**
089: * {@inheritDoc}
090: */
091: public long getAverageActiveTime() {
092: synchronized (this ) {
093: long count = _isActive ? _totalActiveCount - 1
094: : _totalActiveCount;
095: if (count < 1)
096: return _estimatedAverageTime;
097: else
098: return _totalActiveTime / count;
099: }
100: }
101:
102: /**
103: * {@inheritDoc}
104: *
105: * Various statistical information is collected and maintained by this method,
106: * the actual task is performed by the performTask() method.
107: */
108: public void run() {
109: synchronized (this ) {
110: if (_isActive == true)
111: return;
112: _isActive = true;
113:
114: _lastActiveTime = System.currentTimeMillis();
115: _totalActiveCount++;
116: }
117:
118: try {
119: log.fine("performing task");
120:
121: performTask();
122:
123: log.fine("done performing task");
124: } catch (Exception ex) {
125: log.log(Level.WARNING, "task failed", ex);
126: } finally {
127: synchronized (this ) {
128: _totalActiveTime += (System.currentTimeMillis() - _lastActiveTime);
129: _isActive = false;
130: }
131: }
132: }
133:
134: protected void performTask() throws Exception {
135: // for the purposes of this tutorial, sleep for 10 seconds
136: // to imitate a task that takes 10 seconds to perform
137: Thread.sleep(10 * 1000L);
138: }
139:
140: /**
141: * Required implementation of javax.resource.spi.work.Work.release()
142: */
143: public void release() {
144: }
145: }
|