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

com.netflix.karyon.transport.http.HttpRxServerProvider Maven / Gradle / Ivy

There is a newer version: 2.1.00-RC6
Show newest version
package com.netflix.karyon.transport.http;

import javax.annotation.PreDestroy;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
import com.netflix.karyon.transport.AbstractServerModule.ServerConfig;
import com.netflix.karyon.transport.KaryonTransport;
import com.netflix.karyon.transport.http.KaryonHttpModule.HttpServerConfig;
import io.reactivex.netty.metrics.MetricEventsListenerFactory;
import io.reactivex.netty.pipeline.PipelineConfigurator;
import io.reactivex.netty.protocol.http.server.HttpServer;
import io.reactivex.netty.protocol.http.server.HttpServerBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.netflix.karyon.utils.TypeUtils.*;

/**
 * @author Tomasz Bak
 */
@SuppressWarnings("unchecked")
public class HttpRxServerProvider> implements Provider {

    private static final Logger logger = LoggerFactory.getLogger(HttpRxServerProvider.class);

    private final Named nameAnnotation;

    private final Key> routerKey;
    private final Key> interceptorSupportKey;
    @SuppressWarnings("rawtypes")
    private final Key pipelineConfiguratorKey;
    private final Key metricEventsListenerFactoryKey;
    private final Key serverConfigKey;

    private volatile HttpServer httpServer;

    public HttpRxServerProvider(String name, Class iType, Class oType) {
        nameAnnotation = Names.named(name);

        routerKey = keyFor(HttpRequestRouter.class, iType, oType, nameAnnotation);
        interceptorSupportKey = keyFor(GovernatorHttpInterceptorSupport.class, iType, oType, nameAnnotation);
        pipelineConfiguratorKey = Key.get(PipelineConfigurator.class, nameAnnotation);
        metricEventsListenerFactoryKey = Key.get(MetricEventsListenerFactory.class, nameAnnotation);
        serverConfigKey = Key.get(ServerConfig.class, nameAnnotation);
    }

    @Override
    public S get() {
        return (S) httpServer;
    }

    @PreDestroy
    public void shutdown() throws InterruptedException {
        if (httpServer != null) {
            httpServer.shutdown();
        }
    }

    @SuppressWarnings("rawtypes")
    @Inject
    public void setInjector(Injector injector) {
        HttpServerConfig config = (HttpServerConfig) injector.getInstance(serverConfigKey);

        HttpRequestRouter router = injector.getInstance(routerKey);

        GovernatorHttpInterceptorSupport interceptorSupport = injector.getInstance(interceptorSupportKey);
        interceptorSupport.finish(injector);
        HttpRequestHandler httpRequestHandler = new HttpRequestHandler(router, interceptorSupport);

        HttpServerBuilder builder = KaryonTransport.newHttpServerBuilder(config.getPort(), httpRequestHandler)
                .withRequestProcessingThreads(config.getThreadPoolSize());

        if (injector.getExistingBinding(pipelineConfiguratorKey) != null) {
            builder.appendPipelineConfigurator(injector.getInstance(pipelineConfiguratorKey));
        }

        if (injector.getExistingBinding(metricEventsListenerFactoryKey) != null) {
            builder.withMetricEventsListenerFactory(injector.getInstance(metricEventsListenerFactoryKey));
        }

        httpServer = builder.build().start();
        logger.info("Starting server {} on port {}...", nameAnnotation.value(), httpServer.getServerPort());
    }
}