All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gravitee.gateway.standalone.vertx.VertxEmbeddedContainer Maven / Gradle / Ivy

There is a newer version: 4.5.5
Show newest version
/*
 * Copyright © 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed 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 io.gravitee.gateway.standalone.vertx;

import io.gravitee.common.component.AbstractLifecycleComponent;
import io.gravitee.gateway.reactive.standalone.vertx.HttpProtocolVerticle;
import io.gravitee.gateway.reactive.standalone.vertx.TcpProtocolVerticle;
import io.gravitee.node.vertx.verticle.factory.SpringVerticleFactory;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

/**
 * @author David BRASSELY (david.brassely at graviteesource.com)
 * @author GraviteeSource Team
 */
@RequiredArgsConstructor
public class VertxEmbeddedContainer extends AbstractLifecycleComponent {

    /**
     * Logger.
     */
    private final Logger logger = LoggerFactory.getLogger(VertxEmbeddedContainer.class);

    @Value("${http.instances:0}")
    private int httpInstances;

    @Value("${tcp.instances:0}")
    private int tcpInstances;

    private final Vertx vertx;

    private String httpDeploymentId;
    private String tcpDeploymentId;

    @Override
    public VertxEmbeddedContainer start() throws Exception {
        doStart();
        return this;
    }

    @Override
    public VertxEmbeddedContainer stop() throws Exception {
        doStop();
        return this;
    }

    @Override
    protected void doStart() throws Exception {
        if (httpInstances < 1 && tcpInstances < 1) {
            httpInstances = VertxOptions.DEFAULT_EVENT_LOOP_POOL_SIZE / 2;
            tcpInstances = VertxOptions.DEFAULT_EVENT_LOOP_POOL_SIZE / 2;
        } else {
            httpInstances = (httpInstances < 1) ? VertxOptions.DEFAULT_EVENT_LOOP_POOL_SIZE : httpInstances;
            tcpInstances = (tcpInstances < 1) ? VertxOptions.DEFAULT_EVENT_LOOP_POOL_SIZE : tcpInstances;
        }
        startHttpInstances();
        startTcpInstances();
    }

    private void startHttpInstances() {
        logger.info("Starting Vertx container and deploy Gateway HTTP Verticles [{} instance(s)]", httpInstances);

        final DeploymentOptions options = new DeploymentOptions().setInstances(httpInstances);
        final String verticleName = SpringVerticleFactory.VERTICLE_PREFIX + ':' + HttpProtocolVerticle.class.getName();

        vertx.deployVerticle(
            verticleName,
            options,
            event -> {
                if (event.failed()) {
                    logger.error("Unable to start HTTP server", event.cause());

                    // HTTP Server is a required component. Shutdown if not available
                    Runtime.getRuntime().exit(1);
                }

                httpDeploymentId = event.result();
                moveToStarted();
            }
        );
    }

    private void moveToStarted() {
        if (httpDeploymentId != null && tcpDeploymentId != null) {
            lifecycle.moveToStarted();
        }
    }

    private void startTcpInstances() {
        logger.info("Starting Vertx container and deploy Gateway TCP Verticles [{} instance(s)]", tcpInstances);

        final DeploymentOptions options = new DeploymentOptions().setInstances(tcpInstances);
        final String verticleName = SpringVerticleFactory.VERTICLE_PREFIX + ':' + TcpProtocolVerticle.class.getName();

        vertx.deployVerticle(
            verticleName,
            options,
            event -> {
                if (event.failed()) {
                    logger.error("Unable to start TCP server", event.cause());
                }

                tcpDeploymentId = event.result();
                moveToStarted();
            }
        );
    }

    @Override
    protected void doStop() {
        if (httpDeploymentId != null) {
            vertx.undeploy(httpDeploymentId, event -> lifecycle.moveToStopped());
        }
        if (tcpDeploymentId != null) {
            vertx.undeploy(tcpDeploymentId);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy