001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.processor.resequencer;
018    
019    import java.util.Timer;
020    import java.util.TimerTask;
021    
022    /**
023     * A timer task that notifies handlers about scheduled timeouts.
024     * 
025     * @see Timer
026     * @see TimerTask
027     * 
028     * @author Martin Krasser
029     * 
030     * @version $Revision: 747062 $
031     */
032    public class Timeout extends TimerTask {
033        
034        private TimeoutHandler timeoutHandler;
035        
036        private Timer timer;
037        
038        private long timeout;
039        
040        /**
041         * Creates a new timeout task using the given {@link Timer} instance and
042         * timeout value. The task is not scheduled immediately. It will be
043         * scheduled by calling this task's {@link #schedule()} method.
044         * 
045         * @param timer a timer
046         * @param timeout a timeout value.
047         */
048        public Timeout(Timer timer, long timeout) {
049            this.timeout = timeout;
050            this.timer = timer;
051        }
052    
053        /**
054         * Returns the timeout handler that has been registered for notification.
055         * 
056         * @return the timeout handler.
057         */
058        public TimeoutHandler getTimeoutHandlers() {
059            return timeoutHandler;
060        }
061        
062        /**
063         * Sets a timeout handler for receiving timeout notifications.
064         * 
065         * @param timeoutHandler
066         *            a timeout handler.
067         */
068        public void setTimeoutHandler(TimeoutHandler timeoutHandler) {
069            this.timeoutHandler = timeoutHandler;
070        }
071        
072        /**
073         * Schedules this timeout task.
074         */
075        public void schedule() {
076            timer.schedule(this, timeout);
077        }
078    
079        /**
080         * Notifies the timeout handler about the scheduled timeout.
081         */
082        @Override
083        public void run() {
084            timeoutHandler.timeout(this);
085        }
086    
087    }