| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| Delayer |
|
| 0.0;0 |
| 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 org.apache.camel.Exchange; |
|
| 20 | import org.apache.camel.Expression; |
|
| 21 | import org.apache.camel.Processor; |
|
| 22 | import org.apache.camel.util.ExpressionHelper; |
|
| 23 | ||
| 24 | /** |
|
| 25 | * A <a href="http://activemq.apache.org/camel/delayer.html">Delayer</a> which |
|
| 26 | * delays processing the exchange until the correct amount of time has elapsed |
|
| 27 | * using an expression to determine the delivery time. <p/> For example if you |
|
| 28 | * wish to delay JMS messages by 25 seconds from their publish time you could |
|
| 29 | * create an instance of this class with the expression |
|
| 30 | * <code>header("JMSTimestamp")</code> and a delay value of 25000L. |
|
| 31 | * |
|
| 32 | * @version $Revision: 1.1 $ |
|
| 33 | */ |
|
| 34 | public class Delayer extends DelayProcessorSupport { |
|
| 35 | private Expression<Exchange> timeExpression; |
|
| 36 | private long delay; |
|
| 37 | ||
| 38 | public Delayer(Processor processor, Expression<Exchange> timeExpression, long delay) { |
|
| 39 | 3 | super(processor); |
| 40 | 3 | this.timeExpression = timeExpression; |
| 41 | 3 | this.delay = delay; |
| 42 | 3 | } |
| 43 | ||
| 44 | @Override |
|
| 45 | public String toString() { |
|
| 46 | 3 | return "Delayer[on: " + timeExpression + " delay: " + delay + " to: " + getProcessor() + "]"; |
| 47 | } |
|
| 48 | ||
| 49 | // Properties |
|
| 50 | // ------------------------------------------------------------------------- |
|
| 51 | public long getDelay() { |
|
| 52 | 0 | return delay; |
| 53 | } |
|
| 54 | ||
| 55 | /** |
|
| 56 | * Sets the delay from the publish time; which is typically the time from |
|
| 57 | * the expression or the current system time if none is available |
|
| 58 | */ |
|
| 59 | public void setDelay(long delay) { |
|
| 60 | 0 | this.delay = delay; |
| 61 | 0 | } |
| 62 | ||
| 63 | // Implementation methods |
|
| 64 | // ------------------------------------------------------------------------- |
|
| 65 | ||
| 66 | /** |
|
| 67 | * Waits for an optional time period before continuing to process the |
|
| 68 | * exchange |
|
| 69 | */ |
|
| 70 | protected void delay(Exchange exchange) throws Exception { |
|
| 71 | 3 | long time = 0; |
| 72 | 3 | if (timeExpression != null) { |
| 73 | 3 | Long longValue = ExpressionHelper.evaluateAsType(timeExpression, exchange, Long.class); |
| 74 | 3 | if (longValue != null) { |
| 75 | 3 | time = longValue.longValue(); |
| 76 | } |
|
| 77 | } |
|
| 78 | 3 | if (time <= 0) { |
| 79 | 0 | time = defaultProcessTime(exchange); |
| 80 | } |
|
| 81 | ||
| 82 | 3 | time += delay; |
| 83 | ||
| 84 | 3 | waitUntil(time, exchange); |
| 85 | 3 | } |
| 86 | ||
| 87 | /** |
|
| 88 | * A Strategy Method to allow derived implementations to decide the current |
|
| 89 | * system time or some other default exchange property |
|
| 90 | * |
|
| 91 | * @param exchange |
|
| 92 | */ |
|
| 93 | protected long defaultProcessTime(Exchange exchange) { |
|
| 94 | 0 | return currentSystemTime(); |
| 95 | } |
|
| 96 | ||
| 97 | } |