io.vertx.core.AbstractVerticle Maven / Gradle / Ivy
/*
* Copyright (c) 2011-2019 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;
import io.vertx.core.json.JsonObject;
import java.util.List;
/**
*
* An abstract base class that you can extend to write your own Verticle classes.
*
* Instead of implementing {@link io.vertx.core.Verticle} directly, it is often simpler to just extend this class.
*
* In the simplest case, just override the {@link #start(Promise)} method. If you have verticle clean-up to do you can
* optionally override the {@link #stop(Promise)} method too.
*
If your verticle does extra start-up or clean-up that takes some time (e.g. it deploys other verticles) then
* you should override the asynchronous {@link #start(Promise) start} and {@link #stop(Promise) stop} methods.
*
* This class also maintains references to the {@link io.vertx.core.Vertx} and {@link io.vertx.core.Context}
* instances of the verticle for easy access.
* It also provides methods for getting the {@link #config verticle configuration}, {@link #processArgs process arguments},
* and {@link #deploymentID deployment ID}.
*
* @author Tim Fox
*/
public abstract class AbstractVerticle 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 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 void init(Vertx vertx, Context context) {
this.vertx = vertx;
this.context = context;
}
/**
* Get the deployment ID of the verticle deployment
* @return the deployment ID
*/
public String deploymentID() {
return context.deploymentID();
}
/**
* Get the configuration of the verticle.
*
* This can be specified when the verticle is deployed.
* @return the configuration
*/
public JsonObject config() {
return context.config();
}
/**
* Get the arguments used when deploying the Vert.x process.
* @return the list of arguments
*/
public List processArgs() {
return context.processArgs();
}
/**
* Start the verticle.
* This is called by Vert.x when the verticle instance is deployed. Don't call it yourself.
* If your verticle does things in its startup which take some time then you can override this method
* and call the startFuture some time later when start up is complete.
* @param startPromise a promise which should be called when verticle start-up is complete.
* @throws Exception
*/
@Override
public void start(Promise startPromise) throws Exception {
start();
startPromise.complete();
}
/**
* Stop the verticle.
* This is called by Vert.x when the verticle instance is un-deployed. Don't call it yourself.
* If your verticle does things in its shut-down which take some time then you can override this method
* and call the stopFuture some time later when clean-up is complete.
* @param stopPromise a promise which should be called when verticle clean-up is complete.
* @throws Exception
*/
@Override
public void stop(Promise stopPromise) throws Exception {
stop();
stopPromise.complete();
}
/**
* If your verticle does a simple, synchronous start-up then override this method and put your start-up
* code in here.
* @throws Exception
*/
public void start() throws Exception {
}
/**
* If your verticle has simple synchronous clean-up tasks to complete then override this method and put your clean-up
* code in here.
* @throws Exception
*/
public void stop() throws Exception {
}
}