Skip to content

Commit bdbbd3f

Browse files
committed
Merged branch 'jetty-12.0.x' into 'jetty-12.1.x'.
Signed-off-by: Simone Bordet <[email protected]>
2 parents be14c30 + b834b58 commit bdbbd3f

File tree

8 files changed

+101
-4
lines changed

8 files changed

+101
-4
lines changed

documentation/jetty/modules/code/examples/src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import org.eclipse.jetty.server.handler.DefaultHandler;
8686
import org.eclipse.jetty.server.handler.DoSHandler;
8787
import org.eclipse.jetty.server.handler.EventsHandler;
88+
import org.eclipse.jetty.server.handler.GracefulHandler;
8889
import org.eclipse.jetty.server.handler.QoSHandler;
8990
import org.eclipse.jetty.server.handler.ResourceHandler;
9091
import org.eclipse.jetty.server.handler.SecuredRedirectHandler;
@@ -1681,6 +1682,38 @@ public void defaultHandler() throws Exception
16811682
// end::defaultHandler[]
16821683
}
16831684

1685+
public void gracefulHandler() throws Exception
1686+
{
1687+
// tag::gracefulHandler[]
1688+
Server server = new Server();
1689+
1690+
// Install the GracefulHandler.
1691+
GracefulHandler gracefulHandler = new GracefulHandler();
1692+
server.setHandler(gracefulHandler);
1693+
1694+
// Set the Server stopTimeout to wait at most
1695+
// 10 seconds for existing requests to complete.
1696+
server.setStopTimeout(10_000);
1697+
1698+
// Add one web application.
1699+
class MyWebApp extends Handler.Abstract
1700+
{
1701+
@Override
1702+
public boolean handle(Request request, Response response, Callback callback) throws Exception
1703+
{
1704+
// Implement your web application.
1705+
callback.succeeded();
1706+
return true;
1707+
}
1708+
}
1709+
1710+
ContextHandler contextHandler = new ContextHandler(new MyWebApp(), "/app");
1711+
gracefulHandler.setHandler(contextHandler);
1712+
1713+
server.start();
1714+
// end::gracefulHandler[]
1715+
}
1716+
16841717
public void continue100()
16851718
{
16861719
// tag::continue100[]

documentation/jetty/modules/operations-guide/pages/modules/standard.adoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,19 @@ Jetty's configuration properties are identical across all versions of this modul
196196
include::{jetty-home}/modules/ee11-webapp.mod[tags=ini-template]
197197
----
198198

199+
[[graceful]]
200+
== Module `graceful`
201+
202+
The `graceful` module allows to shut down gracefully the Jetty server when it is stopped (see xref:start/index.adoc#stop[this section] for more information about stopping Jetty).
203+
204+
The `graceful` module installs the `GracefulHandler` at the root of the `Handler` tree; the `GracefulHandler` rejects new requests, but allows current requests to terminate within a configurable timeout, as explained in xref:programming-guide:server/http.adoc#handler-use-graceful[this section].
205+
206+
The module properties are:
207+
208+
----
209+
include::{jetty-home}/modules/graceful.mod[tags=documentation]
210+
----
211+
199212
[[http]]
200213
== Module `http`
201214

documentation/jetty/modules/operations-guide/pages/start/index.adoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,18 @@ The `jetty.server.stopAtShutdown` property configures a JVM shutdown hook that i
540540

541541
Obviously, the JVM can also be stopped with `kill -KILL <pid>` that exits the process abruptly without running the JVM shutdown hooks.
542542

543+
[[stop-graceful]]
544+
=== Stopping Gracefully
545+
546+
Stopping Jetty abruptly when there are active HTTP requests being handled may result in a variety or errors, because Jetty components that are used to handle requests are being stopped concurrently.
547+
548+
For example, when the Jetty thread pool is stopped, an attempt to submit a task would throw `RejectedExecutionException`; when a component is stopped, its fields may be nulled-out, resulting in a `NullPointerException` being thrown if the component is used; etc.
549+
550+
You can stop Jetty _gracefully_ by adding the `graceful` Jetty module (see xref:modules/standard.adoc#graceful[this section] for more information).
551+
552+
When Jetty is stopped, the `graceful` module organizes to reject new requests, but allows existing requests to finish within a configurable timeout; then closes all the connections, stops all the Jetty components, and then exits the JVM.
553+
In this way, existing requests are not responded with an error caused by the server stopping, provided they complete within the timeout.
554+
543555
[[stop-remote]]
544556
=== Stopping Jetty from Remote
545557

documentation/jetty/modules/programming-guide/pages/server/http.adoc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,41 @@ include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ht
13251325

13261326
For other denial-of-service protections, see also <<handler-use-qos,`QoSHandler`>> and <<handler-use-thread-limit,`ThreadLimitHandler`>>.
13271327

1328+
[[handler-use-graceful]]
1329+
==== GracefulHandler
1330+
1331+
`GracefulHandler` allows to stop the Jetty server in a graceful way, by rejecting new requests but allowing existing requests to complete within a configurable timeout.
1332+
1333+
In this way, existing requests can be completed normally rather than with failures caused by the fact that the server is stopping; for example, stopping the server causes all TCP connections to be closed, so trying to write a response will result in a failure.
1334+
1335+
In order to stop Jetty gracefully, you need the following:
1336+
1337+
* Install the `GracefulHandler`, typically just after the `Server` at the root of the `Handler` tree.
1338+
* Configure `Server.stopTimeout` to a positive value.
1339+
1340+
When the `Server` component is stopped, it will check the `Server.stopTimeout`, and if positive, it will initiate a graceful shutdown by notifying all components that implement the `Graceful` interface that the shutdown has been initiated.
1341+
1342+
`GracefulHandler` implements `Graceful`, so it will start rejecting new requests with status code `503 Service Available`, but will allow existing requests to complete for a period of time up to `Server.stopTimeout`.
1343+
1344+
When all existing requests have completed, the `Server` stops all ``Connector``s, closes all connections, and finally stops all the components.
1345+
1346+
This is how you configure and use `GracefulHandler`:
1347+
1348+
[,java,indent=0]
1349+
----
1350+
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=gracefulHandler]
1351+
----
1352+
1353+
The `Handler` tree structure looks like the following:
1354+
1355+
[,screen]
1356+
----
1357+
Server
1358+
└── GracefulHandler
1359+
└── ContextHandler
1360+
└── MyWebApp
1361+
----
1362+
13281363
[[handler-use-servlet]]
13291364
=== Servlet API Handlers
13301365

jetty-core/jetty-server/src/main/config/etc/jetty-graceful.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
<New id="GracefulHandler" class="org.eclipse.jetty.server.handler.GracefulHandler" />
1212
</Arg>
1313
</Call>
14+
<Set name="stopTimeout"><Property name="jetty.server.stopTimeout" default="5000"/></Set>
1415
</Configure>

jetty-core/jetty-server/src/main/config/etc/jetty.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
<!-- extra server options -->
9494
<!-- =========================================================== -->
9595
<Set name="stopAtShutdown"><Property name="jetty.server.stopAtShutdown" default="true"/></Set>
96-
<Set name="stopTimeout"><Property name="jetty.server.stopTimeout" default="5000"/></Set>
9796
<Set name="dumpAfterStart" property="jetty.server.dumpAfterStart"/>
9897
<Set name="dumpBeforeStop" property="jetty.server.dumpBeforeStop"/>
9998
<Set name="tempDirectory" property="jetty.server.tempDirectory"/>

jetty-core/jetty-server/src/main/config/modules/graceful.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@ server
1111

1212
[xml]
1313
etc/jetty-graceful.xml
14+
15+
[ini-template]
16+
# tag::documentation[]
17+
## The timeout, in milliseconds, to apply when stopping the server gracefully.
18+
# jetty.server.stopTimeout=5000
19+
# end::documentation[]
20+

jetty-core/jetty-server/src/main/config/modules/server.mod

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ etc/jetty.xml
9898
## Whether ctrl+c on the console gracefully stops the Jetty server
9999
# jetty.server.stopAtShutdown=true
100100

101-
## Timeout in ms to apply when stopping the server gracefully
102-
# jetty.server.stopTimeout=5000
103-
104101
## Dump the state of the Jetty server, components, and webapps after startup
105102
# jetty.server.dumpAfterStart=false
106103

0 commit comments

Comments
 (0)