All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.reactivex.netty.protocol.http.server.HttpServerBuilder Maven / Gradle / Ivy

There is a newer version: 0.3.18
Show newest version
/*
 * Copyright 2014 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.reactivex.netty.protocol.http.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;
import io.reactivex.netty.channel.RxDefaultThreadFactory;
import io.reactivex.netty.metrics.MetricEventsListener;
import io.reactivex.netty.metrics.MetricEventsListenerFactory;
import io.reactivex.netty.pipeline.PipelineConfigurators;
import io.reactivex.netty.server.ConnectionBasedServerBuilder;
import io.reactivex.netty.server.RxServer;
import io.reactivex.netty.server.ServerMetricsEvent;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * A convenience builder to create instances of {@link HttpServer}
 *
 * @author Nitesh Kant
 */
public class HttpServerBuilder
        extends ConnectionBasedServerBuilder, HttpServerResponse, HttpServerBuilder> {

    private long contentSubscriptionTimeoutMs;

    public HttpServerBuilder(int port, RequestHandler requestHandler, boolean send10ResponseFor10Request) {
        super(port, new HttpConnectionHandler(requestHandler, send10ResponseFor10Request));
        pipelineConfigurator(PipelineConfigurators.httpServerConfigurator());
    }

    public HttpServerBuilder(ServerBootstrap bootstrap, int port, RequestHandler requestHandler) {
        super(port, new HttpConnectionHandler(requestHandler), bootstrap);
        pipelineConfigurator(PipelineConfigurators.httpServerConfigurator());
    }

    public HttpServerBuilder(int port, RequestHandler requestHandler) {
        super(port, new HttpConnectionHandler(requestHandler));
        pipelineConfigurator(PipelineConfigurators.httpServerConfigurator());
    }

    public HttpServerBuilder(ServerBootstrap bootstrap, int port, RequestHandler requestHandler,
                             boolean send10ResponseFor10Request) {
        super(port, new HttpConnectionHandler(requestHandler, send10ResponseFor10Request), bootstrap);
        pipelineConfigurator(PipelineConfigurators.httpServerConfigurator());
    }

    /**
     * If the passed executor is not {@code null} , the configured {@link RequestHandler} will be invoked in the passed
     * {@link EventExecutorGroup}
     *
     * @param eventExecutorGroup The {@link EventExecutorGroup} in which to invoke the configured
     *                           {@link RequestHandler}. Can be {@code null}, in which case, the
     *                           {@link RequestHandler} is invoked in the channel's eventloop.
     *
     * @return This builder.
     */
    @Override
    public HttpServerBuilder withEventExecutorGroup(EventExecutorGroup eventExecutorGroup) {
        return super.withEventExecutorGroup(eventExecutorGroup);
    }

    /**
     * Same as calling {@link #withRequestProcessingThreads(int, ThreadFactory)} with {@link RxDefaultThreadFactory}
     *
     * @param threadCount Number of threads to use for request processing.
     *
     * @return This builder.
     */
    public HttpServerBuilder withRequestProcessingThreads(int threadCount) {
        return super.withEventExecutorGroup(new DefaultEventExecutorGroup(threadCount, new RxDefaultThreadFactory("rx-request-processor")));
    }

    /**
     * Same as calling {@link #withEventExecutorGroup(EventExecutorGroup)} with {@link DefaultEventExecutorGroup} using
     * the passed {@code factory}
     *
     * @param threadCount Number of threads to use for request processing.
     * @param factory Thread factory to use for the {@link DefaultEventExecutorGroup}
     *
     * @return This builder.
     */
    public HttpServerBuilder withRequestProcessingThreads(int threadCount, ThreadFactory factory) {
        return super.withEventExecutorGroup(new DefaultEventExecutorGroup(threadCount, factory));
    }

    /**
     * {@link HttpServerRequest} does not allow unlimited delayed content subscriptions.
     * This method specifies the timeout for the subscription of the content.
     *
     * @param subscriptionTimeout Timeout value.
     * @param timeunit Timeout time unit.
     *
     * @return This builder.
     */
    public HttpServerBuilder withRequestContentSubscriptionTimeout(long subscriptionTimeout, TimeUnit timeunit) {
        contentSubscriptionTimeoutMs = TimeUnit.MILLISECONDS.convert(subscriptionTimeout, timeunit);
        return this;
    }

    @Override
    public HttpServer build() {
        return (HttpServer) super.build();
    }

    @Override
    protected HttpServer createServer() {
        return new HttpServer(serverBootstrap, port, pipelineConfigurator,
                                    (HttpConnectionHandler) connectionHandler, eventExecutorGroup,
                                    contentSubscriptionTimeoutMs);
    }

    @Override
    protected MetricEventsListener>
    newMetricsListener(MetricEventsListenerFactory factory, RxServer, HttpServerResponse> server) {
        return factory.forHttpServer((HttpServer) server);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy