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     * @version 
029     */
030    public class Timeout extends TimerTask {
031        
032        private TimeoutHandler timeoutHandler;
033        
034        private Timer timer;
035        
036        private long timeout;
037        
038        /**
039         * Creates a new timeout task using the given {@link Timer} instance and
040         * timeout value. The task is not scheduled immediately. It will be
041         * scheduled by calling this task's {@link #schedule()} method.
042         * 
043         * @param timer a timer
044         * @param timeout a timeout value.
045         */
046        public Timeout(Timer timer, long timeout) {
047            this.timeout = timeout;
048            this.timer = timer;
049        }
050    
051        /**
052         * Returns the timeout handler that has been registered for notification.
053         * 
054         * @return the timeout handler.
055         */
056        public TimeoutHandler getTimeoutHandlers() {
057            return timeoutHandler;
058        }
059        
060        /**
061         * Sets a timeout handler for receiving timeout notifications.
062         * 
063         * @param timeoutHandler
064         *            a timeout handler.
065         */
066        public void setTimeoutHandler(TimeoutHandler timeoutHandler) {
067            this.timeoutHandler = timeoutHandler;
068        }
069        
070        /**
071         * Schedules this timeout task.
072         */
073        public void schedule() {
074            timer.schedule(this, timeout);
075        }
076    
077        /**
078         * Notifies the timeout handler about the scheduled timeout.
079         */
080        @Override
081        public void run() {
082            timeoutHandler.timeout(this);
083        }
084    
085    }