com.netflix.karyon.transport.http.HttpRxServerProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of karyon-governator Show documentation
Show all versions of karyon-governator Show documentation
karyon-governator developed by Netflix
The newest version!
package com.netflix.karyon.transport.http;
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 io.reactivex.netty.protocol.http.server.RequestHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PreDestroy;
import static com.netflix.karyon.utils.TypeUtils.keyFor;
/**
* @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(RequestHandler.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);
RequestHandler 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);
if (config.requiresThreadPool()) {
builder.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());
}
}