Skip to content

Commit

Permalink
Whole lot of fixes and enhancements. Hopefully little to no performance
Browse files Browse the repository at this point in the history
loss.
  • Loading branch information
noboomu committed Apr 24, 2017
1 parent ba639d7 commit 756d337
Show file tree
Hide file tree
Showing 15 changed files with 2,390 additions and 297 deletions.
21 changes: 16 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>

<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
Expand Down Expand Up @@ -117,11 +118,6 @@
<artifactId>javassist</artifactId>
<version>3.22.0-CR1</version>
</dependency>
<dependency>
<groupId>com.jsoniter</groupId>
<artifactId>jsoniter</artifactId>
<version>0.9.8</version>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-core</artifactId>
Expand Down Expand Up @@ -246,6 +242,21 @@
<artifactId>fast-classpath-scanner</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1211</version>
</dependency>

<dependency>
<groupId>com.wurrly</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>
72 changes: 47 additions & 25 deletions src/main/java/com/wurrly/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
*/
package com.wurrly;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand All @@ -24,8 +31,11 @@
import com.jsoniter.DecodingMode;
import com.jsoniter.JsonIterator;
import com.jsoniter.annotation.JsoniterAnnotationSupport;
import com.jsoniter.output.Codegen;
import com.jsoniter.output.EncodingMode;
import com.jsoniter.output.JsonStream;
import com.jsoniter.spi.Encoder;
import com.jsoniter.spi.TypeLiteral;
import com.typesafe.config.Config;
import com.wurrly.controllers.Users;
import com.wurrly.modules.ConfigModule;
Expand All @@ -35,7 +45,8 @@
import com.wurrly.server.handlers.benchmark.BenchmarkHandlers;
import com.wurrly.services.AssetsService;
import com.wurrly.services.SwaggerService;


import io.undertow.Handlers;
import io.undertow.Undertow;
import io.undertow.UndertowOptions;
import io.undertow.server.DefaultResponseListener;
Expand All @@ -52,19 +63,15 @@ public class Application


private static Logger log = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Application.class.getCanonicalName());
private static final String CHARSET = "UTF-8";




public static class BaseHandlers
{
public static void notFoundHandler(HttpServerExchange exchange) {
exchange.setStatusCode(404);
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
exchange.getResponseSender().send("Page Not Found!!");
}
}

/*
* 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;
Expand All @@ -80,8 +87,7 @@ public static void notFoundHandler(HttpServerExchange exchange) {
@Inject
@Named("registeredEndpoints")
protected Set<EndpointInfo> registeredEndpoints;

protected RoutingModule routingModule = null;


public Application()
{
Expand Down Expand Up @@ -129,7 +135,7 @@ public void failure(Service service)

serviceManager.startAsync();
}
public Undertow buildServer()
{

Expand All @@ -156,19 +162,24 @@ public Undertow buildServer()

log.info("\n\nRegistered the following endpoints: \n\n" + sb.toString());



webServer = Undertow.builder()
.addHttpListener(rootConfig.getInt("application.port"), "localhost")
.addHttpListener(rootConfig.getInt("application.port"),rootConfig.getString("application.host"))
.setBufferSize(1024 * 16)
.setIoThreads(Runtime.getRuntime().availableProcessors())
.setServerOption(UndertowOptions.ENABLE_HTTP2, true)
.setIoThreads(Runtime.getRuntime().availableProcessors()*2)
.setServerOption(UndertowOptions.ENABLE_HTTP2, false)
.setServerOption(UndertowOptions.ALWAYS_SET_DATE, true)

// .setServerOption(UndertowOptions.BUFFER_PIPELINED_DATA, true)
.setSocketOption(org.xnio.Options.BACKLOG, 10000)
.setServerOption(UndertowOptions.ALWAYS_SET_KEEP_ALIVE, false)
.setServerOption(UndertowOptions.RECORD_REQUEST_START_TIME, false)
.setServerOption(UndertowOptions.MAX_ENTITY_SIZE, 1000000L * 200 )
.setWorkerThreads(Runtime.getRuntime().availableProcessors() * 8)
.setHandler( new HttpHandler()
.setWorkerThreads(Runtime.getRuntime().availableProcessors()*8)
.setHandler(
new HttpHandler()
{

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception
{
Expand All @@ -178,7 +189,7 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception
// exchange.getResponseHeaders().put(HttpString.tryFromString("Access-Control-Allow-Origin"), "*");
// exchange.getResponseHeaders().put(HttpString.tryFromString("Access-Control-Allow-Methods"), "*");
// exchange.getResponseHeaders().put(HttpString.tryFromString("Access-Control-Allow-Headers"), "*");
exchange.getResponseHeaders().put(Headers.SERVER, "Bowser");
//exchange.getResponseHeaders().put(Headers.SERVER, "Bowser");

try {
router.handleRequest(exchange);
Expand All @@ -191,7 +202,10 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception
}

}
}).build();
}
).build();



return webServer;
}
Expand Down Expand Up @@ -237,5 +251,13 @@ public static void main(String[] args)
}


public static class BaseHandlers
{
public static void notFoundHandler(HttpServerExchange exchange) {
exchange.setStatusCode(404);
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
exchange.getResponseSender().send("Page Not Found!!");
}
}

}
69 changes: 52 additions & 17 deletions src/main/java/com/wurrly/controllers/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
Expand All @@ -22,10 +21,12 @@
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

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

import com.google.common.collect.ImmutableMap;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
Expand All @@ -39,14 +40,15 @@
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.HttpString;
/**
* User API
*/
@Api(tags="users")
@Path("/api/users")
@Produces(("application/json"))
@Consumes(("application/json"))
@Produces((MediaType.APPLICATION_JSON))
@Consumes((MediaType.APPLICATION_JSON))
@Singleton
public class Users
{
Expand All @@ -69,7 +71,7 @@ public Users()
@GET
@Path("/{userId}/type")
@ApiOperation(value = "Find users by id with type", httpMethod = "GET", response = User.class)
public ServerResponse userType(
public ServerResponse<User> userType(
@ApiParam(hidden=true)final ServerRequest serverRequest, @PathParam("userId") final Long userId,
@QueryParam("optionalQueryString") Optional<String> optionalQueryString,
@QueryParam("optionalLong") Optional<Long> optionalLong,
Expand Down Expand Up @@ -101,7 +103,7 @@ public ServerResponse userType(
log.debug("optionalDate: " + optionalDate);


return response()
return response(User.class)
.ok()
.entity(new User(232343L))
.header(HttpString.tryFromString("TestHeader"), "57475475");
Expand All @@ -112,7 +114,7 @@ public ServerResponse userType(
@Path("/form/{userId}")
@Consumes("*/*")
@ApiOperation(value = "Post a complex form", httpMethod = "POST", response = User.class)
public ServerResponse userForm( final ServerRequest serverRequest,
public ServerResponse<Any> userForm( final ServerRequest serverRequest,
@ApiParam(name="userId",required=true) @PathParam("userId") final Long userId,
@ApiParam(name="context",required=false) @QueryParam("context") Optional<String> context,
@ApiParam(name="type",required=true) @QueryParam("type") User.UserType type,
Expand All @@ -126,15 +128,15 @@ public ServerResponse userForm( final ServerRequest serverRequest,
log.debug("testFile: " + testFile);
//
//
return response().ok().entity(Any.wrap(new User(userId,type)));
return response(Any.class).ok().entity(Any.wrap(new User(userId,type)));

}


@GET
@Path("/{userId}")
@ApiOperation(value = "Find users by id", httpMethod = "GET", response = User.class)
public ServerResponse user( final ServerRequest serverRequest,
public ServerResponse<String> user( final ServerRequest serverRequest,
@ApiParam(name="userId", required=true) @PathParam("userId") final Long userId,
@ApiParam(name="context", required=false) @QueryParam("context") Optional<String> context
)
Expand All @@ -146,7 +148,7 @@ public ServerResponse user( final ServerRequest serverRequest,
// log.debug("context: " + context);
//
//
return response()
return response(String.class)
.ok()
.applicationJson()
.body(JsonStream.serialize(new User(userId)));
Expand All @@ -160,14 +162,19 @@ public ServerResponse user( final ServerRequest serverRequest,
//@Consumes("multipart/form-data")
// @ApiImplicitParams({ @ApiImplicitParam(dataType = "com.wurrly.models.User", name = "user", paramType = "body", required = false, allowMultiple = false) })
@ApiOperation(value = "Create a user", httpMethod = "POST", response = User.class)
public ServerResponse createUser( final ServerRequest serverRequest, @QueryParam("context") Optional<String> context, final User user )
public ServerResponse<?> createUser( final ServerRequest serverRequest,
@QueryParam("context") Optional<String> context,
final User user,
List<String> stringArgs,
Optional<List<String>> optionalStringArgs
)
{

ServerResponse response = null;
ServerResponse<?> response = null;

if( user != null )
{
response = response().ok().entity(user) ;
response = response(User.class).ok().entity(user) ;

}
else
Expand All @@ -181,23 +188,51 @@ public ServerResponse createUser( final ServerRequest serverRequest, @QueryPara

@PUT
@Path("/username")
@Consumes("application/json,application/xml")
@Produces("application/json,application/xml")
@Consumes(MediaType.APPLICATION_JSON + "," + MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_JSON + "," + MediaType.APPLICATION_XML)
// @ApiImplicitParams({ @ApiImplicitParam(dataType = "com.wurrly.models.User", name = "user", paramType = "body", required = false, allowMultiple = false) })
@ApiOperation(value = "Update a user's name", httpMethod = "PUT", response = User.class)
public CompletableFuture<ServerResponse> updateUsername(@ApiParam(hidden=true)final ServerRequest serverRequest, @QueryParam("context") Optional<String> context, final User user )
public CompletableFuture<ServerResponse<List<User>>> updateUsername(@ApiParam(hidden=true)final ServerRequest serverRequest, @QueryParam("context") Optional<String> context, final List<User> users )
{
//
log.debug("esIndexName: " + esIndexName);
log.debug("context: " + context);
log.debug("request: " + serverRequest);
log.debug("file: " + user);
log.debug("file: " + users);


return CompletableFuture.completedFuture(response().entity(user));
return CompletableFuture.completedFuture(response(users));

}

@GET
@Path("/empty")
// @ApiImplicitParams({ @ApiImplicitParam(dataType = "com.wurrly.models.User", name = "user", paramType = "body", required = false, allowMultiple = false) })
@ApiOperation(value = "Test an empty control endpoint", httpMethod = "GET" )
public CompletableFuture<ServerResponse<ImmutableMap>> empty()
{
//

return CompletableFuture.completedFuture(response(ImmutableMap.class).entity( ImmutableMap.of("empty", "success") ));

}

@GET
@Path("/raw")
@ApiOperation(value = "Test a raw control endpoint", httpMethod = "GET" )
public void raw(HttpServerExchange exchange)
{
exchange.getResponseSender().send("end");

}

@GET
@Path("/raw2")
@ApiOperation(value = "Test a raw control endpoint", httpMethod = "GET" )
public void raw2(HttpServerExchange exchange)
{
response("end").textPlain().send(exchange);

}

}
Loading

0 comments on commit 756d337

Please sign in to comment.