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.LinkedList; 020 import java.util.List; 021 import java.util.Timer; 022 import java.util.TimerTask; 023 024 /** 025 * A timer task that notifies handlers about scheduled timeouts. 026 * 027 * @see Timer 028 * @see TimerTask 029 * 030 * @author Martin Krasser 031 * 032 * @version $Revision 033 */ 034 public class Timeout extends TimerTask { 035 036 private List<TimeoutHandler> timeoutHandlers; 037 038 private Timer timer; 039 040 private long timeout; 041 042 /** 043 * Creates a new timeout task using the given {@link Timer} instance a timeout value. The 044 * task is not scheduled immediately. It will be scheduled by calling this 045 * task's {@link #schedule()} method. 046 * 047 * @param timer 048 * @param timeout 049 */ 050 public Timeout(Timer timer, long timeout) { 051 this.timeoutHandlers = new LinkedList<TimeoutHandler>(); 052 this.timeout = timeout; 053 this.timer = timer; 054 } 055 056 /** 057 * Returns the list of timeout handlers that have been registered for 058 * notification. 059 * 060 * @return the list of timeout handlers 061 */ 062 public List<TimeoutHandler> getTimeoutHandlers() { 063 return timeoutHandlers; 064 } 065 066 /** 067 * Appends a new timeout handler at the end of the timeout handler list. 068 * 069 * @param handler a timeout handler. 070 */ 071 public void addTimeoutHandler(TimeoutHandler handler) { 072 timeoutHandlers.add(handler); 073 } 074 075 /** 076 * inserts a new timeout handler at the beginning of the timeout handler 077 * list. 078 * 079 * @param handler a timeout handler. 080 */ 081 public void addTimeoutHandlerFirst(TimeoutHandler handler) { 082 timeoutHandlers.add(0, handler); 083 } 084 085 /** 086 * Removes all timeout handlers from the timeout handler list. 087 */ 088 public void clearTimeoutHandlers() { 089 this.timeoutHandlers.clear(); 090 } 091 092 /** 093 * Schedules this timeout task. 094 */ 095 public void schedule() { 096 timer.schedule(this, timeout); 097 } 098 099 /** 100 * Notifies all timeout handlers about the scheduled timeout. 101 */ 102 @Override 103 public void run() { 104 for (TimeoutHandler observer : timeoutHandlers) { 105 observer.timeout(this); 106 } 107 } 108 109 }