You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2015/04/18 09:19:35 UTC

[2/2] camel git commit: CAMEL-8665: Throttler EIP - Using method call for message per sec exp fails in spring

CAMEL-8665: Throttler EIP - Using method call for message per sec exp fails in spring


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0b1100f6
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0b1100f6
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0b1100f6

Branch: refs/heads/camel-2.14.x
Commit: 0b1100f6802747c0cf211cc195a2af0a1b65666c
Parents: 48cc7d0
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Apr 18 08:11:13 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Apr 18 09:22:35 2015 +0200

----------------------------------------------------------------------
 .../apache/camel/model/ThrottleDefinition.java  | 10 +--
 .../processor/ThrottlerMethodCallTest.java      | 90 ++++++++++++++++++++
 .../apache/camel/processor/ThrottlerTest.java   |  2 +-
 .../SpringThrottlerMethodCallTest.java          | 30 +++++++
 .../processor/ThrottlerMethodCallTest.xml       | 40 +++++++++
 5 files changed, 165 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/0b1100f6/camel-core/src/main/java/org/apache/camel/model/ThrottleDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/ThrottleDefinition.java b/camel-core/src/main/java/org/apache/camel/model/ThrottleDefinition.java
index 2a14052..aeb395c 100644
--- a/camel-core/src/main/java/org/apache/camel/model/ThrottleDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/ThrottleDefinition.java
@@ -29,7 +29,6 @@ import org.apache.camel.Processor;
 import org.apache.camel.builder.ExpressionBuilder;
 import org.apache.camel.processor.Throttler;
 import org.apache.camel.spi.RouteContext;
-import org.apache.camel.util.ObjectHelper;
 
 /**
  * Represents an XML &lt;throttle/&gt; element
@@ -112,11 +111,10 @@ public class ThrottleDefinition extends ExpressionNode implements ExecutorServic
     }
 
     private Expression createMaxRequestsPerPeriodExpression(RouteContext routeContext) {
-        if (getExpression() != null) {
-            if (ObjectHelper.isNotEmpty(getExpression().getExpression()) || getExpression().getExpressionValue() != null) {
-                return getExpression().createExpression(routeContext);
-            } 
-        } 
+        ExpressionDefinition expr = getExpression();
+        if (expr != null) {
+            return expr.createExpression(routeContext);
+        }
         return null;
     }
     

http://git-wip-us.apache.org/repos/asf/camel/blob/0b1100f6/camel-core/src/test/java/org/apache/camel/processor/ThrottlerMethodCallTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/processor/ThrottlerMethodCallTest.java b/camel-core/src/test/java/org/apache/camel/processor/ThrottlerMethodCallTest.java
new file mode 100644
index 0000000..d4b8a09
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/processor/ThrottlerMethodCallTest.java
@@ -0,0 +1,90 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.processor;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+
+
+/**
+ * @version
+ */
+public class ThrottlerMethodCallTest extends ContextTestSupport {
+    private static final int INTERVAL = 500;
+    protected int messageCount = 9;
+
+    protected boolean canTest() {
+        // skip test on windows as it does not run well there
+        return !isPlatform("windows");
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("myBean", this);
+        return jndi;
+    }
+
+    public long getMessagesPerSecond() {
+        return 1;
+    }
+
+    public void testConfigurationWithMethodCallExpression() throws Exception {
+        if (!canTest()) {
+            return;
+        }
+
+        MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
+        resultEndpoint.expectedMessageCount(messageCount);
+
+        ExecutorService executor = Executors.newFixedThreadPool(messageCount);
+
+        long start = System.currentTimeMillis();
+        for (int i = 0; i < messageCount; i++) {
+            executor.execute(new Runnable() {
+                public void run() {
+                    template.sendBody("direct:expressionMethod", "<message>payload</message>");
+                }
+            });
+        }
+
+        // let's wait for the exchanges to arrive
+        resultEndpoint.assertIsSatisfied();
+
+        // now assert that they have actually been throttled
+        long minimumTime = (messageCount - 1) * INTERVAL;
+        // add a little slack
+        long delta = System.currentTimeMillis() - start + 200;
+        assertTrue("Should take at least " + minimumTime + "ms, was: " + delta, delta >= minimumTime);
+        executor.shutdownNow();
+    }
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:expressionMethod")
+                    .throttle(method("myBean", "getMessagesPerSecond")).timePeriodMillis(INTERVAL)
+                        .to("log:result", "mock:result");
+            }
+        };
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/0b1100f6/camel-core/src/test/java/org/apache/camel/processor/ThrottlerTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/processor/ThrottlerTest.java b/camel-core/src/test/java/org/apache/camel/processor/ThrottlerTest.java
index 31ecd58..3b118c5 100644
--- a/camel-core/src/test/java/org/apache/camel/processor/ThrottlerTest.java
+++ b/camel-core/src/test/java/org/apache/camel/processor/ThrottlerTest.java
@@ -236,7 +236,7 @@ public class ThrottlerTest extends ContextTestSupport {
                 from("direct:expressionConstant").throttle(constant(1)).timePeriodMillis(INTERVAL).to("log:result", "mock:result");
 
                 from("direct:expressionHeader").throttle(header("throttleValue")).timePeriodMillis(INTERVAL).to("log:result", "mock:result");
-                
+
                 from("direct:start").throttle(2).timePeriodMillis(10000).rejectExecution(true).to("log:result", "mock:result");
             }
         };

http://git-wip-us.apache.org/repos/asf/camel/blob/0b1100f6/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringThrottlerMethodCallTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringThrottlerMethodCallTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringThrottlerMethodCallTest.java
new file mode 100644
index 0000000..3cb2ee8
--- /dev/null
+++ b/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringThrottlerMethodCallTest.java
@@ -0,0 +1,30 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.spring.processor;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.processor.ThrottlerMethodCallTest;
+
+import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext;
+
+public class SpringThrottlerMethodCallTest extends ThrottlerMethodCallTest {
+
+    protected CamelContext createCamelContext() throws Exception {
+        return createSpringCamelContext(this,
+                "org/apache/camel/spring/processor/ThrottlerMethodCallTest.xml");
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/0b1100f6/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/ThrottlerMethodCallTest.xml
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/ThrottlerMethodCallTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/ThrottlerMethodCallTest.xml
new file mode 100644
index 0000000..e7ab603
--- /dev/null
+++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/ThrottlerMethodCallTest.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+    ">
+
+  <bean id="myBean" class="org.apache.camel.spring.processor.SpringThrottlerMethodCallTest"/>
+
+  <camelContext xmlns="http://camel.apache.org/schema/spring">
+
+    <route>
+      <from uri="direct:expressionMethod"/>
+      <throttle timePeriodMillis="500">
+        <!-- use a java  bean method call to determine how many messages to throttle per 0.5 sec -->
+        <method ref="myBean" method="getMessagesPerSecond"/>
+        <to uri="mock:result"/>
+      </throttle>
+    </route>
+
+  </camelContext>
+
+</beans>