com.netflix.karyon.transport.http.websockets.WebSocketsRxServerProvider 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.websockets;
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.http.websockets.KaryonWebSocketsModule.WebSocketsServerConfig;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
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.protocol.http.websocket.WebSocketServerBuilder;
import io.reactivex.netty.server.RxServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.netflix.karyon.utils.TypeUtils.*;
/**
* @author Tomasz Bak
*/
public class WebSocketsRxServerProvider> implements Provider {
private static final Logger logger = LoggerFactory.getLogger(WebSocketsRxServerProvider.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 WebSocketsRxServerProvider(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);
}
@SuppressWarnings("unchecked")
@Override
public S get() {
return (S) server;
}
@PreDestroy
public void shutdown() throws InterruptedException {
if (server != null) {
logger.info("Starting WebSockets server {} on port {}...", nameAnnotation.value(), server.getServerPort());
server.shutdown();
}
}
@Inject
@SuppressWarnings("unchecked")
public void setInjector(Injector injector) {
WebSocketsServerConfig config = (WebSocketsServerConfig) injector.getInstance(serverConfigKey);
ConnectionHandler connectionHandler = injector.getInstance(connectionHandlerKey);
WebSocketServerBuilder builder = RxNetty.newWebSocketServerBuilder(config.getPort(), connectionHandler)
.withMessageAggregator(config.isMessageAggregator());
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 WebSockets server {} on port {}...", nameAnnotation.value(), server.getServerPort());
}
}