Repository: camel Updated Branches: refs/heads/feature/CAMEL-11335-routes-endpoint [created] 2a05c3b74
CAMEL-11335: Add Actuator endpoint for exposing Camel routes Signed-off-by: Gregor Zurowski <gre...@zurowski.org> Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/2a05c3b7 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/2a05c3b7 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/2a05c3b7 Branch: refs/heads/feature/CAMEL-11335-routes-endpoint Commit: 2a05c3b74937ca734eedbad592cf61fe828180d1 Parents: d900401 Author: Gregor Zurowski <gre...@zurowski.org> Authored: Fri May 26 09:18:28 2017 +0200 Committer: Gregor Zurowski <gre...@zurowski.org> Committed: Fri May 26 09:18:28 2017 +0200 ---------------------------------------------------------------------- .../actuate/endpoint/CamelRoutesEndpoint.java | 107 +++++++++++++++++++ .../CamelRoutesEndpointAutoConfiguration.java | 47 ++++++++ .../main/resources/META-INF/spring.factories | 1 + .../actuate/endpoint/ActuatorTestRoute.java | 30 ++++++ .../endpoint/CamelRoutesEndpointTest.java | 57 ++++++++++ 5 files changed, 242 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/2a05c3b7/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpoint.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpoint.java new file mode 100644 index 0000000..a110149 --- /dev/null +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpoint.java @@ -0,0 +1,107 @@ +/** + * 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.boot.actuate.endpoint; + +import java.util.List; +import java.util.stream.Collectors; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +import org.apache.camel.CamelContext; +import org.apache.camel.Route; +import org.apache.camel.spring.boot.actuate.endpoint.CamelRoutesEndpoint.RouteEndpointInfo; +import org.springframework.boot.actuate.endpoint.Endpoint; + +/** + * {@link Endpoint} to expose {@link org.apache.camel.Route} information. + */ +public class CamelRoutesEndpoint implements Endpoint<List<RouteEndpointInfo>> { + + private static final String ENDPONT_ID = "camelroutes"; + + private CamelContext camelContext; + + public CamelRoutesEndpoint(CamelContext camelContext) { + this.camelContext = camelContext; + } + + @Override + public String getId() { + return ENDPONT_ID; + } + + @Override + public boolean isEnabled() { + return true; + } + + @Override + public boolean isSensitive() { + return true; + } + + @Override + public List<RouteEndpointInfo> invoke() { + // @formatter:off + return camelContext.getRoutes().stream() + .map(r -> new RouteEndpointInfo(r)) + .collect(Collectors.toList()); + // @formatter:on + } + + /** + * Container for exposing {@link org.apache.camel.Route} information as JSON. + */ + @JsonPropertyOrder({"id", "description", "uptime", "uptimeMillis"}) + @JsonInclude(JsonInclude.Include.NON_EMPTY) + public static class RouteEndpointInfo { + + private final String id; + + private final String description; + + private final String uptime; + + private final long uptimeMillis; + + public RouteEndpointInfo(Route route) { + this.id = route.getId(); + this.description = route.getDescription(); + this.uptime = route.getUptime(); + this.uptimeMillis = route.getUptimeMillis(); + } + + public String getId() { + return id; + } + + public String getDescription() { + return description; + } + + public String getUptime() { + return uptime; + } + + public long getUptimeMillis() { + return uptimeMillis; + } + + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/2a05c3b7/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpointAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpointAutoConfiguration.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpointAutoConfiguration.java new file mode 100644 index 0000000..628a705 --- /dev/null +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpointAutoConfiguration.java @@ -0,0 +1,47 @@ +/** + * 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.boot.actuate.endpoint; + +import org.apache.camel.CamelContext; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Auto configuration for the {@link CamelRoutesEndpoint}. + */ +@Configuration +@ConditionalOnClass({CamelRoutesEndpoint.class}) +@ConditionalOnBean(CamelAutoConfiguration.class) +@AutoConfigureAfter(CamelAutoConfiguration.class) +public class CamelRoutesEndpointAutoConfiguration { + + @ConditionalOnClass({CamelContext.class}) + @ConditionalOnMissingBean(CamelRoutesEndpoint.class) + protected static class CamelEndpointInitializer { + + @Bean + public CamelRoutesEndpoint camelEndpoint(CamelContext camelContext) { + return new CamelRoutesEndpoint(camelContext); + } + + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/2a05c3b7/components/camel-spring-boot/src/main/resources/META-INF/spring.factories ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/resources/META-INF/spring.factories b/components/camel-spring-boot/src/main/resources/META-INF/spring.factories index da3defc..48be2f1 100644 --- a/components/camel-spring-boot/src/main/resources/META-INF/spring.factories +++ b/components/camel-spring-boot/src/main/resources/META-INF/spring.factories @@ -17,6 +17,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.apache.camel.spring.boot.CamelAutoConfiguration,\ +org.apache.camel.spring.boot.actuate.endpoint.CamelRoutesEndpointAutoConfiguration,\ org.apache.camel.spring.boot.cloud.CamelCloudAutoConfiguration,\ org.apache.camel.spring.boot.cloud.CamelCloudServiceCallConfigurationAutoConfiguration,\ org.apache.camel.spring.boot.cloud.CamelCloudServiceDiscoveryAutoConfiguration,\ http://git-wip-us.apache.org/repos/asf/camel/blob/2a05c3b7/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/endpoint/ActuatorTestRoute.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/endpoint/ActuatorTestRoute.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/endpoint/ActuatorTestRoute.java new file mode 100644 index 0000000..599f940 --- /dev/null +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/endpoint/ActuatorTestRoute.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.boot.actuate.endpoint; + +import org.apache.camel.builder.RouteBuilder; +import org.springframework.stereotype.Component; + +@Component +public class ActuatorTestRoute extends RouteBuilder { + + @Override + public void configure() throws Exception { + from("timer:foo").routeId("foo-route").to("log:foo"); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/2a05c3b7/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpointTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpointTest.java new file mode 100644 index 0000000..45e2ffb --- /dev/null +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpointTest.java @@ -0,0 +1,57 @@ +/** + * 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.boot.actuate.endpoint; + +import java.util.List; + +import org.apache.camel.CamelContext; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.apache.camel.spring.boot.actuate.endpoint.CamelRoutesEndpoint.RouteEndpointInfo; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Test for the {@link CamelRoutesEndpoint} actuator endpoint. + */ +@RunWith(SpringRunner.class) +@EnableAutoConfiguration +@SpringBootApplication +@SpringBootTest(classes = {CamelAutoConfiguration.class, CamelRoutesEndpointAutoConfiguration.class, ActuatorTestRoute.class}) +public class CamelRoutesEndpointTest extends Assert { + + @Autowired + CamelRoutesEndpoint endpoint; + + @Autowired + CamelContext camelContext; + + @Test + public void testRoutesEndpoint() throws Exception { + List<RouteEndpointInfo> routes = endpoint.invoke(); + + assertFalse(routes.isEmpty()); + assertEquals(routes.size(), camelContext.getRoutes().size()); + assertTrue(routes.stream().anyMatch(r -> "foo-route".equals(r.getId()))); + } + +}