Search in sources :

Example 6 with HttpServer

use of com.linkedin.r2.transport.http.server.HttpServer in project rest.li by linkedin.

the class MockHttpServerFactory method create.

/**
 * Creates a {@link HttpServer} that contains a {@link RestLiServer} to be used for testing a set of Rest.li
 * resources.
 *
 * The {@link HttpServer} uses an empty {@link FilterChain} and uses "/" as the context path.
 *
 * If the server is run in async mode (by calling this function with the last parameter {@code true}), the
 * timeout used is {@link #ASYNC_TIMEOUT}.
 *
 * Both the async and sync servers will use {@link #NUM_THREADS} threads.
 *
 * @param port the port the server will run on on localhost
 * @param config the {@link RestLiConfig} to be used by the {@link RestLiServer}
 * @param beans beans you want to inject into your Rest.li resource.
 * @param enableAsync true if the server should be async, false otherwise
 */
private static HttpServer create(int port, RestLiConfig config, Map<String, ?> beans, boolean enableAsync) {
    final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(NUM_THREADS);
    final ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
    EngineBuilder engineBuilder = new EngineBuilder().setTaskExecutor(scheduler).setTimerScheduler(scheduler);
    com.linkedin.parseq.AsyncCallableTask.register(engineBuilder, executor);
    final Engine engine = engineBuilder.build();
    ResourceFactory resourceFactory = createResourceFactory(beans);
    TransportDispatcher dispatcher = new DelegatingTransportDispatcher(new RestLiServer(config, resourceFactory, engine));
    final FilterChain fc = FilterChains.empty().addLastRest(new SimpleLoggingFilter());
    final HttpServer server = new HttpServerFactory(fc).createServer(port, HttpServerFactory.DEFAULT_CONTEXT_PATH, NUM_THREADS, dispatcher, enableAsync ? HttpJettyServer.ServletType.ASYNC_EVENT : HttpJettyServer.ServletType.RAP, enableAsync ? ASYNC_TIMEOUT : -1);
    return new HttpServer() {

        @Override
        public void start() throws IOException {
            server.start();
        }

        @Override
        public void stop() throws IOException {
            server.stop();
            engine.shutdown();
            executor.shutdown();
            scheduler.shutdown();
        }

        @Override
        public void waitForStop() throws InterruptedException {
            server.waitForStop();
        }
    };
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RestLiServer(com.linkedin.restli.server.RestLiServer) DelegatingTransportDispatcher(com.linkedin.restli.server.DelegatingTransportDispatcher) FilterChain(com.linkedin.r2.filter.FilterChain) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) HttpServer(com.linkedin.r2.transport.http.server.HttpServer) ResourceFactory(com.linkedin.restli.server.resources.ResourceFactory) InjectMockResourceFactory(com.linkedin.restli.server.mock.InjectMockResourceFactory) DelegatingTransportDispatcher(com.linkedin.restli.server.DelegatingTransportDispatcher) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) EngineBuilder(com.linkedin.parseq.EngineBuilder) SimpleLoggingFilter(com.linkedin.r2.filter.logging.SimpleLoggingFilter) Engine(com.linkedin.parseq.Engine)

Example 7 with HttpServer

use of com.linkedin.r2.transport.http.server.HttpServer in project rest.li by linkedin.

the class RestLiExampleD2Server method main.

public static void main(String[] args) throws Exception {
    // write D2-related configuration to ZooKeeper
    // all the configuration here are permanent, no need to re-write as long as ZooKeeper still has the data
    // therefore in real case, do this "discovery" step separately
    final HttpServer server = RestLiExampleBasicServer.createServer();
    final ZooKeeperConnectionManager zkConn = createZkConn();
    startServer(server, zkConn);
    System.out.println("Example server with D2 is running with URL " + RestLiExampleBasicServer.getServerUrl() + ". Press any key to stop server.");
    System.in.read();
    stopServer(server, zkConn);
}
Also used : ZooKeeperConnectionManager(com.linkedin.d2.balancer.servers.ZooKeeperConnectionManager) HttpServer(com.linkedin.r2.transport.http.server.HttpServer)

Example 8 with HttpServer

use of com.linkedin.r2.transport.http.server.HttpServer in project rest.li by linkedin.

the class TestMockHttpServerFactory method testCreateUsingClassNames.

@Test
public void testCreateUsingClassNames() throws IOException, RemoteInvocationException {
    Set<Class<?>> resourceClasses = new HashSet<>();
    resourceClasses.add(PhotoResource.class);
    resourceClasses.add(AlbumResource.class);
    Map<String, Object> beans = getBeans();
    boolean[] enableAsyncOptions = { true, false };
    for (boolean enableAsync : enableAsyncOptions) {
        HttpServer server = MockHttpServerFactory.create(PORT, resourceClasses, beans, enableAsync);
        runTest(server);
    }
}
Also used : HttpServer(com.linkedin.r2.transport.http.server.HttpServer) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 9 with HttpServer

use of com.linkedin.r2.transport.http.server.HttpServer in project rest.li by linkedin.

the class HttpServerFactory method createHttpsH2cServer.

public HttpServer createHttpsH2cServer(int port, int sslPort, String keyStore, String keyStorePassword, String contextPath, int threadPoolSize, TransportDispatcher transportDispatcher, HttpJettyServer.ServletType servletType, int asyncTimeOut, boolean restOverStream) {
    final TransportDispatcher filterDispatcher = new FilterChainDispatcher(transportDispatcher, _filters);
    final HttpDispatcher dispatcher = HttpDispatcherFactory.create((filterDispatcher));
    return new HttpsH2JettyServer(port, sslPort, keyStore, keyStorePassword, contextPath, threadPoolSize, dispatcher, servletType, asyncTimeOut, restOverStream);
}
Also used : FilterChainDispatcher(com.linkedin.r2.filter.transport.FilterChainDispatcher) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher)

Example 10 with HttpServer

use of com.linkedin.r2.transport.http.server.HttpServer in project rest.li by linkedin.

the class HttpServerFactory method createRAPServer.

public HttpServer createRAPServer(int port, TransportDispatcher transportDispatcher, int timeout, boolean restOverStream) {
    final TransportDispatcher filterDispatcher = new FilterChainDispatcher(transportDispatcher, _filters);
    HttpServlet httpServlet = restOverStream ? new RAPStreamServlet(filterDispatcher, timeout, DEFAULT_LOG_SERVLET_EXCEPTIONS) : new RAPServlet(filterDispatcher);
    return new HttpJettyServer(port, httpServlet);
}
Also used : FilterChainDispatcher(com.linkedin.r2.filter.transport.FilterChainDispatcher) HttpServlet(javax.servlet.http.HttpServlet) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher)

Aggregations

TransportDispatcher (com.linkedin.r2.transport.common.bridge.server.TransportDispatcher)11 FilterChainDispatcher (com.linkedin.r2.filter.transport.FilterChainDispatcher)7 HttpServer (com.linkedin.r2.transport.http.server.HttpServer)6 HttpServerFactory (com.linkedin.r2.transport.http.server.HttpServerFactory)4 DelegatingTransportDispatcher (com.linkedin.restli.server.DelegatingTransportDispatcher)4 RestLiServer (com.linkedin.restli.server.RestLiServer)4 InjectMockResourceFactory (com.linkedin.restli.server.mock.InjectMockResourceFactory)4 ResourceFactory (com.linkedin.restli.server.resources.ResourceFactory)4 DefaultDocumentationRequestHandler (com.linkedin.restli.docgen.DefaultDocumentationRequestHandler)3 SimpleBeanProvider (com.linkedin.restli.server.mock.SimpleBeanProvider)3 Engine (com.linkedin.parseq.Engine)2 EngineBuilder (com.linkedin.parseq.EngineBuilder)2 GroupMembershipMgr (com.linkedin.restli.examples.groups.server.api.GroupMembershipMgr)2 GroupMgr (com.linkedin.restli.examples.groups.server.api.GroupMgr)2 HashGroupMembershipMgr (com.linkedin.restli.examples.groups.server.impl.HashGroupMembershipMgr)2 HashMapGroupMgr (com.linkedin.restli.examples.groups.server.impl.HashMapGroupMgr)2 GroupsRestApplication (com.linkedin.restli.examples.groups.server.rest.impl.GroupsRestApplication)2 ParseqTraceDebugRequestHandler (com.linkedin.restli.server.ParseqTraceDebugRequestHandler)2 RestLiConfig (com.linkedin.restli.server.RestLiConfig)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2