Skip to content

Commit

Permalink
Updated mvn exec. Made the root handler configurable from an HttpHandler
Browse files Browse the repository at this point in the history
instance or injected from a class.
  • Loading branch information
noboomu committed Apr 26, 2017
1 parent ec049d6 commit 56fd11d
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 79 deletions.
63 changes: 39 additions & 24 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<source>1.8</source>
<target>1.8</target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<compilerArgument>-parameters</compilerArgument>

</configuration>
Expand Down Expand Up @@ -76,17 +76,18 @@
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>

<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>

<executable>java</executable>
<arguments>
<argument>-Dlogback.configurationFile=${project.build.directory}/conf/logback.xml</argument>
<argument>-Xbootclasspath/p:lib/alpn-boot-8.1.11.v20170118.jar</argument>

<argument>-classpath</argument>
<!-- automatically creates the classpath using all project dependencies,
also adding the project build directory -->
Expand All @@ -111,7 +112,21 @@
</execution>
</executions>
</plugin>

<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>io.proteus.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
Expand Down Expand Up @@ -300,26 +315,26 @@
<artifactId>jansi</artifactId>
<version>1.15</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1211</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1211</version>
</dependency>

<dependency>
<groupId>io.proteus</groupId>
<artifactId>jsoniter</artifactId>
<version>0.9.8</version>
</dependency>
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>[3.2.0,)</version>
</dependency>
</dependencies>
<dependency>
<groupId>io.proteus</groupId>
<artifactId>jsoniter</artifactId>
<version>0.9.8</version>
</dependency>
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>[3.2.0,)</version>
</dependency>
</dependencies>
</project>
113 changes: 67 additions & 46 deletions src/main/java/io/proteus/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
*
*/
package io.proteus;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
Expand All @@ -13,7 +12,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.Service;
import com.google.common.util.concurrent.ServiceManager;
Expand All @@ -33,48 +32,40 @@
import io.proteus.controllers.Users;
import io.proteus.modules.ConfigModule;
import io.proteus.server.endpoints.EndpointInfo;
import io.proteus.server.handlers.BaseHttpHandler;
import io.proteus.server.handlers.DefaultHttpHandler;
import io.proteus.server.handlers.HandlerGenerator;
import io.proteus.services.AssetsService;
import io.proteus.services.SwaggerService;
import io.undertow.Undertow;
import io.undertow.UndertowOptions;
import io.undertow.server.DefaultResponseListener;
import io.undertow.server.HttpHandler;
import io.undertow.server.RoutingHandler;
/**
* @author jbauer
*/
public class Application
{



private static Logger log = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Application.class.getCanonicalName());

/*
* public static ExecutorService EXECUTOR =
new ThreadPoolExecutor(
cpuCount * 2, cpuCount * 25, 200, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(cpuCount * 100),
new ThreadPoolExecutor.CallerRunsPolicy());
*/



protected Injector injector = null;
protected ServiceManager serviceManager = null;
protected Undertow undertow = null;

protected Set<Class<? extends Service>> registeredServices = new HashSet<>();


@Inject
@Named("registeredControllers")
protected Set<Class<?>> registeredControllers;

@Inject
@Named("registeredEndpoints")
protected Set<EndpointInfo> registeredEndpoints;

protected Injector injector = null;
protected ServiceManager serviceManager = null;
protected Undertow undertow = null;

protected Set<Class<? extends Service>> registeredServices = new HashSet<>();

protected Class<? extends HttpHandler> rootHandlerClass;

protected HttpHandler rootHandler;


public Application()
{
Expand All @@ -86,39 +77,50 @@ public Application()

public void start()
{
log.info("Starting services...");


if( this.rootHandlerClass == null && this.rootHandler == null )
{
log.error("Cannot start the server without specifying the root handler class or a root HttpHandler!");
System.exit(1);
}

log.info("Starting services...");

Set<Service> services = registeredServices.stream()
.map( sc -> injector.getInstance(sc))
.collect(Collectors.toSet());

this.serviceManager = new ServiceManager(services);

this.serviceManager.addListener(new Listener() {
public void stopped() {}
public void stopped() {

}
public void healthy() {
log.info("Services are healthy...");



buildServer().start();
}
public void failure(Service service)
{
log.error("Error on service: " + service);
System.exit(1);
}
},
MoreExecutors.directExecutor());

Runtime.getRuntime().addShutdownHook(new Thread(() -> {

Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
log.info("Shutting down...");

serviceManager.stopAsync().awaitStopped(5, TimeUnit.SECONDS);
undertow.stop();

log.info("Shutdown complete.");
} catch (TimeoutException timeout) {
// stopping timed out
}}));
timeout.printStackTrace();
}}});

serviceManager.startAsync();
}
Expand Down Expand Up @@ -157,8 +159,16 @@ public Undertow buildServer()

log.info(sb.toString());

HttpHandler baseHandler = injector.getInstance(BaseHttpHandler.class);

final HttpHandler handler;

if( this.rootHandlerClass != null )
{
handler = this.injector.getInstance(this.rootHandlerClass);
}
else
{
handler = this.rootHandler;
}

undertow = Undertow.builder()
.addHttpListener(rootConfig.getInt("application.port"),rootConfig.getString("application.host"))
Expand All @@ -172,26 +182,36 @@ public Undertow buildServer()
.setServerOption(UndertowOptions.RECORD_REQUEST_START_TIME, false)
.setServerOption(UndertowOptions.MAX_ENTITY_SIZE, 1000000L * 200 )
.setWorkerThreads(Runtime.getRuntime().availableProcessors()*8)
.setHandler( baseHandler )
.setHandler( handler )
.build();



return undertow;
}

public Application useService(Class<? extends Service> serviceClass)
public Application addService(Class<? extends Service> serviceClass)
{
this.registeredServices.add(serviceClass);
return this;
}

public Application useController(Class<?> controllerClass)
public Application addController(Class<?> controllerClass)
{
this.registeredControllers.add(controllerClass);
return this;
}

public void setRootHandlerClass( Class<? extends HttpHandler> rootHandlerClass )
{
this.rootHandlerClass = rootHandlerClass;
}

public void setRootHandler( HttpHandler rootHandler )
{
this.rootHandler = rootHandler;
}


/**
* @return the undertow
Expand All @@ -206,24 +226,27 @@ public static void main(String[] args)
{

try
{
{

JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_WITH_HASH);
JsonStream.setMode(EncodingMode.DYNAMIC_MODE);
JsoniterAnnotationSupport.enable();

Application app = new Application();

app.useService(SwaggerService.class);
app.addService(SwaggerService.class);

app.useService(AssetsService.class);
app.addService(AssetsService.class);

app.useController(Users.class);
app.addController(Users.class);

app.addController(Benchmarks.class);

app.useController(Benchmarks.class);
app.setRootHandlerClass(DefaultHttpHandler.class);

app.start();



} catch (Exception e)
{
Expand All @@ -232,8 +255,6 @@ public static void main(String[] args)


}




}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
/**
* @author jbauer
*/
public class BaseHttpHandler implements HttpHandler
public class DefaultHttpHandler implements HttpHandler
{
private static Logger log = LoggerFactory.getLogger(BaseHttpHandler.class.getCanonicalName());
private static Logger log = LoggerFactory.getLogger(DefaultHttpHandler.class.getCanonicalName());

@Inject(optional=true)
protected DefaultResponseListener defaultResponseListener;
Expand All @@ -38,10 +38,10 @@ public class BaseHttpHandler implements HttpHandler

/**
* @param defaultResponseListener
* @param rootHandler
* @param router
*/
@Inject
public BaseHttpHandler(Config config)
public DefaultHttpHandler(Config config)
{
Config globalHeaders = config.getConfig("globalHeaders");

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/proteus/services/AssetsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class AssetsService extends BaseService
protected Set<EndpointInfo> registeredEndpoints;

@Inject
protected RoutingHandler rootHandler;
protected RoutingHandler router;

@Inject
@Named("assets")
Expand All @@ -59,7 +59,7 @@ protected void startUp() throws Exception
final FileResourceManager fileResourceManager = new FileResourceManager(Paths.get(assetsDirectoryName).toFile());


rootHandler.add(Methods.GET, assetsPath + "/*", io.undertow.Handlers.rewrite("regex['" + assetsPath + "/(.*)']", "/$1", getClass().getClassLoader(), new ResourceHandler(fileResourceManager)
router.add(Methods.GET, assetsPath + "/*", io.undertow.Handlers.rewrite("regex['" + assetsPath + "/(.*)']", "/$1", getClass().getClassLoader(), new ResourceHandler(fileResourceManager)
.setCachable(TruePredicate.instance())
.setCacheTime(assetsCacheTime)
));
Expand Down
Loading

0 comments on commit 56fd11d

Please sign in to comment.