Repository: camel Updated Branches: refs/heads/camel-2.15.x b3b0b4683 -> 04539b689
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/04539b68 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/04539b68 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/04539b68 Branch: refs/heads/camel-2.15.x Commit: 04539b689b78101fc741698fe4753defa372cb95 Parents: b3b0b46 Author: Claus Ibsen <davscl...@apache.org> Authored: Sat Apr 18 08:11:13 2015 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Sat Apr 18 09:23:00 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/04539b68/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 f9f7457..eb560e7 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 @@ -31,7 +31,6 @@ import org.apache.camel.model.language.ExpressionDefinition; import org.apache.camel.processor.Throttler; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.RouteContext; -import org.apache.camel.util.ObjectHelper; /** * Controls the rate at which messages are passed to the next node in the route @@ -109,11 +108,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/04539b68/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/04539b68/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 56fe114..4931db4 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 @@ -274,7 +274,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/04539b68/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/04539b68/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>