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: 697732 $
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
046 * a timer
047 * @param timeout
048 * a timeout value.
049 */
050 public Timeout(Timer timer, long timeout) {
051 this.timeout = timeout;
052 this.timer = timer;
053 }
054
055 /**
056 * Returns the timeout handler that has been registered for notification.
057 *
058 * @return the timeout handler.
059 */
060 public TimeoutHandler getTimeoutHandlers() {
061 return timeoutHandler;
062 }
063
064 /**
065 * Sets a timeout handler for receiving timeout notifications.
066 *
067 * @param timeoutHandler
068 * a timeout handler.
069 */
070 public void setTimeoutHandler(TimeoutHandler timeoutHandler) {
071 this.timeoutHandler = timeoutHandler;
072 }
073
074 /**
075 * Schedules this timeout task.
076 */
077 public void schedule() {
078 timer.schedule(this, timeout);
079 }
080
081 /**
082 * Notifies the timeout handler about the scheduled timeout.
083 */
084 @Override
085 public void run() {
086 timeoutHandler.timeout(this);
087 }
088
089 }