From 4d8bec8330e271dfbc9687410ca776ed7b7e477b Mon Sep 17 00:00:00 2001 From: Julien Viet Date: Thu, 1 Feb 2024 14:43:04 +0100 Subject: [PATCH] Implement a new base verticle that carries start/stop methods returning futures --- src/main/java/io/vertx/core/VerticleBase.java | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/main/java/io/vertx/core/VerticleBase.java diff --git a/src/main/java/io/vertx/core/VerticleBase.java b/src/main/java/io/vertx/core/VerticleBase.java new file mode 100644 index 00000000000..5d981c94326 --- /dev/null +++ b/src/main/java/io/vertx/core/VerticleBase.java @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2011-2023 Contributors to the Eclipse Foundation + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 + * which is available at https://www.apache.org/licenses/LICENSE-2.0. + * + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 + */ + +package io.vertx.core; + +/** + * + * An abstract base class that you can extend to write your own Verticle classes. + *

+ * Instead of implementing {@link Verticle} directly, it is often simpler to just extend this class. + *

+ * In the simplest case, just override the {@link #start()} method. If you have verticle clean-up to do you can optionally + * override the {@link #stop()} method too. + *

+ * This class also maintains references to the {@link Vertx} and {@link Context} instances of the verticle for easy access.

+ * + * @author Tim Fox + * @author Julien Viet + */ +public abstract class VerticleBase implements Verticle { + + /** + * Reference to the Vert.x instance that deployed this verticle + */ + protected Vertx vertx; + + /** + * Reference to the context of the verticle + */ + protected Context context; + + /** + * Get the Vert.x instance + * @return the Vert.x instance + */ + @Override + public final Vertx getVertx() { + return vertx; + } + + /** + * Initialise the verticle.

+ * This is called by Vert.x when the verticle instance is deployed. Don't call it yourself. + * @param vertx the deploying Vert.x instance + * @param context the context of the verticle + */ + @Override + public final void init(Vertx vertx, Context context) { + this.vertx = vertx; + this.context = context; + } + + /** + * This is called by Vert.x when the verticle instance is undeployed. Don't call it yourself. + */ + @Override + @SuppressWarnings("unchecked") + public final void start(Promise startPromise) throws Exception { + handle(start(), startPromise); + } + + /** + * This is called by Vert.x when the verticle instance is deployed. Don't call it yourself.

+ */ + @Override + public final void stop(Promise stopPromise) throws Exception { + handle(stop(), stopPromise); + } + + private void handle(Future completion, Promise promise) { + if (completion != null) { + completion.onComplete((Handler) promise); + } else { + promise.complete(); + } + } + + /** + * Override to implement Verticle start. + * + * @throws Exception any exception preventing the start of the verticle + */ + public Future start() throws Exception { + return Future.succeededFuture(); + } + + /** + * Override this put your clean-up code in here. + * + * @throws Exception any exception happening during the stop of the verticle + */ + public Future stop() throws Exception { + return Future.succeededFuture(); + } +}