Repository: camel Updated Branches: refs/heads/camel-2.18.x 7ac3b362f -> 121a83461 refs/heads/camel-2.19.x 480239b54 -> 1d6bde9de refs/heads/master ab9a26276 -> 633b659cc
CAMEL-11355: Timer consumer should be suspendable so it can shutdown graceful in a better way. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/633b659c Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/633b659c Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/633b659c Branch: refs/heads/master Commit: 633b659cc116a2251517387241d132d9ad147268 Parents: ab9a262 Author: Claus Ibsen <[email protected]> Authored: Mon May 29 13:43:41 2017 +0200 Committer: Claus Ibsen <[email protected]> Committed: Mon May 29 13:43:41 2017 +0200 ---------------------------------------------------------------------- .../camel/component/timer/TimerConsumer.java | 3 +- .../timer/TimerGracefulShutdownTest.java | 90 ++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/633b659c/camel-core/src/main/java/org/apache/camel/component/timer/TimerConsumer.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/timer/TimerConsumer.java b/camel-core/src/main/java/org/apache/camel/component/timer/TimerConsumer.java index eedcd18..8908cfa 100644 --- a/camel-core/src/main/java/org/apache/camel/component/timer/TimerConsumer.java +++ b/camel-core/src/main/java/org/apache/camel/component/timer/TimerConsumer.java @@ -27,6 +27,7 @@ import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.StartupListener; +import org.apache.camel.Suspendable; import org.apache.camel.impl.DefaultConsumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,7 +37,7 @@ import org.slf4j.LoggerFactory; * * @version */ -public class TimerConsumer extends DefaultConsumer implements StartupListener { +public class TimerConsumer extends DefaultConsumer implements StartupListener, Suspendable { private static final Logger LOG = LoggerFactory.getLogger(TimerConsumer.class); private final TimerEndpoint endpoint; private volatile TimerTask task; http://git-wip-us.apache.org/repos/asf/camel/blob/633b659c/camel-core/src/test/java/org/apache/camel/component/timer/TimerGracefulShutdownTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/timer/TimerGracefulShutdownTest.java b/camel-core/src/test/java/org/apache/camel/component/timer/TimerGracefulShutdownTest.java new file mode 100644 index 0000000..825d8f4 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/timer/TimerGracefulShutdownTest.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.component.timer; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.impl.JndiRegistry; +import org.apache.camel.spi.ExceptionHandler; + +/** + * @version + */ +public class TimerGracefulShutdownTest extends ContextTestSupport { + + private MyExceptionHandler eh = new MyExceptionHandler(); + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + jndi.bind("eh", eh); + return jndi; + } + + public void testTimerShutdown() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMinimumMessageCount(1); + + assertMockEndpointsSatisfied(); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + + assertFalse("Should not throw exception during graceful shutdown", eh.isError()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("timer:foo?period=100&exceptionHandler=#eh") + .delay(200) + .to("log:time") + .to("mock:result"); + } + }; + } + + private static final class MyExceptionHandler implements ExceptionHandler { + + private volatile boolean error; + + @Override + public void handleException(Throwable exception) { + error = true; + } + + @Override + public void handleException(String message, Throwable exception) { + error = true; + } + + @Override + public void handleException(String message, Exchange exchange, Throwable exception) { + error = true; + } + + public boolean isError() { + return error; + } + } +}
