This is an automated email from the ASF dual-hosted git repository.
lburgazzoli 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 db84194 camel-platform-http-vertx: simplify and cleanup the component
a bit
db84194 is described below
commit db84194fd4452db568b485675a082808d5f02976
Author: lburgazzoli <[email protected]>
AuthorDate: Wed May 6 12:33:06 2020 +0200
camel-platform-http-vertx: simplify and cleanup the component a bit
---
.../src/main/docs/platform-http-vertx.adoc | 2 +-
.../platform/http/vertx/VertxPlatformHttp.java | 76 ------
.../http/vertx/VertxPlatformHttpConsumer.java | 35 +--
.../http/vertx/VertxPlatformHttpEngine.java | 38 +--
.../http/vertx/VertxPlatformHttpRouter.java | 271 +++++++++++++++++++++
.../http/vertx/VertxPlatformHttpServer.java | 241 ++++++++++--------
.../http/vertx/VertxPlatformHttpEngineTest.java | 23 +-
7 files changed, 446 insertions(+), 240 deletions(-)
diff --git
a/components/camel-platform-http-vertx/src/main/docs/platform-http-vertx.adoc
b/components/camel-platform-http-vertx/src/main/docs/platform-http-vertx.adoc
index eb33c4d..314df00 100644
---
a/components/camel-platform-http-vertx/src/main/docs/platform-http-vertx.adoc
+++
b/components/camel-platform-http-vertx/src/main/docs/platform-http-vertx.adoc
@@ -37,7 +37,7 @@ final CamelContext context = new DefaultCamelContext();
VertxPlatformHttpServerConfiguration conf = new
VertxPlatformHttpServerConfiguration();
conf.setBindPort(port);
-context.addService(new VertxPlatformHttpServer(context, conf), true, true);
+context.addService(new VertxPlatformHttpServer(conf));
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
diff --git
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttp.java
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttp.java
deleted file mode 100644
index bdc63c0..0000000
---
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttp.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * 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.platform.http.vertx;
-
-import java.util.Collections;
-import java.util.List;
-
-import io.vertx.core.Handler;
-import io.vertx.core.Vertx;
-import io.vertx.ext.web.Router;
-import io.vertx.ext.web.RoutingContext;
-import org.apache.camel.CamelContext;
-import org.apache.camel.component.platform.http.PlatformHttpConstants;
-import org.apache.camel.support.CamelContextHelper;
-
-/**
- * This class holds an instance of a {@link Router} and an optional list of
[{@link Handler<RoutingContext>} that are
- * used by the {@link VertxPlatformHttpConsumer} to create and register Vert.x
{@link io.vertx.ext.web.Route}].
- */
-public class VertxPlatformHttp {
- public static final String PLATFORM_HTTP_ROUTER_NAME =
PlatformHttpConstants.PLATFORM_HTTP_COMPONENT_NAME + "-router";
-
- private final Vertx vertx;
- private final Router router;
- private final List<Handler<RoutingContext>> handlers;
-
- public VertxPlatformHttp(Vertx vertx, Router router) {
- this(vertx, router, Collections.emptyList());
- }
-
- public VertxPlatformHttp(Vertx vertx, Router router,
List<Handler<RoutingContext>> handlers) {
- this.vertx = vertx;
- this.router = router;
- this.handlers = handlers;
- }
-
- public Vertx vertx() {
- return this.vertx;
- }
-
- public Router router() {
- return router;
- }
-
- public List<Handler<RoutingContext>> handlers() {
- return handlers;
- }
-
- // **********************
- //
- // Helpers
- //
- // **********************
-
- public static VertxPlatformHttp lookup(CamelContext camelContext) {
- return CamelContextHelper.mandatoryLookup(
- camelContext,
- VertxPlatformHttp.PLATFORM_HTTP_ROUTER_NAME,
- VertxPlatformHttp.class
- );
- }
-}
diff --git
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java
index a0b716b..f816ea6 100644
---
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java
+++
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java
@@ -30,7 +30,6 @@ import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.FileUpload;
import io.vertx.ext.web.Route;
-import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
@@ -57,24 +56,22 @@ public class VertxPlatformHttpConsumer extends
DefaultConsumer {
private static final Logger LOGGER =
LoggerFactory.getLogger(VertxPlatformHttpConsumer.class);
private static final Pattern PATH_PARAMETER_PATTERN =
Pattern.compile("\\{([^/}]+)\\}");
- private final Router router;
private final List<Handler<RoutingContext>> handlers;
- private final String fileNameExtWhitelist;
private final UploadAttacher uploadAttacher;
+ private final String fileNameExtWhitelist;
private Route route;
- public VertxPlatformHttpConsumer(PlatformHttpEndpoint endpoint, Processor
processor, Router router,
- List<Handler<RoutingContext>> handlers,
UploadAttacher uploadAttacher) {
+ public VertxPlatformHttpConsumer(
+ PlatformHttpEndpoint endpoint,
+ Processor processor,
+ List<Handler<RoutingContext>> handlers,
+ UploadAttacher uploadAttacher) {
super(endpoint, processor);
- this.router = router;
this.handlers = handlers;
-
- String list = endpoint.getFileNameExtWhitelist();
-
- this.fileNameExtWhitelist = list == null ? null :
list.toLowerCase(Locale.US);
this.uploadAttacher = uploadAttacher;
+ this.fileNameExtWhitelist = endpoint.getFileNameExtWhitelist() == null
? null : endpoint.getFileNameExtWhitelist().toLowerCase(Locale.US);
}
@Override
@@ -83,17 +80,19 @@ public class VertxPlatformHttpConsumer extends
DefaultConsumer {
}
@Override
- protected void doInit() throws Exception {
- super.doInit();
+ protected void doStart() throws Exception {
+ super.doStart();
+ final VertxPlatformHttpRouter router =
VertxPlatformHttpRouter.lookup(getEndpoint().getCamelContext());
final PlatformHttpEndpoint endpoint = getEndpoint();
final String path = configureEndpointPath(endpoint);
final Route newRoute = router.route(path);
final Set<Method> methods =
Method.parseList(endpoint.getHttpMethodRestrict());
if (!methods.equals(Method.getAll())) {
- methods.stream().forEach(m ->
newRoute.method(HttpMethod.valueOf(m.name())));
+ methods.forEach(m ->
newRoute.method(HttpMethod.valueOf(m.name())));
}
+
if (endpoint.getConsumes() != null) {
newRoute.consumes(endpoint.getConsumes());
}
@@ -101,7 +100,10 @@ public class VertxPlatformHttpConsumer extends
DefaultConsumer {
newRoute.produces(endpoint.getProduces());
}
- handlers.forEach(newRoute::handler);
+ newRoute.handler(router.bodyHandler());
+ for (Handler<RoutingContext> handler: handlers) {
+ newRoute.handler(handler);
+ }
newRoute.handler(
ctx -> {
@@ -161,7 +163,7 @@ public class VertxPlatformHttpConsumer extends
DefaultConsumer {
private Exchange toExchange(RoutingContext ctx) {
final Exchange exchange = getEndpoint().createExchange();
- Message in = toCamelMessage(ctx, exchange);
+ final Message in = toCamelMessage(ctx, exchange);
final String charset =
ctx.parsedHeaders().contentType().parameter("charset");
if (charset != null) {
@@ -185,8 +187,7 @@ public class VertxPlatformHttpConsumer extends
DefaultConsumer {
final Map<String, Object> body = new HashMap<>();
for (String key : formData.names()) {
for (String value : formData.getAll(key)) {
- if (headerFilterStrategy != null
- &&
!headerFilterStrategy.applyFilterToExternalHeaders(key, value, exchange)) {
+ if (headerFilterStrategy != null &&
!headerFilterStrategy.applyFilterToExternalHeaders(key, value, exchange)) {
appendHeader(result.getHeaders(), key, value);
appendHeader(body, key, value);
}
diff --git
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngine.java
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngine.java
index d0769d6..98ebeff 100644
---
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngine.java
+++
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngine.java
@@ -22,8 +22,6 @@ import java.util.List;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
-import org.apache.camel.CamelContext;
-import org.apache.camel.CamelContextAware;
import org.apache.camel.Consumer;
import org.apache.camel.Experimental;
import org.apache.camel.Processor;
@@ -33,7 +31,6 @@ import
org.apache.camel.component.platform.http.spi.PlatformHttpEngine;
import org.apache.camel.component.platform.http.spi.UploadAttacher;
import org.apache.camel.spi.annotations.JdkService;
import org.apache.camel.support.service.ServiceSupport;
-import org.apache.camel.util.ObjectHelper;
/**
@@ -41,9 +38,7 @@ import org.apache.camel.util.ObjectHelper;
*/
@Experimental
@JdkService(PlatformHttpConstants.PLATFORM_HTTP_ENGINE_FACTORY)
-public class VertxPlatformHttpEngine extends ServiceSupport implements
PlatformHttpEngine, CamelContextAware {
- private CamelContext camelContext;
- private VertxPlatformHttp router;
+public class VertxPlatformHttpEngine extends ServiceSupport implements
PlatformHttpEngine {
private List<Handler<RoutingContext>> handlers;
private UploadAttacher uploadAttacher;
@@ -51,20 +46,12 @@ public class VertxPlatformHttpEngine extends ServiceSupport
implements PlatformH
this.handlers = Collections.emptyList();
}
- public VertxPlatformHttp getRouter() {
- return router;
- }
-
- public void setRouter(VertxPlatformHttp router) {
- this.router = router;
- }
-
public List<Handler<RoutingContext>> getHandlers() {
return Collections.unmodifiableList(handlers);
}
public void setHandlers(List<Handler<RoutingContext>> handlers) {
- if (handlers == null) {
+ if (handlers != null) {
this.handlers = new ArrayList<>(handlers);
}
}
@@ -78,22 +65,8 @@ public class VertxPlatformHttpEngine extends ServiceSupport
implements PlatformH
}
@Override
- public CamelContext getCamelContext() {
- return camelContext;
- }
-
- @Override
- public void setCamelContext(CamelContext camelContext) {
- this.camelContext = camelContext;
- }
-
- @Override
protected void doStart() throws Exception {
- if (router == null) {
- ObjectHelper.notNull(getCamelContext(), "Camel Context");
-
- router = VertxPlatformHttp.lookup(getCamelContext());
- }
+ // no-op
}
@Override
@@ -103,14 +76,9 @@ public class VertxPlatformHttpEngine extends ServiceSupport
implements PlatformH
@Override
public Consumer createConsumer(PlatformHttpEndpoint endpoint, Processor
processor) {
- List<Handler<RoutingContext>> handlers = new
ArrayList<>(this.handlers.size() + router.handlers().size());
- handlers.addAll(this.router.handlers());
- handlers.addAll(this.handlers);
-
return new VertxPlatformHttpConsumer(
endpoint,
processor,
- router.router(),
handlers,
uploadAttacher);
}
diff --git
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpRouter.java
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpRouter.java
new file mode 100644
index 0000000..74a0f41
--- /dev/null
+++
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpRouter.java
@@ -0,0 +1,271 @@
+/*
+ * 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.platform.http.vertx;
+
+import java.util.List;
+
+import io.vertx.core.Handler;
+import io.vertx.core.Vertx;
+import io.vertx.core.http.HttpMethod;
+import io.vertx.core.http.HttpServerRequest;
+import io.vertx.ext.web.Route;
+import io.vertx.ext.web.Router;
+import io.vertx.ext.web.RoutingContext;
+import io.vertx.ext.web.handler.BodyHandler;
+import org.apache.camel.CamelContext;
+import org.apache.camel.component.platform.http.PlatformHttpConstants;
+import org.apache.camel.support.CamelContextHelper;
+
+public class VertxPlatformHttpRouter implements Router {
+ public static final String PLATFORM_HTTP_ROUTER_NAME =
PlatformHttpConstants.PLATFORM_HTTP_COMPONENT_NAME + "-router";
+
+ private final Vertx vertx;
+ private final Router delegate;
+
+ public VertxPlatformHttpRouter(Vertx vertx, Router delegate) {
+ this.vertx = vertx;
+ this.delegate = delegate;
+ }
+
+ public Vertx vertx() {
+ return vertx;
+ }
+
+ @Override
+ public Route route() {
+ return delegate.route();
+ }
+
+ @Override
+ public Route route(HttpMethod method, String s) {
+ return delegate.route(method, s);
+ }
+
+ @Override
+ public Route route(String s) {
+ return delegate.route(s);
+ }
+
+ @Override
+ public Route routeWithRegex(HttpMethod method, String s) {
+ return delegate.routeWithRegex(method, s);
+ }
+
+ @Override
+ public Route routeWithRegex(String s) {
+ return delegate.routeWithRegex(s);
+ }
+
+ @Override
+ public Route get() {
+ return delegate.get();
+ }
+
+ @Override
+ public Route get(String s) {
+ return delegate.get(s);
+ }
+
+ @Override
+ public Route getWithRegex(String s) {
+ return delegate.getWithRegex(s);
+ }
+
+ @Override
+ public Route head() {
+ return delegate.head();
+ }
+
+ @Override
+ public Route head(String s) {
+ return delegate.head(s);
+ }
+
+ @Override
+ public Route headWithRegex(String s) {
+ return delegate.headWithRegex(s);
+ }
+
+ @Override
+ public Route options() {
+ return delegate.options();
+ }
+
+ @Override
+ public Route options(String s) {
+ return delegate.options(s);
+ }
+
+ @Override
+ public Route optionsWithRegex(String s) {
+ return delegate.optionsWithRegex(s);
+ }
+
+ @Override
+ public Route put() {
+ return delegate.put();
+ }
+
+ @Override
+ public Route put(String s) {
+ return delegate.put(s);
+ }
+
+ @Override
+ public Route putWithRegex(String s) {
+ return delegate.putWithRegex(s);
+ }
+
+ @Override
+ public Route post() {
+ return delegate.post();
+ }
+
+ @Override
+ public Route post(String s) {
+ return delegate.post(s);
+ }
+
+ @Override
+ public Route postWithRegex(String s) {
+ return delegate.postWithRegex(s);
+ }
+
+ @Override
+ public Route delete() {
+ return delegate.delete();
+ }
+
+ @Override
+ public Route delete(String s) {
+ return delegate.delete(s);
+ }
+
+ @Override
+ public Route deleteWithRegex(String s) {
+ return delegate.deleteWithRegex(s);
+ }
+
+ @Override
+ public Route trace() {
+ return delegate.trace();
+ }
+
+ @Override
+ public Route trace(String s) {
+ return delegate.trace(s);
+ }
+
+ @Override
+ public Route traceWithRegex(String s) {
+ return delegate.traceWithRegex(s);
+ }
+
+ @Override
+ public Route connect() {
+ return delegate.connect();
+ }
+
+ @Override
+ public Route connect(String s) {
+ return delegate.connect(s);
+ }
+
+ @Override
+ public Route connectWithRegex(String s) {
+ return delegate.connectWithRegex(s);
+ }
+
+ @Override
+ public Route patch() {
+ return delegate.patch();
+ }
+
+ @Override
+ public Route patch(String s) {
+ return delegate.patch(s);
+ }
+
+ @Override
+ public Route patchWithRegex(String s) {
+ return delegate.patchWithRegex(s);
+ }
+
+ @Override
+ public List<Route> getRoutes() {
+ return delegate.getRoutes();
+ }
+
+ @Override
+ public Router clear() {
+ return delegate.clear();
+ }
+
+ @Override
+ public Router mountSubRouter(String s, Router router) {
+ return delegate.mountSubRouter(s, router);
+ }
+
+ @Override
+ @Deprecated
+ public Router exceptionHandler(Handler<Throwable> handler) {
+ return delegate.exceptionHandler(handler);
+ }
+
+ @Override
+ public Router errorHandler(int i, Handler<RoutingContext> handler) {
+ return delegate.errorHandler(i, handler);
+ }
+
+ @Override
+ public void handleContext(RoutingContext context) {
+ delegate.handleContext(context);
+ }
+
+ @Override
+ public void handleFailure(RoutingContext context) {
+ delegate.handleFailure(context);
+ }
+
+ @Override
+ public Router modifiedHandler(Handler<Router> handler) {
+ return delegate.modifiedHandler(handler);
+ }
+
+ @Override
+ public void handle(HttpServerRequest request) {
+ delegate.handle(request);
+ }
+
+ public Handler<RoutingContext> bodyHandler() {
+ return BodyHandler.create();
+ }
+
+ // **********************
+ //
+ // Helpers
+ //
+ // **********************
+
+ public static VertxPlatformHttpRouter lookup(CamelContext camelContext) {
+ return CamelContextHelper.mandatoryLookup(
+ camelContext,
+ VertxPlatformHttpRouter.PLATFORM_HTTP_ROUTER_NAME,
+ VertxPlatformHttpRouter.class
+ );
+ }
+}
diff --git
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpServer.java
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpServer.java
index 3360c81..21d8f6d 100644
---
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpServer.java
+++
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpServer.java
@@ -16,20 +16,22 @@
*/
package org.apache.camel.component.platform.http.vertx;
-import java.util.Collections;
import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.CompletionStage;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
+import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.ext.web.Router;
+import io.vertx.ext.web.RoutingContext;
import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
import org.apache.camel.component.platform.http.PlatformHttpConstants;
import org.apache.camel.support.CamelContextHelper;
+import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.support.service.ServiceSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -42,94 +44,70 @@ import static
org.apache.camel.component.platform.http.vertx.VertxPlatformHttpSe
* This class implement a basic Vert.x Web based server that can be used by
the {@link VertxPlatformHttpEngine} on
* platforms that do not provide Vert.x based http services.
*/
-public class VertxPlatformHttpServer extends ServiceSupport {
+public class VertxPlatformHttpServer extends ServiceSupport implements
CamelContextAware {
private static final Logger LOGGER =
LoggerFactory.getLogger(VertxPlatformHttpServer.class);
- private final CamelContext context;
private final VertxPlatformHttpServerConfiguration configuration;
+ private CamelContext context;
private ExecutorService executor;
- private Vertx vertx;
- private boolean localVertx;
+ private boolean localVertx;
+ private Vertx vertx;
+ private Router router;
+ private Router subRouter;
private HttpServer server;
- public VertxPlatformHttpServer(CamelContext context,
VertxPlatformHttpServerConfiguration configuration) {
- this(context, configuration, null);
+ public VertxPlatformHttpServer(VertxPlatformHttpServerConfiguration
configuration) {
+ this.configuration = configuration;
}
- public VertxPlatformHttpServer(CamelContext context,
VertxPlatformHttpServerConfiguration configuration, Vertx vertx) {
+ @Override
+ public CamelContext getCamelContext() {
+ return context;
+ }
+
+ @Override
+ public void setCamelContext(CamelContext context) {
this.context = context;
- this.configuration = configuration;
+ }
+
+ public Vertx getVertx() {
+ return vertx;
+ }
+
+ public void setVertx(Vertx vertx) {
+ if (ServiceHelper.isStarted(this)) {
+ throw new IllegalArgumentException("Can't set the Vertx instance
after the service has been started");
+ }
+
this.vertx = vertx;
+ this.localVertx = false;
}
@Override
protected void doStart() throws Exception {
- executor =
context.getExecutorServiceManager().newSingleThreadExecutor(this,
"platform-http-service");
- vertx = lookupVertx();
- if (vertx == null) {
- LOGGER.info("Creating new Vert.x instance");
- vertx = createVertxInstance();
- localVertx = true;
- } else {
- LOGGER.info("Found Vert.x instance in registry: {}", vertx);
- }
-
- startAsync().toCompletableFuture().join();
+ initializeServer();
+ startServer();
}
@Override
protected void doStop() throws Exception {
- try {
- if (server != null) {
- stopAsync().toCompletableFuture().join();
- }
- } finally {
- this.server = null;
- }
-
- if (vertx != null && localVertx) {
- try {
- executor.submit(
- () -> {
- CountDownLatch latch = new CountDownLatch(1);
-
- vertx.close(result -> {
- try {
- if (result.failed()) {
- LOGGER.warn("Failed to close Vert.x
reason: {}",
- result.cause().getMessage()
- );
-
- throw new RuntimeException(result.cause());
- }
-
- LOGGER.info("Vert.x stopped");
- } finally {
- latch.countDown();
- }
- });
-
- try {
- latch.await();
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
- ).get();
- } finally {
- vertx = null;
- localVertx = false;
- }
- }
+ stopServer();
+ stopVertx();
- if (executor != null) {
- context.getExecutorServiceManager().shutdown(executor);
- executor = null;
+ if (this.executor != null) {
+ this.context.getExecutorServiceManager().shutdown(this.executor);
+ this.executor = null;
}
}
+ // *******************************
+ //
+ // Helpers
+ //
+ // *******************************
+
protected Vertx lookupVertx() {
return CamelContextHelper.findByType(context, Vertx.class);
}
@@ -143,9 +121,20 @@ public class VertxPlatformHttpServer extends
ServiceSupport {
return Vertx.vertx(options);
}
- private CompletionStage<Void> startAsync() {
- final Router router = Router.router(vertx);
- final Router subRouter = Router.router(vertx);
+ protected void initializeServer() {
+ if (vertx == null) {
+ vertx = lookupVertx();
+ if (vertx == null) {
+ LOGGER.info("Creating new Vert.x instance");
+ vertx = createVertxInstance();
+ localVertx = true;
+ } else {
+ LOGGER.info("Found Vert.x instance in registry: {}", vertx);
+ }
+ }
+
+ this.router = Router.router(vertx);
+ this.subRouter = Router.router(vertx);
if (configuration.getCors().isEnabled()) {
subRouter.route().handler(createCorsHandler(configuration));
@@ -154,17 +143,25 @@ public class VertxPlatformHttpServer extends
ServiceSupport {
router.mountSubRouter(configuration.getPath(), subRouter);
context.getRegistry().bind(
- VertxPlatformHttp.PLATFORM_HTTP_ROUTER_NAME,
- new VertxPlatformHttp(vertx, subRouter,
Collections.singletonList(createBodyHandler(configuration)))
+ VertxPlatformHttpRouter.PLATFORM_HTTP_ROUTER_NAME,
+ new VertxPlatformHttpRouter(vertx, subRouter) {
+ @Override
+ public Handler<RoutingContext> bodyHandler() {
+ return createBodyHandler(configuration);
+ }
+ }
);
+ }
+ protected void startServer() {
HttpServerOptions options = new HttpServerOptions();
configureSSL(options, configuration, context);
+ executor =
context.getExecutorServiceManager().newSingleThreadExecutor(this,
"platform-http-service");
server = vertx.createHttpServer(options);
- return CompletableFuture.runAsync(
+ CompletableFuture.runAsync(
() -> {
CountDownLatch latch = new CountDownLatch(1);
server.requestHandler(router).listen(configuration.getBindPort(),
configuration.getBindHost(), result -> {
@@ -192,40 +189,88 @@ public class VertxPlatformHttpServer extends
ServiceSupport {
}
},
executor
- );
+ ).toCompletableFuture().join();
}
- private CompletionStage<Void> stopAsync() {
- return CompletableFuture.runAsync(
- () -> {
- CountDownLatch latch = new CountDownLatch(1);
+ protected void stopServer() {
+ if (this.server == null) {
+ return;
+ }
- // remove the platform-http component
-
context.removeComponent(PlatformHttpConstants.PLATFORM_HTTP_COMPONENT_NAME);
+ try {
+ CompletableFuture.runAsync(
+ () -> {
+ CountDownLatch latch = new CountDownLatch(1);
- server.close(result -> {
- try {
- if (result.failed()) {
- LOGGER.warn("Failed to close Vert.x HttpServer
reason: {}",
- result.cause().getMessage()
- );
+ // remove the platform-http component
+
context.removeComponent(PlatformHttpConstants.PLATFORM_HTTP_COMPONENT_NAME);
- throw new RuntimeException(result.cause());
+ server.close(result -> {
+ try {
+ if (result.failed()) {
+ LOGGER.warn("Failed to close Vert.x HttpServer
reason: {}",
+ result.cause().getMessage()
+ );
+
+ throw new RuntimeException(result.cause());
+ }
+
+ LOGGER.info("Vert.x HttpServer stopped");
+ } finally {
+ latch.countDown();
}
+ });
- LOGGER.info("Vert.x HttpServer stopped");
- } finally {
- latch.countDown();
+ try {
+ latch.await();
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
}
- });
+ },
+ executor
+ ).toCompletableFuture().join();
+ } finally {
+ this.server = null;
+ }
+ }
- try {
- latch.await();
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- },
- executor
- );
+ protected void stopVertx() {
+ if (this.vertx == null || this.localVertx) {
+ return;
+ }
+
+ try {
+ CompletableFuture.runAsync(
+ () -> {
+ CountDownLatch latch = new CountDownLatch(1);
+
+ vertx.close(result -> {
+ try {
+ if (result.failed()) {
+ LOGGER.warn("Failed to close Vert.x reason:
{}",
+ result.cause().getMessage()
+ );
+
+ throw new RuntimeException(result.cause());
+ }
+
+ LOGGER.info("Vert.x stopped");
+ } finally {
+ latch.countDown();
+ }
+ });
+
+ try {
+ latch.await();
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ },
+ executor
+ ).toCompletableFuture().join();
+ } finally {
+ this.vertx = null;
+ this.localVertx = false;
+ }
}
}
diff --git
a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
index 075cf52..e993860 100644
---
a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
+++
b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
@@ -84,7 +84,7 @@ public class VertxPlatformHttpEngineTest {
VertxPlatformHttpServerConfiguration conf = new
VertxPlatformHttpServerConfiguration();
conf.setBindPort(port);
- context.addService(new VertxPlatformHttpServer(context, conf),
true, true);
+ context.addService(new VertxPlatformHttpServer(conf));
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
@@ -99,12 +99,9 @@ public class VertxPlatformHttpEngineTest {
context.start();
- assertThat(VertxPlatformHttp.lookup(context)).isNotNull();
+ assertThat(VertxPlatformHttpRouter.lookup(context)).isNotNull();
assertThat(context.getComponent("platform-http")).isInstanceOfSatisfying(PlatformHttpComponent.class,
component -> {
-
assertThat(component.getEngine()).isInstanceOfSatisfying(VertxPlatformHttpEngine.class,
e -> {
- assertThat(e.getRouter().router()).isNotNull();
- assertThat(e.getRouter().handlers()).isNotEmpty();
- });
+
assertThat(component.getEngine()).isInstanceOf(VertxPlatformHttpEngine.class);
});
given()
@@ -138,13 +135,13 @@ public class VertxPlatformHttpEngineTest {
CamelContext context = new DefaultCamelContext();
try {
- context.addService(new VertxPlatformHttpServer(context, conf),
true, true);
+ context.addService(new VertxPlatformHttpServer(conf));
context.getRegistry().bind("clientSSLContextParameters",
clientSSLParameters);
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
- fromF("platform-http:/")
+ from("platform-http:/")
.transform().body(String.class, b -> b.toUpperCase());
}
});
@@ -172,13 +169,13 @@ public class VertxPlatformHttpEngineTest {
try {
context.setSSLContextParameters(serverSSLParameters);
- context.addService(new VertxPlatformHttpServer(context, conf),
true, true);
+ context.addService(new VertxPlatformHttpServer(conf));
context.getRegistry().bind("clientSSLContextParameters",
clientSSLParameters);
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
- fromF("platform-http:/")
+ from("platform-http:/")
.transform().body(String.class, b -> b.toUpperCase());
}
});
@@ -206,7 +203,7 @@ public class VertxPlatformHttpEngineTest {
CamelContext context = new DefaultCamelContext();
try {
- context.addService(new VertxPlatformHttpServer(context, conf),
true, true);
+ context.addService(new VertxPlatformHttpServer(conf));
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
@@ -246,12 +243,12 @@ public class VertxPlatformHttpEngineTest {
CamelContext context = new DefaultCamelContext();
try {
final String greeting = "Hello Camel";
- context.addService(new VertxPlatformHttpServer(context, conf),
true, true);
+ context.addService(new VertxPlatformHttpServer(conf));
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("platform-http:/greeting/{name}?matchOnUriPrefix=true")
- .transform().simple("Hello ${header.name}");
+ .transform().simple("Hello ${header.name}");
}
});