Uploaded image for project: 'Application Server 3  4  5 and 6'
  1. Application Server 3 4 5 and 6
  2. JBAS-2433

TimedCachePolicy setResolution() cannot work.

XMLWordPrintable

      The setResolution() method in TimedCachePolicy cannot work. The code in TimedCachePolicy looks like this (I have numbered the lines for reference):

      01 public class TimedCachePolicy ...
      02
      03 protected static Timer resolutionTimer = new Timer(true);
      04
      05 public synchronized void setResolution(int resolution)
      06 {
      07 if( resolution <= 0 )
      08 resolution = 60;
      09 if( resolution != this.resolution )
      10

      { 11 this.resolution = resolution; 12 resolutionTimer.cancel(); 13 resolutionTimer.scheduleAtFixedRate(this, 0, 1000*resolution); 14 }

      15 }

      On line 12 the static resolution Timer is cancelled, followed on line 13 by an attempt to re-schedule this cache at a new fixed rate. This is incorrect, once a Timer is cancelled, no new TimerTasks may be scheduled into it. The result is a runtime exception. Not only that, because the Timer is static (which is fine) the result of the cancellation is to cancel ALL TimerCachePolicy objects scheduled within it.

      The logic needs to read as follows:

      05 public synchronized void setResolution(int resolution)
      06 {
      07 if( resolution <= 0 )
      08 resolution = 60;
      09 if( resolution != this.resolution )
      10

      { 11 this.resolution = resolution; 12 this.cancel(); 13 resolutionTimer.scheduleAtFixedRate(this, 0, 1000*resolution); 14 }

      15 }

      Note the change in line 12. What needs to be cancelled is 'this' TimerTask, not the Timer itself. 'This' TimerTask is then rescheduled on line 13.

              starksm64 Scott Stark (Inactive)
              brettw_jira brettw (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved: