camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r781713 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/model/ camel-core/src/main/java/org/apache/camel/processor/ camel-core/src/test/java/org/apache/camel/processor/ components/camel-spring/src/test/java/org/apache/camel/spr...
Date Thu, 04 Jun 2009 11:37:27 GMT
Author: davsclaus
Date: Thu Jun  4 11:37:26 2009
New Revision: 781713

URL: http://svn.apache.org/viewvc?rev=781713&view=rev
Log:
CAMEL-1668: Using the delay is now easier especially in the Spring DSL.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/DelayDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Delayer.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DelayerTest.java
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDelayerTest.java
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringWireTapTest.xml
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteNoWaitTest.xml
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteNoWaitWithErrorTest.xml
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteTest.xml
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteWaitIfReplyExpectedTest.xml
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteWithErrorTest.xml
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/delayer.xml

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/DelayDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/DelayDefinition.java?rev=781713&r1=781712&r2=781713&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/DelayDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/DelayDefinition.java Thu Jun
 4 11:37:26 2009
@@ -18,12 +18,11 @@
 
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
-import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlRootElement;
 
 import org.apache.camel.Expression;
 import org.apache.camel.Processor;
-import org.apache.camel.builder.ExpressionClause;
+import org.apache.camel.builder.ExpressionBuilder;
 import org.apache.camel.model.language.ExpressionDefinition;
 import org.apache.camel.processor.Delayer;
 import org.apache.camel.spi.RouteContext;
@@ -37,77 +36,54 @@
 @XmlRootElement(name = "delay")
 @XmlAccessorType(XmlAccessType.FIELD)
 public class DelayDefinition extends ExpressionNode {
-    @XmlAttribute
-    private Long delayTime = 0L;
 
     public DelayDefinition() {
     }
 
-    public DelayDefinition(Expression processAtExpression) {
-        super(processAtExpression);
+    public DelayDefinition(Expression delay) {
+        super(delay);
     }
 
-    public DelayDefinition(ExpressionDefinition processAtExpression) {
-        super(processAtExpression);
+    @Override
+    public String getLabel() {
+        return "delay";
     }
 
-    public DelayDefinition(Expression processAtExpression, long delayTime) {
-        super(processAtExpression);
-        this.delayTime = delayTime;
+    @Override
+    public String getShortName() {
+        return "delay";
     }
 
     @Override
     public String toString() {
-        return "Delay[on: " + getExpression() + " delay: " + delayTime + " -> " + getOutputs()
+ "]";
+        return "Delay[" + getExpression() + " -> " + getOutputs() + "]";
     }
-    
+
     // Fluent API
     // -------------------------------------------------------------------------
 
     /**
      * Sets the delay time in millis to delay
+     *
      * @param delay delay time in millis
      * @return the builder
      */
     public DelayDefinition delayTime(Long delay) {
-        setDelayTime(delay);
+        setExpression(new ExpressionDefinition(ExpressionBuilder.constantExpression(delay)));
         return this;
     }
     
-    /**
-     * Set the expression that the delayer will use
-     * @return the builder
-     */
-    public ExpressionClause<DelayDefinition> expression() {
-        return ExpressionClause.createAndSetExpression(this);
-    }
-
-    @Override
-    public String getShortName() {
-        return "delay";
-    }
-
-    public Long getDelayTime() {
-        return delayTime;
-    }
-
-    public void setDelayTime(Long delayTime) {
-        this.delayTime = delayTime;
-    }   
-    
-
     @Override
     public Processor createProcessor(RouteContext routeContext) throws Exception {
         Processor childProcessor = routeContext.createProcessor(this);
-        Expression processAtExpression = createAbsoluteTimeDelayExpression(routeContext);
-        return new Delayer(childProcessor, processAtExpression, delayTime);
+        Expression delay = createAbsoluteTimeDelayExpression(routeContext);
+        return new Delayer(childProcessor, delay);
     }
 
     private Expression createAbsoluteTimeDelayExpression(RouteContext routeContext) {
         ExpressionDefinition expr = getExpression();
         if (expr != null) {
-            if (ObjectHelper.isNotEmpty(expr.getExpression())
-                || expr.getExpressionValue() != null) {
+            if (ObjectHelper.isNotEmpty(expr.getExpression()) || expr.getExpressionValue()
!= null) {
                 return expr.createExpression(routeContext);
             } 
         } 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java?rev=781713&r1=781712&r2=781713&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java Thu
Jun  4 11:37:26 2009
@@ -39,6 +39,7 @@
 import org.apache.camel.builder.DataFormatClause;
 import org.apache.camel.builder.ErrorHandlerBuilder;
 import org.apache.camel.builder.ErrorHandlerBuilderRef;
+import org.apache.camel.builder.ExpressionBuilder;
 import org.apache.camel.builder.ExpressionClause;
 import org.apache.camel.builder.ProcessorBuilder;
 import org.apache.camel.model.dataformat.DataFormatDefinition;
@@ -1111,25 +1112,11 @@
      * <a href="http://camel.apache.org/delayer.html">Delayer EIP:</a>
      * Creates a delayer allowing you to delay the delivery of messages to some destination.
      *
-     * @param processAtExpression  an expression to calculate the time at which the messages
should be processed,
-     *                             should be convertable to long as time in millis
+     * @param delay  an expression to calculate the delay time in millis
      * @return the builder
      */
-    public DelayDefinition delay(Expression processAtExpression) {
-        return delay(processAtExpression, 0L);
-    }
-
-    /**
-     * <a href="http://camel.apache.org/delayer.html">Delayer EIP:</a>
-     * Creates a delayer allowing you to delay the delivery of messages to some destination.
-     *
-     * @param processAtExpression  an expression to calculate the time at which the messages
should be processed,
-     *                             should be convertable to long as time in millis
-     * @param delay                the delay in milliseconds which is added to the processAtExpression
-     * @return the builder
-     */
-    public DelayDefinition delay(Expression processAtExpression, long delay) {
-        DelayDefinition answer = new DelayDefinition(processAtExpression, delay);
+    public DelayDefinition delay(Expression delay) {
+        DelayDefinition answer = new DelayDefinition(delay);
         addOutput(answer);
         return answer;
     }
@@ -1150,11 +1137,11 @@
      * <a href="http://camel.apache.org/delayer.html">Delayer EIP:</a>
      * Creates a delayer allowing you to delay the delivery of messages to some destination.
      *
-     * @param delay  the default delay in millis
+     * @param delay  the delay in millis
      * @return the builder
      */
     public DelayDefinition delay(long delay) {
-        return delay(null, delay);
+        return delay(ExpressionBuilder.constantExpression(delay));
     }
 
     /**

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Delayer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Delayer.java?rev=781713&r1=781712&r2=781713&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Delayer.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Delayer.java Thu Jun 
4 11:37:26 2009
@@ -23,40 +23,21 @@
 /**
  * A <a href="http://camel.apache.org/delayer.html">Delayer</a> which
  * delays processing the exchange until the correct amount of time has elapsed
- * using an expression to determine the delivery time. <p/> For example if you
- * wish to delay JMS messages by 25 seconds from their publish time you could
- * create an instance of this class with the expression
- * <code>header("JMSTimestamp")</code> and a delay value of 25000L.
+ * using an expression to determine the delivery time.
  * 
  * @version $Revision$
  */
 public class Delayer extends DelayProcessorSupport {
-    private final Expression timeExpression;
-    private long delay;
+    private final Expression delay;
 
-    public Delayer(Processor processor, Expression timeExpression, long delay) {
+    public Delayer(Processor processor, Expression delay) {
         super(processor);
-        this.timeExpression = timeExpression;
         this.delay = delay;
     }
 
     @Override
     public String toString() {
-        return "Delayer[" + (timeExpression != null ? "on: " + timeExpression : "") + " delay:
" + delay + " to: " + getProcessor() + "]";
-    }
-
-    // Properties
-    // -------------------------------------------------------------------------
-    public long getDelay() {
-        return delay;
-    }
-
-    /**
-     * Sets the delay from the publish time; which is typically the time from
-     * the expression or the current system time if none is available
-     */
-    public void setDelay(long delay) {
-        this.delay = delay;
+        return "Delayer[" + delay + " to: " + getProcessor() + "]";
     }
 
     // Implementation methods
@@ -68,17 +49,19 @@
      */
     protected void delay(Exchange exchange) throws Exception {
         long time = 0;
-        if (timeExpression != null) {
-            Long longValue = timeExpression.evaluate(exchange, Long.class);
+        if (delay != null) {
+            Long longValue = delay.evaluate(exchange, Long.class);
             if (longValue != null) {
                 time = longValue;
             }
         }
         if (time <= 0) {
-            time = defaultProcessTime(exchange);
+            // no delay
+            return;
         }
 
-        time += delay;
+        // now add the current time
+        time += defaultProcessTime(exchange);
 
         waitUntil(time, exchange);
     }
@@ -91,4 +74,5 @@
         return currentSystemTime();
     }
 
+
 }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DelayerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DelayerTest.java?rev=781713&r1=781712&r2=781713&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DelayerTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DelayerTest.java Thu Jun
 4 11:37:26 2009
@@ -27,20 +27,26 @@
 
     public void testSendingMessageGetsDelayed() throws Exception {
         MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
-        resultEndpoint.expectedMessageCount(0);
 
-        template.sendBodyAndHeader("seda:a", "<hello>world!</hello>", "JMSTimestamp",
System.currentTimeMillis());
+        // do not wait for the first message
+        resultEndpoint.expectedMessageCount(0);
+        resultEndpoint.setResultWaitTime(500);
+        template.sendBodyAndHeader("seda:a", "<hello>world!</hello>", "MyDelay",
1000);
+        // we should not receive it as we wait at most 0.5 sec and it take 1 sec to send
         resultEndpoint.assertIsSatisfied();
 
         // now if we wait a bit longer we should receive the message!
+        resultEndpoint.reset();
         resultEndpoint.expectedMessageCount(1);
         resultEndpoint.assertIsSatisfied();
+    }
 
-        template.sendBody("seda:b", "<hello>world!</hello>");
-        resultEndpoint.assertIsSatisfied();
-
-        // now if we wait a bit longer we should receive the message!
+    public void testDelayConstant() throws Exception {
+        MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
         resultEndpoint.expectedMessageCount(1);
+        // should at least take 1 sec to complete
+        resultEndpoint.setMinimumResultWaitTime(1000);
+        template.sendBody("seda:b", "<hello>world!</hello>");
         resultEndpoint.assertIsSatisfied();
     }
 
@@ -48,10 +54,11 @@
         return new RouteBuilder() {
             public void configure() {
                 // START SNIPPET: ex
-                from("seda:a").delay(header("JMSTimestamp"), 3000).to("mock:result");
+                from("seda:a").delay().header("MyDelay").to("mock:result");
                 // END SNIPPET: ex
+
                 // START SNIPPET: ex2
-                from("seda:b").delay(3000).to("mock:result");
+                from("seda:b").delay(1000).to("mock:result");
                 // END SNIPPET: ex2
             }
         };

Modified: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDelayerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDelayerTest.java?rev=781713&r1=781712&r2=781713&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDelayerTest.java
(original)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDelayerTest.java
Thu Jun  4 11:37:26 2009
@@ -21,8 +21,8 @@
 import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext;
 
 public class SpringDelayerTest extends DelayerTest {
+
     protected CamelContext createCamelContext() throws Exception {
-        return createSpringCamelContext(this,
-                "org/apache/camel/spring/processor/delayer.xml");
+        return createSpringCamelContext(this, "org/apache/camel/spring/processor/delayer.xml");
     }
 }

Modified: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringWireTapTest.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringWireTapTest.xml?rev=781713&r1=781712&r2=781713&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringWireTapTest.xml
(original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringWireTapTest.xml
Thu Jun  4 11:37:26 2009
@@ -35,7 +35,7 @@
 
         <route>
             <from uri="direct:tap"/>
-            <delay delayTime="1000"><constant/></delay>
+            <delay><constant>1000</constant></delay>
             <setBody><constant>Tapped</constant></setBody>
             <to uri="mock:result"/>
             <to uri="mock:tap"/>

Modified: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteNoWaitTest.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteNoWaitTest.xml?rev=781713&r1=781712&r2=781713&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteNoWaitTest.xml
(original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteNoWaitTest.xml
Thu Jun  4 11:37:26 2009
@@ -32,9 +32,7 @@
 
             <threads waitForTaskToComplete="Newer">
                 <to uri="mock:foo"/>
-                <delay delayTime="500">
-                    <constant/>
-                </delay>
+                <delay><constant>500</constant></delay>
                 <process ref="myProcessor"/>
                 <to uri="mock:result"/>
             </threads>

Modified: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteNoWaitWithErrorTest.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteNoWaitWithErrorTest.xml?rev=781713&r1=781712&r2=781713&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteNoWaitWithErrorTest.xml
(original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteNoWaitWithErrorTest.xml
Thu Jun  4 11:37:26 2009
@@ -32,9 +32,7 @@
 
             <threads waitForTaskToComplete="Newer">
                 <to uri="mock:foo"/>
-                <delay delayTime="500">
-                    <constant/>
-                </delay>
+                <delay><constant>500</constant></delay>
                 <process ref="myProcessor"/>
                 <to uri="mock:result"/>
             </threads>

Modified: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteTest.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteTest.xml?rev=781713&r1=781712&r2=781713&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteTest.xml
(original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteTest.xml
Thu Jun  4 11:37:26 2009
@@ -32,9 +32,7 @@
 
             <threads>
                 <to uri="mock:foo"/>
-                <delay delayTime="500">
-                    <constant/>
-                </delay>
+                <delay><constant>500</constant></delay>
                 <process ref="myProcessor"/>
                 <to uri="mock:result"/>
             </threads>

Modified: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteWaitIfReplyExpectedTest.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteWaitIfReplyExpectedTest.xml?rev=781713&r1=781712&r2=781713&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteWaitIfReplyExpectedTest.xml
(original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteWaitIfReplyExpectedTest.xml
Thu Jun  4 11:37:26 2009
@@ -33,9 +33,7 @@
             <threads waitForTaskToComplete="IfReplyExpected">
                 <to uri="mock:foo"/>
                 <!-- wait 0.5 sec -->
-                <delay delayTime="500">
-                    <constant/>
-                </delay>
+                <delay><constant>500</constant></delay>
                 <process ref="myProcessor"/>
                 <to uri="mock:result"/>
             </threads>

Modified: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteWithErrorTest.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteWithErrorTest.xml?rev=781713&r1=781712&r2=781713&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteWithErrorTest.xml
(original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteWithErrorTest.xml
Thu Jun  4 11:37:26 2009
@@ -32,9 +32,7 @@
 
             <threads>
                 <to uri="mock:foo"/>
-                <delay delayTime="500">
-                    <constant/>
-                </delay>
+                <delay><constant>500</constant></delay>
                 <process ref="myProcessor"/>
                 <to uri="mock:result"/>
             </threads>

Modified: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/delayer.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/delayer.xml?rev=781713&r1=781712&r2=781713&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/delayer.xml
(original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/delayer.xml
Thu Jun  4 11:37:26 2009
@@ -21,23 +21,20 @@
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
     ">
-    <!--
-        from("seda:a").delayer(header("JMSTimestamp"), 3000).to("mock:result");
-        from("seda:b").delayer(3000).to("mock:result");
-    -->
+
     <!-- START SNIPPET: example -->
     <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
         <route>
             <from uri="seda:a"/>
-            <delay delayTime="3000">
-                <simple>header.JMSTimestamp</simple>
+            <delay>
+                <header>MyDelay</header>
             </delay>
             <to uri="mock:result"/>
         </route>
         <route>
             <from uri="seda:b"/>
-            <delay delayTime="3000">
-                <language/>
+            <delay>
+                <constant>1000</constant>
             </delay>
             <to uri="mock:result"/>
         </route>



Mime
View raw message