Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
DelayProcessorSupport |
|
| 1.8;1.8 |
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.camel.processor; |
|
18 | ||
19 | import java.util.concurrent.CountDownLatch; |
|
20 | import java.util.concurrent.TimeUnit; |
|
21 | ||
22 | import org.apache.camel.AlreadyStoppedException; |
|
23 | import org.apache.camel.Exchange; |
|
24 | import org.apache.camel.Processor; |
|
25 | import org.apache.commons.logging.Log; |
|
26 | import org.apache.commons.logging.LogFactory; |
|
27 | ||
28 | /** |
|
29 | * A useful base class for any processor which provides some kind of throttling |
|
30 | * or delayed processing |
|
31 | * |
|
32 | * @version $Revision: $ |
|
33 | */ |
|
34 | public abstract class DelayProcessorSupport extends DelegateProcessor { |
|
35 | 3 | private static final transient Log LOG = LogFactory.getLog(Delayer.class); |
36 | 6 | private CountDownLatch stoppedLatch = new CountDownLatch(1); |
37 | 6 | private boolean fastStop = true; |
38 | ||
39 | public DelayProcessorSupport(Processor processor) { |
|
40 | 6 | super(processor); |
41 | 6 | } |
42 | ||
43 | public void process(Exchange exchange) throws Exception { |
|
44 | 12 | delay(exchange); |
45 | 12 | super.process(exchange); |
46 | 12 | } |
47 | ||
48 | public boolean isFastStop() { |
|
49 | 6 | return fastStop; |
50 | } |
|
51 | ||
52 | /** |
|
53 | * Enables & disables a fast stop; basically to avoid waiting a possibly |
|
54 | * long time for delays to complete before the context shuts down; instead |
|
55 | * the current processing method throws |
|
56 | * {@link org.apache.camel.AlreadyStoppedException} to terminate processing. |
|
57 | */ |
|
58 | public void setFastStop(boolean fastStop) { |
|
59 | 0 | this.fastStop = fastStop; |
60 | 0 | } |
61 | ||
62 | protected void doStop() throws Exception { |
|
63 | 6 | stoppedLatch.countDown(); |
64 | 6 | super.doStop(); |
65 | 6 | } |
66 | ||
67 | protected abstract void delay(Exchange exchange) throws Exception; |
|
68 | ||
69 | /** |
|
70 | * Wait until the given system time before continuing |
|
71 | * |
|
72 | * @param time the system time to wait for |
|
73 | * @param exchange the exchange being processed |
|
74 | */ |
|
75 | protected void waitUntil(long time, Exchange exchange) throws Exception { |
|
76 | while (true) { |
|
77 | 6 | long delay = time - currentSystemTime(); |
78 | 6 | if (delay < 0) { |
79 | 3 | return; |
80 | } else { |
|
81 | 3 | if (isFastStop() && (isStopped() || isStopping())) { |
82 | 0 | throw new AlreadyStoppedException(); |
83 | } |
|
84 | try { |
|
85 | 3 | sleep(delay); |
86 | 0 | } catch (InterruptedException e) { |
87 | 0 | handleSleepInteruptedException(e); |
88 | 3 | } |
89 | } |
|
90 | 3 | } |
91 | } |
|
92 | ||
93 | protected void sleep(long delay) throws InterruptedException { |
|
94 | 3 | if (LOG.isDebugEnabled()) { |
95 | 0 | LOG.debug("Sleeping for: " + delay + " millis"); |
96 | } |
|
97 | 3 | if (isFastStop()) { |
98 | 3 | stoppedLatch.await(delay, TimeUnit.MILLISECONDS); |
99 | 3 | } else { |
100 | 0 | Thread.sleep(delay); |
101 | } |
|
102 | 3 | } |
103 | ||
104 | /** |
|
105 | * Called when a sleep is interupted; allows derived classes to handle this |
|
106 | * case differently |
|
107 | */ |
|
108 | protected void handleSleepInteruptedException(InterruptedException e) { |
|
109 | 0 | LOG.debug("Sleep interupted: " + e, e); |
110 | 0 | } |
111 | ||
112 | protected long currentSystemTime() { |
|
113 | 15 | return System.currentTimeMillis(); |
114 | } |
|
115 | } |