This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 54156c9 CAMEL-12253: Add restart action to controlbus 54156c9 is described below commit 54156c914adcd564555669096dbfce7ab6103ea1 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sat Feb 10 10:17:33 2018 +0100 CAMEL-12253: Add restart action to controlbus --- camel-core/src/main/docs/controlbus-component.adoc | 7 +- .../component/controlbus/ControlBusEndpoint.java | 17 ++++- .../component/controlbus/ControlBusProducer.java | 10 +++ .../controlbus/ControlBusRestartRouteTest.java | 76 ++++++++++++++++++++++ 4 files changed, 105 insertions(+), 5 deletions(-) diff --git a/camel-core/src/main/docs/controlbus-component.adoc b/camel-core/src/main/docs/controlbus-component.adoc index 221aab4..a7ebf97 100644 --- a/camel-core/src/main/docs/controlbus-component.adoc +++ b/camel-core/src/main/docs/controlbus-component.adoc @@ -85,14 +85,15 @@ with the following path and query parameters: | *language* | Allows you to specify the name of a Language to use for evaluating the message body. If there is any result from the evaluation then the result is put in the message body. | | Language |=== -==== Query Parameters (5 parameters): +==== Query Parameters (6 parameters): [width="100%",cols="2,5,^1,2",options="header"] |=== | Name | Description | Default | Type -| *action* (producer) | To denote an action that can be either: start stop or status. To either start or stop a route or to get the status of the route as output in the message body. You can use suspend and resume from Camel 2.11.1 onwards to either suspend or resume a route. And from Camel 2.11.1 onwards you can use stats to get performance statics returned in XML format; the routeId option can be used to define which route to get the performance stats for if routeId is not defined then [...] +| *action* (producer) | To denote an action that can be either: start stop or status. To either start or stop a route or to get the status of the route as output in the message body. You can use suspend and resume from Camel 2.11.1 onwards to either suspend or resume a route. And from Camel 2.11.1 onwards you can use stats to get performance statics returned in XML format; the routeId option can be used to define which route to get the performance stats for if routeId is not defined then [...] | *async* (producer) | Whether to execute the control bus task asynchronously. Important: If this option is enabled then any result from the task is not set on the Exchange. This is only possible if executing tasks synchronously. | false | boolean | *loggingLevel* (producer) | Logging level used for logging when task is done or if any exceptions occurred during processing the task. | INFO | LoggingLevel +| *restartDelay* (producer) | The delay in millis to use when restarting a route. | 1000 | int | *routeId* (producer) | To specify a route by its id. The special keyword current indicates the current route. | | String | *synchronous* (advanced) | Sets whether synchronous processing should be strictly used or Camel is allowed to use asynchronous processing (if supported). | false | boolean |=== @@ -185,4 +186,4 @@ We use `async=true` to stop Camel asynchronously as otherwise we would be trying to stop Camel while it was in-flight processing the message we sent to the control bus component. -TIP: You can also use other languages such as <<groovy-language,Groovy>>, etc. \ No newline at end of file +TIP: You can also use other languages such as <<groovy-language,Groovy>>, etc. diff --git a/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusEndpoint.java b/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusEndpoint.java index bedc299..20eb4a2 100644 --- a/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusEndpoint.java +++ b/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusEndpoint.java @@ -44,8 +44,10 @@ public class ControlBusEndpoint extends DefaultEndpoint { private Language language; @UriParam private String routeId; - @UriParam(enums = "start,stop,suspend,resume,status") + @UriParam(enums = "start,stop,suspend,resume,restart,status,stats") private String action; + @UriParam(defaultValue = "1000") + private int restartDelay = 1000; @UriParam private boolean async; @UriParam(defaultValue = "INFO") @@ -112,12 +114,23 @@ public class ControlBusEndpoint extends DefaultEndpoint { * You can use suspend and resume from Camel 2.11.1 onwards to either suspend or resume a route. * And from Camel 2.11.1 onwards you can use stats to get performance statics returned in XML format; * the routeId option can be used to define which route to get the performance stats for, if routeId is not defined, - * then you get statistics for the entire CamelContext. + * then you get statistics for the entire CamelContext. The restart action will restart the route. */ public void setAction(String action) { this.action = action; } + public int getRestartDelay() { + return restartDelay; + } + + /** + * The delay in millis to use when restarting a route. + */ + public void setRestartDelay(int restartDelay) { + this.restartDelay = restartDelay; + } + public boolean isAsync() { return async; } diff --git a/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java b/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java index f2d1574..c3d5bf5 100644 --- a/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java +++ b/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java @@ -163,6 +163,16 @@ public class ControlBusProducer extends DefaultAsyncProducer { getEndpoint().getCamelContext().suspendRoute(id); } else if ("resume".equals(action)) { getEndpoint().getCamelContext().resumeRoute(id); + } else if ("restart".equals(action)) { + getEndpoint().getCamelContext().stopRoute(id); + int delay = getEndpoint().getRestartDelay(); + try { + log.debug("Sleeping {} ms before starting route"); + Thread.sleep(delay); + } catch (InterruptedException e) { + // ignore + } + getEndpoint().getCamelContext().startRoute(id); } else if ("status".equals(action)) { ServiceStatus status = getEndpoint().getCamelContext().getRouteStatus(id); if (status != null) { diff --git a/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusRestartRouteTest.java b/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusRestartRouteTest.java new file mode 100644 index 0000000..20c29a3 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusRestartRouteTest.java @@ -0,0 +1,76 @@ +/** + * 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.controlbus; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Route; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.support.RoutePolicySupport; + +public class ControlBusRestartRouteTest extends ContextTestSupport { + + private MyRoutePolicy myRoutePolicy = new MyRoutePolicy(); + + public void testControlBusRestart() throws Exception { + assertEquals(1, myRoutePolicy.getStart()); + assertEquals(0, myRoutePolicy.getStop()); + + assertEquals("Started", context.getRouteStatus("foo").name()); + + template.sendBody("controlbus:route?routeId=foo&action=restart", null); + + assertEquals("Started", context.getRouteStatus("foo").name()); + + assertEquals(2, myRoutePolicy.getStart()); + assertEquals(1, myRoutePolicy.getStop()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("seda:foo").routeId("foo").routePolicy(myRoutePolicy) + .to("mock:foo"); + } + }; + } + + private final class MyRoutePolicy extends RoutePolicySupport { + + private int start; + private int stop; + + @Override + public void onStart(Route route) { + start++; + } + + @Override + public void onStop(Route route) { + stop++; + } + + public int getStart() { + return start; + } + + public int getStop() { + return stop; + } + } +} -- To stop receiving notification emails like this one, please contact davscl...@apache.org.