KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > beanflow > TimeoutActivity


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

17 package org.apache.servicemix.beanflow;
18
19 import java.util.Timer JavaDoc;
20 import java.util.TimerTask JavaDoc;
21
22 /**
23  * A really simple flow which if the flow is not started within a specific
24  * timeout period then the flow fails.
25  *
26  * @version $Revision: $
27  */

28 public class TimeoutActivity extends AbstractActivity {
29
30     private State<Boolean JavaDoc> timedOut = new DefaultState<Boolean JavaDoc>(Boolean.FALSE);
31     private TimerTask JavaDoc timeoutTask;
32
33     /**
34      * Called when the timeout event occurs
35      */

36     public void onTimedOut() {
37         // ignore any timeout events after the workflow is stopped
38
if (!isStopped()) {
39             timedOut.set(Boolean.TRUE);
40         }
41     }
42
43     public void run() {
44         if (!isStopped()) {
45             if (timedOut.get().booleanValue()) {
46                 fail("Timed out");
47             }
48             else {
49                 onValidStateChange();
50             }
51         }
52     }
53
54     /**
55      * Returns true if the flow timed out
56      */

57     public boolean isTimedOut() {
58         return timedOut.get();
59     }
60
61     public void startWithTimeout(Timer JavaDoc timer, long timeout) {
62         scheduleTimeout(timer, timeout);
63         start();
64     }
65
66     /**
67      * Schedules the flow to timeout at the given value
68      */

69     public void scheduleTimeout(Timer JavaDoc timer, long timeout) {
70         if (timeout > 0) {
71             timer.schedule(getTimeoutTask(), timeout);
72         }
73     }
74
75     /**
76      * Returns a timer task for the timeout event
77      */

78     public TimerTask JavaDoc getTimeoutTask() {
79         if (timeoutTask == null) {
80             timeoutTask = new TimerTask JavaDoc() {
81                 public void run() {
82                     onTimedOut();
83                 }
84             };
85         }
86         return timeoutTask;
87     }
88
89     /**
90      * A hook so that derived classes can ignore whether the flow is started or
91      * timed out and instead focus on the other state
92      */

93     protected void onValidStateChange() {
94     }
95 }
96
Popular Tags