Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit f4fdb89

Browse files
Add support for server worker executor.
1 parent 75e4551 commit f4fdb89

File tree

6 files changed

+97
-0
lines changed

6 files changed

+97
-0
lines changed

jooby-jetty/src/main/java/org/jooby/internal/jetty/JettyServer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.reflect.Method;
2323
import java.util.Arrays;
2424
import java.util.Map;
25+
import java.util.concurrent.Executor;
2526
import java.util.concurrent.TimeUnit;
2627
import java.util.function.Function;
2728
import java.util.stream.Collectors;
@@ -69,11 +70,13 @@ public class JettyServer implements org.jooby.spi.Server {
6970
private final Logger log = LoggerFactory.getLogger(org.jooby.spi.Server.class);
7071

7172
private Server server;
73+
private Executor executor;
7274

7375
@Inject
7476
public JettyServer(final HttpHandler handler, final Config conf,
7577
final Provider<SSLContext> sslCtx) {
7678
this.server = server(handler, conf, sslCtx);
79+
this.executor = this.server.getThreadPool();
7780
}
7881

7982
private Server server(final HttpHandler handler, final Config conf,
@@ -194,6 +197,12 @@ public void stop() throws Exception {
194197
server.stop();
195198
}
196199

200+
@Override
201+
public Executor executor()
202+
{
203+
return executor;
204+
}
205+
197206
private void tryOption(final Object source, final Config config, final Method option) {
198207
Try.run(() -> {
199208
String optionName = option.getName().replace("set", "");

jooby-netty/src/main/java/org/jooby/internal/netty/NettyServer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.lang.reflect.ParameterizedType;
2727
import java.util.Map;
2828
import java.util.Map.Entry;
29+
import java.util.concurrent.Executor;
2930
import java.util.concurrent.ThreadFactory;
3031
import java.util.function.BiConsumer;
3132

@@ -152,6 +153,12 @@ public void join() throws InterruptedException {
152153
ch.closeFuture().sync();
153154
}
154155

156+
@Override
157+
public Executor executor()
158+
{
159+
return executor;
160+
}
161+
155162
@SuppressWarnings({"rawtypes", "unchecked" })
156163
private void configure(final Config config, final String path,
157164
final BiConsumer<ChannelOption<Object>, Object> setter) {

jooby-undertow/src/main/java/org/jooby/internal/undertow/UndertowServer.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.lang.reflect.Field;
2222
import java.util.Map;
23+
import java.util.concurrent.Executor;
2324
import java.util.concurrent.TimeUnit;
2425
import java.util.function.BiConsumer;
2526
import java.util.function.Consumer;
@@ -54,6 +55,7 @@ private interface SetOption {
5455
private static final Logger log = LoggerFactory.getLogger(org.jooby.spi.Server.class);
5556

5657
private Undertow server;
58+
private Executor executor;
5759

5860
private final GracefulShutdownHandler shutdown;
5961

@@ -85,6 +87,8 @@ public UndertowServer(final org.jooby.spi.HttpHandler dispatcher, final Config c
8587

8688
this.server = ubuilder.setHandler(shutdown)
8789
.build();
90+
91+
this.executor = server.getWorker();
8892
}
8993

9094
private String host(final String host) {
@@ -206,4 +210,10 @@ public void stop() throws Exception {
206210
server.stop();
207211
}
208212

213+
@Override
214+
public Executor executor()
215+
{
216+
return executor;
217+
}
218+
209219
}

jooby/src/main/java/org/jooby/Jooby.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
import org.jooby.internal.ParameterNameProvider;
109109
import org.jooby.internal.RequestScope;
110110
import org.jooby.internal.RouteMetadata;
111+
import org.jooby.internal.ServerExecutorProvider;
111112
import org.jooby.internal.ServerLookup;
112113
import org.jooby.internal.ServerSessionManager;
113114
import org.jooby.internal.SessionManager;
@@ -2358,6 +2359,24 @@ public Jooby executor(final Executor executor) {
23582359
return this;
23592360
}
23602361

2362+
/**
2363+
* Set a named executor to use from {@link Deferred Deferred API}. Useful for override the
2364+
* default/global executor.
2365+
*
2366+
* Default executor runs each task in the thread that invokes {@link Executor#execute execute},
2367+
* that's a Jooby worker thread. A worker thread in Jooby can block.
2368+
*
2369+
* @param name Name of the executor.
2370+
* @param provider Provider for the executor.
2371+
* @return This jooby instance.
2372+
*/
2373+
public Jooby executor(final String name, final Class<? extends javax.inject.Provider<Executor>> provider) {
2374+
this.executors.add(binder -> {
2375+
binder.bind(Key.get(Executor.class, Names.named(name))).toProvider(provider).in(Singleton.class);
2376+
});
2377+
return this;
2378+
}
2379+
23612380
/**
23622381
* Set a named executor to use from {@link Deferred Deferred API}. Useful for override the
23632382
* default/global executor.
@@ -2565,6 +2584,7 @@ private Injector bootstrap(final Config args,
25652584
executor(MoreExecutors.directExecutor());
25662585
}
25672586
executor("direct", MoreExecutors.directExecutor());
2587+
executor("server", ServerExecutorProvider.class);
25682588

25692589
/** Some basic xss functions. */
25702590
xss(finalEnv);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.jooby.internal;
20+
21+
import org.jooby.spi.Server;
22+
23+
import javax.inject.Inject;
24+
import javax.inject.Provider;
25+
import java.util.concurrent.Executor;
26+
27+
import static java.util.Objects.requireNonNull;
28+
29+
public class ServerExecutorProvider implements Provider<Executor> {
30+
31+
private Executor executor;
32+
33+
@Inject
34+
public ServerExecutorProvider(final Server server) {
35+
executor = requireNonNull(server, "Server is required.").executor();
36+
}
37+
38+
@Override
39+
public Executor get() {
40+
return executor;
41+
}
42+
43+
}

jooby/src/main/java/org/jooby/spi/Server.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.jooby.spi;
2020

21+
import java.util.concurrent.Executor;
2122

2223
/**
2324
* A HTTP web server.
@@ -48,4 +49,11 @@ public interface Server {
4849
*/
4950
void join() throws InterruptedException;
5051

52+
/**
53+
* Obtain the executor for worker threads.
54+
*
55+
* @return The executor for worker threads.
56+
*/
57+
Executor executor();
58+
5159
}

0 commit comments

Comments
 (0)