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-karaf.git
The following commit(s) were added to refs/heads/master by this push: new 55e02ea CAMEL-15036: Make it easier to switch to use supervising route controller in XML DSL 55e02ea is described below commit 55e02ea4d6aa4f80ac1d747cb444ca71ba1d7333 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Mon May 11 14:05:44 2020 +0200 CAMEL-15036: Make it easier to switch to use supervising route controller in XML DSL --- .../camel/blueprint/CamelContextFactoryBean.java | 12 +++ .../blueprint/SupervisingRouteControllerTest.java | 116 +++++++++++++++++++++ .../BlueprintSupervisingRouteControllerTest.xml | 50 +++++++++ 3 files changed, 178 insertions(+) diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java index bd6fb2e..4ae7856 100644 --- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java +++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java @@ -45,6 +45,7 @@ import org.apache.camel.core.xml.AbstractCamelContextFactoryBean; import org.apache.camel.core.xml.AbstractCamelFactoryBean; import org.apache.camel.core.xml.CamelJMXAgentDefinition; import org.apache.camel.core.xml.CamelPropertyPlaceholderDefinition; +import org.apache.camel.core.xml.CamelRouteControllerDefinition; import org.apache.camel.core.xml.CamelServiceExporterDefinition; import org.apache.camel.core.xml.CamelStreamCachingStrategyDefinition; import org.apache.camel.model.ContextScanDefinition; @@ -166,6 +167,8 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Blu private CamelJMXAgentDefinition camelJMXAgent; @XmlElement(name = "streamCaching", type = CamelStreamCachingStrategyDefinition.class) private CamelStreamCachingStrategyDefinition camelStreamCachingStrategy; + @XmlElement(name = "routeController", type = CamelRouteControllerDefinition.class) + private CamelRouteControllerDefinition camelRouteController; @XmlElements({@XmlElement(name = "template", type = CamelProducerTemplateFactoryBean.class), @XmlElement(name = "fluentTemplate", type = CamelFluentProducerTemplateFactoryBean.class), @XmlElement(name = "consumerTemplate", type = CamelConsumerTemplateFactoryBean.class), @XmlElement(name = "proxy", type = CamelProxyFactoryBean.class), @@ -758,6 +761,15 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Blu } @Override + public CamelRouteControllerDefinition getCamelRouteController() { + return camelRouteController; + } + + public void setCamelRouteController(CamelRouteControllerDefinition camelRouteController) { + this.camelRouteController = camelRouteController; + } + + @Override public List<AbstractCamelFactoryBean<?>> getBeansFactory() { return beansFactory; } diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/SupervisingRouteControllerTest.java b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/SupervisingRouteControllerTest.java new file mode 100644 index 0000000..b00f300 --- /dev/null +++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/SupervisingRouteControllerTest.java @@ -0,0 +1,116 @@ +/* + * 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.test.blueprint; + +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.Consumer; +import org.apache.camel.Endpoint; +import org.apache.camel.Processor; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.component.seda.SedaComponent; +import org.apache.camel.component.seda.SedaConsumer; +import org.apache.camel.component.seda.SedaEndpoint; +import org.apache.camel.spi.SupervisingRouteController; +import org.junit.Test; + +public class SupervisingRouteControllerTest extends CamelBlueprintTestSupport { + + @Override + protected String getBlueprintDescriptor() { + return "org/apache/camel/test/blueprint/BlueprintSupervisingRouteControllerTest.xml"; + } + + @Test + public void testSupervising() throws Exception { + MockEndpoint mock = context.getEndpoint("mock:foo", MockEndpoint.class); + mock.expectedMinimumMessageCount(3); + + MockEndpoint mock2 = context.getEndpoint("mock:cheese", MockEndpoint.class); + mock2.expectedMessageCount(0); + + MockEndpoint mock3 = context.getEndpoint("mock:cake", MockEndpoint.class); + mock3.expectedMessageCount(0); + + MockEndpoint mock4 = context.getEndpoint("mock:bar", MockEndpoint.class); + mock4.expectedMessageCount(0); + + MockEndpoint.assertIsSatisfied(5, TimeUnit.SECONDS, mock, mock2, mock3, mock4); + + assertEquals("Started", context.getRouteController().getRouteStatus("foo").toString()); + // cheese was not able to start + assertEquals("Stopped", context.getRouteController().getRouteStatus("cheese").toString()); + // cake was not able to start + assertEquals("Stopped", context.getRouteController().getRouteStatus("cake").toString()); + + SupervisingRouteController src = context.getRouteController().adapt(SupervisingRouteController.class); + + Throwable e = src.getRestartException("cake"); + assertNotNull(e); + assertEquals("Cannot start", e.getMessage()); + assertTrue(e instanceof IllegalArgumentException); + + // bar is no auto startup + assertEquals("Stopped", context.getRouteController().getRouteStatus("bar").toString()); + } + + public static class MyJmsComponent extends SedaComponent { + + @Override + protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { + return new MyJmsEndpoint(remaining); + } + } + + public static class MyJmsEndpoint extends SedaEndpoint { + + private String name; + + public MyJmsEndpoint(String name) { + super(); + this.name = name; + } + + @Override + public Consumer createConsumer(Processor processor) throws Exception { + return new MyJmsConsumer(this, processor); + } + + @Override + protected String createEndpointUri() { + return "jms:" + name; + } + } + + public static class MyJmsConsumer extends SedaConsumer { + + private int counter; + + public MyJmsConsumer(SedaEndpoint endpoint, Processor processor) { + super(endpoint, processor); + } + + @Override + protected void doStart() throws Exception { + if (counter++ < 5) { + throw new IllegalArgumentException("Cannot start"); + } + } + } + +} diff --git a/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/BlueprintSupervisingRouteControllerTest.xml b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/BlueprintSupervisingRouteControllerTest.xml new file mode 100644 index 0000000..3b3bd29 --- /dev/null +++ b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/BlueprintSupervisingRouteControllerTest.xml @@ -0,0 +1,50 @@ +<?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. + +--> +<!-- START SNIPPET: example --> +<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> + + <bean id="jms" class="org.apache.camel.test.blueprint.SupervisingRouteControllerTest.MyJmsComponent"/> + + <camelContext streamCache="true" xmlns="http://camel.apache.org/schema/blueprint"> + <routeController id="myController" + supervising="true" initialDelay="100" threadPoolSize="2" backOffDelay="25" backOffMaxAttempts="3"/> + <route id="foo"> + <from uri="timer:foo"/> + <to uri="mock:foo"/> + </route> + <route id="cheese"> + <from uri="jms:cheese"/> + <to uri="mock:cheese"/> + </route> + <route id="cake"> + <from uri="jms:cake"/> + <to uri="mock:cake"/> + </route> + <route id="bar" autoStartup="false"> + <from uri="seda:bar"/> + <to uri="mock:bar"/> + </route> + </camelContext> + +</blueprint> +<!-- END SNIPPET: example -->