Search in sources :

Example 6 with HttpServerFactory

use of com.linkedin.r2.transport.http.server.HttpServerFactory 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
   * @return
   */
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 HttpServerFactory

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

the class TestChannelPoolBehavior method setup.

@BeforeClass
public void setup() throws IOException {
    _scheduler = Executors.newSingleThreadScheduledExecutor();
    _clientFactory = new HttpClientFactory();
    _client1 = new TransportClientAdapter(_clientFactory.getClient(getClientProperties()), true);
    _client2 = new TransportClientAdapter(_clientFactory.getClient(getClientProperties()), true);
    _server = new HttpServerFactory().createServer(PORT, getTransportDispatcher(), true);
    _server.start();
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) BeforeClass(org.testng.annotations.BeforeClass)

Example 8 with HttpServerFactory

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

the class TestClientShutdown method testShutdown.

@Test(dataProvider = "configs")
public void testShutdown(boolean clientROS, boolean serverROS, String protocolVersion) throws Exception {
    _clientFactory = new HttpClientFactory();
    Map<String, String> clientProperties = new HashMap<String, String>();
    // very long shutdown timeout
    clientProperties.put(HttpClientFactory.HTTP_SHUTDOWN_TIMEOUT, "60000");
    clientProperties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, protocolVersion);
    _client = new TransportClientAdapter(_clientFactory.getClient(clientProperties), clientROS);
    TransportDispatcher dispatcher = new TransportDispatcherBuilder().addRestHandler(ECHO_URI, new EchoHandler()).build();
    _server = new HttpServerFactory(HttpJettyServer.ServletType.RAP).createH2cServer(PORT, dispatcher, serverROS);
    try {
        _server.start();
        RestRequestBuilder builder = new RestRequestBuilder(URI.create("http://localhost:" + PORT + ECHO_URI));
        byte[] content = new byte[100];
        builder.setEntity(content);
        Future<RestResponse> future = _client.restRequest(builder.build());
        RestResponse response = future.get(30, TimeUnit.SECONDS);
        Assert.assertEquals(response.getEntity().copyBytes(), content);
        final FutureCallback<None> clientShutdownCallback = new FutureCallback<None>();
        _client.shutdown(clientShutdownCallback);
        // we should catch those clients that do not shutdown properly in 5 seconds
        clientShutdownCallback.get(5000, TimeUnit.MILLISECONDS);
        final FutureCallback<None> factoryShutdownCallback = new FutureCallback<None>();
        _clientFactory.shutdown(factoryShutdownCallback);
        factoryShutdownCallback.get();
    } finally {
        if (_server != null) {
            _server.stop();
            _server.waitForStop();
        }
    }
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) TransportDispatcherBuilder(com.linkedin.r2.transport.common.bridge.server.TransportDispatcherBuilder) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 9 with HttpServerFactory

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

the class TestJetty404 method setup.

@BeforeClass
public void setup() throws IOException {
    _clientFactory = new HttpClientFactory();
    _client = new TransportClientAdapter(_clientFactory.getClient(Collections.<String, String>emptyMap()), true);
    _server = new HttpServerFactory().createH2cServer(PORT, "/correct-path", 50, new TransportDispatcher() {

        @Override
        public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
            callback.onResponse(TransportResponseImpl.success(new RestResponseBuilder().build()));
        }

        @Override
        public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
            req.getEntityStream().setReader(new DrainReader());
            callback.onResponse(TransportResponseImpl.success(new StreamResponseBuilder().build(EntityStreams.emptyStream())));
        }
    }, true);
    _server.start();
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) RestRequest(com.linkedin.r2.message.rest.RestRequest) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) RequestContext(com.linkedin.r2.message.RequestContext) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) Map(java.util.Map) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) BeforeClass(org.testng.annotations.BeforeClass)

Example 10 with HttpServerFactory

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

the class TestDisruptor method setup.

@BeforeClass
public void setup() throws IOException {
    final TransportDispatcher dispatcher = new TransportDispatcherBuilder(REST_OVER_STREAM).addRestHandler(URI.create(PATH), new AbstractHttpServerTest.HeaderEchoHandler()).build();
    final HttpServerFactory factory = new HttpServerFactory();
    _server = factory.createH2cServer(PORT, dispatcher, REST_OVER_STREAM);
    _server.start();
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) TransportDispatcherBuilder(com.linkedin.r2.transport.common.bridge.server.TransportDispatcherBuilder) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

HttpServerFactory (com.linkedin.r2.transport.http.server.HttpServerFactory)14 TransportDispatcher (com.linkedin.r2.transport.common.bridge.server.TransportDispatcher)13 TransportClientAdapter (com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter)8 HttpClientFactory (com.linkedin.r2.transport.http.client.HttpClientFactory)7 TransportDispatcherBuilder (com.linkedin.r2.transport.common.bridge.server.TransportDispatcherBuilder)6 BeforeClass (org.testng.annotations.BeforeClass)6 RequestContext (com.linkedin.r2.message.RequestContext)5 RestRequest (com.linkedin.r2.message.rest.RestRequest)5 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)5 HashMap (java.util.HashMap)5 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)4 StreamRequestHandler (com.linkedin.r2.transport.common.StreamRequestHandler)4 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)4 Map (java.util.Map)4 ByteString (com.linkedin.data.ByteString)3 FilterChain (com.linkedin.r2.filter.FilterChain)3 RestResponse (com.linkedin.r2.message.rest.RestResponse)3 DrainReader (com.linkedin.r2.message.stream.entitystream.DrainReader)3 DelegatingTransportDispatcher (com.linkedin.restli.server.DelegatingTransportDispatcher)3 RestLiServer (com.linkedin.restli.server.RestLiServer)3