package se252.jan15.calvinandhobbes.project0;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.NetworkListener;
import org.glassfish.grizzly.nio.transport.TCPNIOTransport;
import org.glassfish.grizzly.strategies.WorkerThreadIOStrategy;
import org.glassfish.grizzly.threadpool.ThreadPoolConfig;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import java.io.IOException;
import java.net.URI;
/**
* Main class to launch echo REST web service
*
* @author Yogesh Simmhan
* @version 1.0
* @see <a href="http://www.serc.iisc.ernet.in/~simmhan/SE252/">IISc SERC 'SE252:Intro to Cloud Computing' Course Webpage</a>
*
* (c) Yogesh Simmhan, 2015
* This work is licensed under a Attribution 4.0 International (CC BY 4.0).
* http://creativecommons.org/licenses/by/4.0/
*/
public class ServerLauncher {
/**
* Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
* @return Grizzly HTTP server.
*/
private static HttpServer startServer(String baseUri) throws IOException {
final ResourceConfig rc = new ResourceConfig().packages("se252.jan15.calvinandhobbes.project0");
System.out.println("Trying to starting service at: " + baseUri);
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create(baseUri), rc, false);
TCPNIOTransport transport = getListener(httpServer).getTransport();
ThreadPoolConfig config = ThreadPoolConfig.defaultConfig()
.setPoolName("worker-thread-").setCorePoolSize(20).setMaxPoolSize(20)
.setQueueLimit(-1)/* same as default */;
transport.configureBlocking(false);
transport.setSelectorRunnersCount(3);
transport.setWorkerThreadPoolConfig(config);
transport.setIOStrategy(WorkerThreadIOStrategy.getInstance());
transport.setTcpNoDelay(true);
httpServer.start();
System.out.println("Blocking Transport(T/F): "+transport.isBlocking());
System.out.println("Num SelectorRunners: "+transport.getSelectorRunnersCount());
System.out.println("Num WorkerThreads: "+transport.getWorkerThreadPoolConfig().getCorePoolSize());
return httpServer;
}
private static NetworkListener getListener(HttpServer httpServer) {
return httpServer.getListeners().iterator().next();
}
public static void main(String[] args) throws InterruptedException,
IOException, InstantiationException, IllegalAccessException,
ClassNotFoundException {
if(args.length != 1 && args.length !=2){
System.out.println("Please pass the public IP or public hostname of the local machine/VM as parameter. e.g. ec2-54-254-184-72.ap-southeast-1.compute.amazonaws.com");
System.out.println("You can also optionally pass a port number for the REST service. 8081 is used by default.");
return;
}
int port = args.length == 2 ? Integer.parseInt(args[1]) : 8081;
String baseUri = "http://" + args[0] + ":" + port + "/project0";
final HttpServer server = startServer(baseUri);
System.out.println("Initializing all Services");
IIScCampusMapGETProxyService.cacheInit();
IIScCampusMapRDBMS_GETService.cacheInit();
Queues.initQueues();
SDBConn.initSDB();
DBConn.initDBConn();
System.out.println(String.format("Jersey app started with WADL available at "
+ "%s/application.wadl\nHit enter to stop it...", baseUri));
System.in.read();
server.shutdownNow();
}
}