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 2019/10/17 19:09:27 UTC
[camel] 06/09: CAMEL-14050: Polish Superivisiting route controller
a bit, and fixed a test that was unpredicable.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch route-collector
in repository https://gitbox.apache.org/repos/asf/camel.git
commit b14e58a36a793a18dbe0ee46a6079cd9e6791d3e
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Oct 17 20:26:42 2019 +0200
CAMEL-14050: Polish Superivisiting route controller a bit, and fixed a test that was unpredicable.
---
.../boot/CamelSpringBootApplicationListener.java | 7 +--
.../spring/boot/SpringBootRoutesCollector.java | 70 ++++++++++++++++++++++
.../boot/SupervisingRouteControllerTest.java | 20 +++++--
.../java/org/apache/camel/spi/RouteContext.java | 10 +---
.../apache/camel/main/DefaultRoutesCollector.java | 18 ------
.../org/apache/camel/main/RoutesCollector.java | 2 +-
6 files changed, 90 insertions(+), 37 deletions(-)
diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationListener.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationListener.java
index ed0eca1..107d749 100644
--- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationListener.java
+++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationListener.java
@@ -85,11 +85,8 @@ public class CamelSpringBootApplicationListener implements ApplicationListener<C
&& camelContext.getStatus().isStopped()) {
LOG.debug("Post-processing CamelContext bean: {}", camelContext.getName());
- if (configurationProperties.isRoutesCollectorEnabled()) {
- LOG.debug("RoutesCollectorEnabled: {}", springBootRoutesCollector);
- RoutesConfigurer configurer = new RoutesConfigurer(springBootRoutesCollector);
- configurer.configureRoutes(camelContext, configurationProperties);
- }
+ RoutesConfigurer configurer = new RoutesConfigurer(springBootRoutesCollector);
+ configurer.configureRoutes(camelContext, configurationProperties);
for (CamelContextConfiguration camelContextConfiguration : camelContextConfigurations) {
LOG.debug("CamelContextConfiguration found. Invoking beforeApplicationStart: {}", camelContextConfiguration);
diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootRoutesCollector.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootRoutesCollector.java
index d6b446f..335a46c 100644
--- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootRoutesCollector.java
+++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringBootRoutesCollector.java
@@ -17,15 +17,19 @@
package org.apache.camel.spring.boot;
import java.io.FileNotFoundException;
+import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import org.apache.camel.CamelContext;
+import org.apache.camel.RoutesBuilder;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.main.DefaultRoutesCollector;
import org.apache.camel.model.ModelHelper;
import org.apache.camel.model.RoutesDefinition;
import org.apache.camel.model.rest.RestsDefinition;
+import org.apache.camel.util.AntPathMatcher;
+import org.apache.camel.util.ObjectHelper;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;
@@ -41,6 +45,72 @@ public class SpringBootRoutesCollector extends DefaultRoutesCollector {
}
@Override
+ public List<RoutesBuilder> collectRoutesFromRegistry(CamelContext camelContext, String excludePattern, String includePattern) {
+ final List<RoutesBuilder> routes = new ArrayList<>();
+
+ final AntPathMatcher matcher = new AntPathMatcher();
+ for (RoutesBuilder routesBuilder : applicationContext.getBeansOfType(RoutesBuilder.class, true, true).values()) {
+ // filter out abstract classes
+ boolean abs = Modifier.isAbstract(routesBuilder.getClass().getModifiers());
+ if (!abs) {
+ String name = routesBuilder.getClass().getName();
+ // make name as path so we can use ant path matcher
+ name = name.replace('.', '/');
+
+ boolean match = !"false".equals(includePattern);
+ // exclude take precedence over include
+ if (match && ObjectHelper.isNotEmpty(excludePattern)) {
+ // there may be multiple separated by comma
+ String[] parts = excludePattern.split(",");
+ for (String part : parts) {
+ // must negate when excluding, and hence !
+ match = !matcher.match(part, name);
+ log.trace("Java RoutesBuilder: {} exclude filter: {} -> {}", name, part, match);
+ if (!match) {
+ break;
+ }
+ }
+ }
+ // special support for testing with @ExcludeRoutes annotation with camel-test-spring
+ excludePattern = System.getProperty("CamelTestSpringExcludeRoutes");
+ // exclude take precedence over include
+ if (match && ObjectHelper.isNotEmpty(excludePattern)) {
+ // this property is a comma separated list of FQN class names, so we need to make
+ // name as path so we can use ant patch matcher
+ excludePattern = excludePattern.replace('.', '/');
+ // there may be multiple separated by comma
+ String[] parts = excludePattern.split(",");
+ for (String part : parts) {
+ // must negate when excluding, and hence !
+ match = !matcher.match(part, name);
+ log.trace("Java RoutesBuilder: {} exclude filter: {} -> {}", name, part, match);
+ if (!match) {
+ break;
+ }
+ }
+ }
+ if (match && ObjectHelper.isNotEmpty(includePattern)) {
+ // there may be multiple separated by comma
+ String[] parts = includePattern.split(",");
+ for (String part : parts) {
+ match = matcher.match(part, name);
+ log.trace("Java RoutesBuilder: {} include filter: {} -> {}", name, part, match);
+ if (match) {
+ break;
+ }
+ }
+ }
+ log.debug("Java RoutesBuilder: {} accepted by include/exclude filter: {}", name, match);
+ if (match) {
+ routes.add(routesBuilder);
+ }
+ }
+ }
+
+ return routes;
+ }
+
+ @Override
public List<RoutesDefinition> collectXmlRoutesFromDirectory(CamelContext camelContext, String directory) {
List<RoutesDefinition> answer = new ArrayList<>();
diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SupervisingRouteControllerTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SupervisingRouteControllerTest.java
index 3b2b640..7d9c495 100644
--- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SupervisingRouteControllerTest.java
+++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SupervisingRouteControllerTest.java
@@ -16,6 +16,8 @@
*/
package org.apache.camel.spring.boot;
+import java.util.concurrent.TimeUnit;
+
import org.apache.camel.CamelContext;
import org.apache.camel.ServiceStatus;
import org.apache.camel.builder.RouteBuilder;
@@ -32,6 +34,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
+import static org.awaitility.Awaitility.await;
+
@DirtiesContext
@RunWith(SpringRunner.class)
@SpringBootTest(
@@ -42,6 +46,7 @@ import org.springframework.test.context.junit4.SpringRunner;
},
properties = {
"camel.springboot.xml-routes = false",
+ "camel.springboot.xml-rests = false",
"camel.springboot.main-run-controller = true",
"camel.supervising.controller.enabled = true",
"camel.supervising.controller.initial-delay = 2s",
@@ -78,13 +83,18 @@ public class SupervisingRouteControllerTest {
Assert.assertEquals(Long.MAX_VALUE, bar.getMaxDelay().toMillis());
Assert.assertEquals(3L, bar.getMaxAttempts().longValue());
- Assert.assertEquals(controller, context.getRoute("foo").getRouteContext().getRouteController());
- Assert.assertEquals(controller, context.getRoute("bar").getRouteContext().getRouteController());
- Assert.assertNull(context.getRoute("timer-unmanaged").getRouteContext().getRouteController());
- Assert.assertNull(context.getRoute("timer-no-autostartup").getRouteContext().getRouteController());
-
Assert.assertEquals(ServiceStatus.Stopped, context.getRouteController().getRouteStatus("foo"));
Assert.assertEquals(ServiceStatus.Stopped, context.getRouteController().getRouteStatus("bar"));
+ Assert.assertEquals(ServiceStatus.Stopped, context.getRouteController().getRouteStatus("timer-no-autostartup"));
+ Assert.assertEquals(ServiceStatus.Stopped, context.getRouteController().getRouteStatus("jetty"));
+
+ // Wait for the controller to start the routes also unmanaged
+ await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> {
+ Assert.assertEquals(ServiceStatus.Started, context.getRouteController().getRouteStatus("jetty"));
+ Assert.assertEquals(ServiceStatus.Started, context.getRouteController().getRouteStatus("timer-unmanaged"));
+ });
+ Assert.assertEquals(ServiceStatus.Started, context.getRouteController().getRouteStatus("jetty"));
+ Assert.assertEquals(ServiceStatus.Started, context.getRouteController().getRouteStatus("timer-unmanaged"));
}
// *************************************
diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/RouteContext.java b/core/camel-api/src/main/java/org/apache/camel/spi/RouteContext.java
index feb117f..7c5b337 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/RouteContext.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/RouteContext.java
@@ -28,7 +28,6 @@ import org.apache.camel.NamedNode;
import org.apache.camel.Processor;
import org.apache.camel.Route;
import org.apache.camel.RuntimeConfiguration;
-import org.apache.camel.meta.Experimental;
/**
* The context used to activate new routing rules
@@ -234,19 +233,14 @@ public interface RouteContext extends RuntimeConfiguration, EndpointAware {
*
* @return the route controller,
*/
- @Experimental
- default RouteController getRouteController() {
- return null;
- }
+ RouteController getRouteController();
/**
* Sets the {@link RouteController} for this route.
*
* @param controller the RouteController
*/
- @Experimental
- default void setRouteController(RouteController controller) {
- }
+ void setRouteController(RouteController controller);
Processor getOnCompletion(String onCompletionId);
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/DefaultRoutesCollector.java b/core/camel-main/src/main/java/org/apache/camel/main/DefaultRoutesCollector.java
index 2b64ff9..3bd10a3 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/DefaultRoutesCollector.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/DefaultRoutesCollector.java
@@ -71,24 +71,6 @@ public class DefaultRoutesCollector implements RoutesCollector {
}
}
}
- // special support for testing with @ExcludeRoutes annotation with camel-test-spring
- excludePattern = System.getProperty("CamelTestSpringExcludeRoutes");
- // exclude take precedence over include
- if (match && ObjectHelper.isNotEmpty(excludePattern)) {
- // this property is a comma separated list of FQN class names, so we need to make
- // name as path so we can use ant patch matcher
- excludePattern = excludePattern.replace('.', '/');
- // there may be multiple separated by comma
- String[] parts = excludePattern.split(",");
- for (String part : parts) {
- // must negate when excluding, and hence !
- match = !matcher.match(part, name);
- log.trace("Java RoutesBuilder: {} exclude filter: {} -> {}", name, part, match);
- if (!match) {
- break;
- }
- }
- }
if (match && ObjectHelper.isNotEmpty(includePattern)) {
// there may be multiple separated by comma
String[] parts = includePattern.split(",");
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/RoutesCollector.java b/core/camel-main/src/main/java/org/apache/camel/main/RoutesCollector.java
index b8e9648..8d8b2fc 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/RoutesCollector.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/RoutesCollector.java
@@ -25,7 +25,7 @@ import org.apache.camel.model.rest.RestsDefinition;
/**
* Collects routes and rests from the various sources (like registry or opinionated
- * classpath locations) and injects these into the Camel context.
+ * classpath locations) and adds these into the Camel context.
*/
public interface RoutesCollector {