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

com.netflix.karyon.transport.tcp.TcpRxServerProvider Maven / Gradle / Ivy

The newest version!
package com.netflix.karyon.transport.tcp;

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 io.reactivex.netty.RxNetty;
import io.reactivex.netty.channel.ConnectionHandler;
import io.reactivex.netty.metrics.MetricEventsListenerFactory;
import io.reactivex.netty.pipeline.PipelineConfigurator;
import io.reactivex.netty.server.RxServer;
import io.reactivex.netty.server.ServerBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

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

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

    private final Named nameAnnotation;

    protected final Key> connectionHandlerKey;
    @SuppressWarnings("rawtypes")
    private final Key pipelineConfiguratorKey;
    private final Key metricEventsListenerFactoryKey;
    private final Key serverConfigKey;

    private RxServer server;

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

        connectionHandlerKey = keyFor(ConnectionHandler.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) server;
    }

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

    @Inject
    public void setInjector(Injector injector) {
        ServerConfig config = injector.getInstance(serverConfigKey);

        ConnectionHandler connectionHandler = injector.getInstance(connectionHandlerKey);

        ServerBuilder builder = RxNetty.newTcpServerBuilder(config.getPort(), connectionHandler);

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

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

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