Search in sources :

Example 1 with JacksonSerializationProvider

use of io.servicetalk.data.jackson.JacksonSerializationProvider in project FrameworkBenchmarks by TechEmpower.

the class Server method main.

public static void main(String[] args) throws Exception {
    /*
         * Disable  AsyncContext
         */
    AsyncContext.disable();
    /*
         *   Factory to implement io pooling
         */
    IoExecutor ioExecutor = NettyIoExecutors.createIoExecutor(Runtime.getRuntime().availableProcessors(), new IoThreadFactory("io-pool"));
    /*
         * Factory to disable headers validation
         */
    DefaultHttpHeadersFactory headersFactory = new DefaultHttpHeadersFactory(false, false);
    HttpSerializationProvider serializer = HttpSerializationProviders.jsonSerializer(new JacksonSerializationProvider());
    // Create a custom server builder with performance enhancements
    HttpServers.forPort(8080).executionStrategy(HttpExecutionStrategies.noOffloadsStrategy()).ioExecutor(ioExecutor).disableDrainingRequestPayloadBody().protocols(HttpProtocolConfigs.h1().headersFactory(headersFactory).build()).appendConnectionAcceptorFilter(delegate -> new ConnectionAcceptor() {

        @Override
        public Completable accept(ConnectionContext context) {
            ((NettyConnectionContext) context).updateFlushStrategy((current, isOrig) -> FlushStrategies.flushOnEnd());
            return delegate.accept(context);
        }
    }).listenAndAwait((ctx, request, responseFactory) -> {
        ((NettyConnectionContext) ctx).updateFlushStrategy(((current, isCurrentOriginal) -> FlushStrategies.flushOnEach()));
        if (request.path().equals("/json")) {
            Map<String, String> obj = new HashMap<String, String>();
            obj.put("message", "Hello, World!");
            return succeeded(responseFactory.ok().payloadBody(obj, serializer.serializerFor(Map.class)).addHeader("Date", getCurrentTime()).addHeader("Server", "ServiceTalk"));
        }
        if (request.path().equals("/plaintext")) {
            return succeeded(responseFactory.ok().payloadBody("Hello, World!", HttpSerializationProviders.textSerializer()).addHeader("Date", getCurrentTime()).addHeader("Server", "ServiceTalk"));
        }
        ;
        return null;
    }).awaitShutdown();
}
Also used : io.servicetalk.transport.netty.internal(io.servicetalk.transport.netty.internal) ZonedDateTime(java.time.ZonedDateTime) Completable(io.servicetalk.concurrent.api.Completable) HttpProtocolConfigs(io.servicetalk.http.netty.HttpProtocolConfigs) HashMap(java.util.HashMap) ConnectionAcceptor(io.servicetalk.transport.api.ConnectionAcceptor) IoExecutor(io.servicetalk.transport.api.IoExecutor) JacksonSerializationProvider(io.servicetalk.data.jackson.JacksonSerializationProvider) AsyncContext(io.servicetalk.concurrent.api.AsyncContext) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Map(java.util.Map) DateTimeFormatter(java.time.format.DateTimeFormatter) ZoneOffset(java.time.ZoneOffset) io.servicetalk.http.api(io.servicetalk.http.api) HttpServers(io.servicetalk.http.netty.HttpServers) ConnectionContext(io.servicetalk.transport.api.ConnectionContext) ConnectionAcceptor(io.servicetalk.transport.api.ConnectionAcceptor) Completable(io.servicetalk.concurrent.api.Completable) IoExecutor(io.servicetalk.transport.api.IoExecutor) JacksonSerializationProvider(io.servicetalk.data.jackson.JacksonSerializationProvider) ConnectionContext(io.servicetalk.transport.api.ConnectionContext) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

AsyncContext (io.servicetalk.concurrent.api.AsyncContext)1 Completable (io.servicetalk.concurrent.api.Completable)1 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)1 JacksonSerializationProvider (io.servicetalk.data.jackson.JacksonSerializationProvider)1 io.servicetalk.http.api (io.servicetalk.http.api)1 HttpProtocolConfigs (io.servicetalk.http.netty.HttpProtocolConfigs)1 HttpServers (io.servicetalk.http.netty.HttpServers)1 ConnectionAcceptor (io.servicetalk.transport.api.ConnectionAcceptor)1 ConnectionContext (io.servicetalk.transport.api.ConnectionContext)1 IoExecutor (io.servicetalk.transport.api.IoExecutor)1 io.servicetalk.transport.netty.internal (io.servicetalk.transport.netty.internal)1 ZoneOffset (java.time.ZoneOffset)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1