/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.activemq.thread;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.activemq.util.ServiceStopper;
import org.apache.activemq.util.ServiceSupport;

/**
 * 
 */
public final class Scheduler extends ServiceSupport { 
    private final String name;
	private Timer timer;
    private final HashMap<Runnable, TimerTask> timerTasks = new HashMap<Runnable, TimerTask>();
    
    public Scheduler (String name) {
        this.name = name;
    }
        
    public void executePeriodically(final Runnable task, long period) {
    	TimerTask timerTask = new SchedulerTimerTask(task);
        timer.scheduleAtFixedRate(timerTask, period, period);
        timerTasks.put(task, timerTask);
    }

    /*
     * execute on rough schedual based on termination of last execution. There is no
     * compensation (two runs in quick succession) for delays
     */
    public synchronized void schedualPeriodically(final Runnable task, long period) {
        TimerTask timerTask = new SchedulerTimerTask(task);
        timer.schedule(timerTask, period, period);
        timerTasks.put(task, timerTask);
    }
    
    public synchronized void cancel(Runnable task) {
    	TimerTask ticket = timerTasks.remove(task);
        if (ticket != null) {
            ticket.cancel();
            timer.purge();//remove cancelled TimerTasks
        }
    }

    public synchronized void executeAfterDelay(final Runnable task, long redeliveryDelay) {
    	TimerTask timerTask = new SchedulerTimerTask(task);
        timer.schedule(timerTask, redeliveryDelay);
    }
    
    public void shutdown() {
        timer.cancel();
    }

    @Override
    protected synchronized void doStart() throws Exception {
        this.timer = new Timer(name, true);
        
    }

    @Override
    protected synchronized void doStop(ServiceStopper stopper) throws Exception {
       if (this.timer != null) {
           this.timer.cancel();
       }
        
    }

    public String getName() {
        return name;
    }
}

@@ -1015,17 +1015,16 @@
 %0A%0A/**%0A *
- 
 %0A */%0Apub
@@ -1073,17 +1073,16 @@
 upport %7B
- 
 %0A    pri
@@ -1105,17 +1105,20 @@
 g name;%0A
-%09
+    
 private 
@@ -1226,20 +1226,16 @@
 ask%3E();%0A
-    
 %0A    pub
@@ -1292,32 +1292,24 @@
 name;%0A    %7D%0A
-        
 %0A    public 
@@ -1365,33 +1365,36 @@
 g period) %7B%0A    
-%09
+    
 TimerTask timerT
@@ -1455,19 +1455,8 @@
 dule
-AtFixedRate
 (tim
@@ -1565,18 +1565,18 @@
 h schedu
-a
 l
+e
  based o
@@ -1926,36 +1926,32 @@
 merTask);%0A    %7D%0A
-    
 %0A    public sync
@@ -1984,33 +1984,36 @@
 ble task) %7B%0A    
-%09
+    
 TimerTask ticket
@@ -2272,9 +2272,12 @@
     
-%09
+    
 Time
@@ -2382,20 +2382,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub
@@ -2562,24 +2562,16 @@
  true);%0A
-        
 %0A    %7D%0A%0A
@@ -2740,24 +2740,16 @@
       %7D%0A
-        
 %0A    %7D%0A%0A

fix for: https://issues.apache.org/jira/browse/AMQ-3031