com.google.sitebricks.NettySitebricks Maven / Gradle / Ivy
package com.google.sitebricks;
import com.google.common.base.Preconditions;
import com.google.inject.Inject;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author [email protected] (Dhanji R. Prasanna)
*/
class NettySitebricks implements Sitebricks {
private ExecutorService bossPool;
private ExecutorService workerPool;
private ServerBootstrap bootstrap;
private final Config config = new Config();
private final SitebricksAsyncHandler handler;
@Inject private java.util.logging.Logger log;
@Inject
public NettySitebricks(SitebricksAsyncHandler handler) {
this.handler = handler;
}
@Override
public Sitebricks executors(ExecutorService bossPool, ExecutorService workerPool) {
Preconditions.checkArgument(bossPool != null, "Boss executor cannot be null!");
Preconditions.checkArgument(workerPool != null, "Worker executor cannot be null!");
this.bossPool = bossPool;
this.workerPool = workerPool;
return this;
}
@Override
public Sitebricks enableWebsockets(String uri) {
config.websocketsUri = uri;
return this;
}
@Override
public Sitebricks enableWebsockets() {
return enableWebsockets("/websocket");
}
@Override public Sitebricks at(String host, int port) {
config.host = host;
config.port = port;
return this;
}
@Override public synchronized void start() {
if (null == bossPool) {
bossPool = Executors.newCachedThreadPool();
workerPool = Executors.newCachedThreadPool();
}
// Start sitebricks.
handler.startup(config);
// Start Netty.
this.bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(bossPool, workerPool));
this.bootstrap.setPipelineFactory(new SitebricksWebsocketPipelineFactory(config, handler));
InetSocketAddress address = new InetSocketAddress(config.host, config.port);
this.bootstrap.bind(address);
log.info("Sitebricks Async started. Listening at http(s)://" + config.host + ":"
+ config.port);
}
@Override public synchronized void shutdown() {
// This should shut down the threadpools too.
bootstrap.releaseExternalResources();
handler.shutdown();
}
}