use of com.linkedin.parseq.EngineBuilder 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();
}
};
}
use of com.linkedin.parseq.EngineBuilder in project rest.li by linkedin.
the class RestliServlet method buildR2ServletFromServletParams.
private AbstractR2Servlet buildR2ServletFromServletParams(ServletConfig servletConfig) {
ResourceFactory resourceFactory = new PrototypeResourceFactory();
RestLiConfig config = new RestLiConfig();
config.setResourcePackageNamesSet(getResourcePackageSet(servletConfig));
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(getParseqThreadPoolSize(servletConfig));
Engine engine = new EngineBuilder().setTaskExecutor(scheduler).setTimerScheduler(scheduler).build();
final RestLiServer restLiServer = new RestLiServer(config, resourceFactory, engine);
DelegatingTransportDispatcher dispatcher = new DelegatingTransportDispatcher(restLiServer, restLiServer);
boolean useAsync = getUseAsync(servletConfig);
long asyncTimeOut = getAsyncTimeout(servletConfig);
if (useAsync && servletConfig.getServletContext().getMajorVersion() < 3) {
throw new IllegalArgumentException("This servlet is configured with useAsync=true, but the current servlet " + "context does not support the required Servlet API 3.0.");
}
if (!useAsync) {
log.info("Initializing Rest.li with a thread based request handling. Set useAsync=true on a Servlet API 3.0 container to enable Rest.li's async servlet.");
return new RAPServlet(dispatcher);
} else {
log.info("Initializing Rest.li with an async request handling enabled.");
return new AsyncR2Servlet(dispatcher, asyncTimeOut);
}
}
use of com.linkedin.parseq.EngineBuilder in project rest.li by linkedin.
the class TestAsyncMethodInvocationPlanClass method engine.
private Engine engine(SettablePromise<Trace> traceHolder) {
ExecutorService taskScheduler = Executors.newFixedThreadPool(1);
ScheduledExecutorService timerScheduler = Executors.newSingleThreadScheduledExecutor();
return new EngineBuilder().setTaskExecutor(taskScheduler).setTimerScheduler(timerScheduler).setPlanCompletionListener(planCtx -> {
try {
traceHolder.done(planCtx.getRootTask().getTrace());
} catch (PromiseResolvedException e) {
// this is expected in MULTIPLE_PLANS mux mode
}
}).build();
}
use of com.linkedin.parseq.EngineBuilder in project rest.li by linkedin.
the class TestRestLiMethodInvocation method setUp.
@BeforeTest
protected void setUp() {
_scheduler = Executors.newSingleThreadScheduledExecutor();
_engine = new EngineBuilder().setTaskExecutor(_scheduler).setTimerScheduler(_scheduler).build();
_resourceFactory = new EasyMockResourceFactory();
_errorResponseBuilder = new ErrorResponseBuilder();
_methodAdapterProvider = new DefaultMethodAdapterProvider(_errorResponseBuilder);
// Add filters to the invoker.
_invoker = new RestLiMethodInvoker(_resourceFactory, _engine, ErrorResponseBuilder.DEFAULT_INTERNAL_ERROR_MESSAGE);
}
use of com.linkedin.parseq.EngineBuilder in project rest.li by linkedin.
the class TestMultiplexedRequestHandlerImpl method createMultiplexer.
private static MultiplexedRequestHandlerImpl createMultiplexer(RestRequestHandler requestHandler, MultiplexerSingletonFilter multiplexerSingletonFilter, Set<String> individualRequestHeaderWhitelist, int maxRequestCount, MultiplexerRunMode multiplexerRunMode) {
ExecutorService taskScheduler = Executors.newFixedThreadPool(1);
ScheduledExecutorService timerScheduler = Executors.newSingleThreadScheduledExecutor();
Engine engine = new EngineBuilder().setTaskExecutor(taskScheduler).setTimerScheduler(timerScheduler).build();
return new MultiplexedRequestHandlerImpl(requestHandler, engine, maxRequestCount, individualRequestHeaderWhitelist, multiplexerSingletonFilter, multiplexerRunMode, new ErrorResponseBuilder());
}
Aggregations