diff --git a/jetty-core/jetty-alpn/jetty-alpn-conscrypt-client/src/test/java/org/eclipse/jetty/alpn/java/client/ConscryptHTTP2ClientTest.java b/jetty-core/jetty-alpn/jetty-alpn-conscrypt-client/src/test/java/org/eclipse/jetty/alpn/java/client/ConscryptHTTP2ClientTest.java index f1a7d1938f00..d5623ef5733d 100644 --- a/jetty-core/jetty-alpn/jetty-alpn-conscrypt-client/src/test/java/org/eclipse/jetty/alpn/java/client/ConscryptHTTP2ClientTest.java +++ b/jetty-core/jetty-alpn/jetty-alpn-conscrypt-client/src/test/java/org/eclipse/jetty/alpn/java/client/ConscryptHTTP2ClientTest.java @@ -57,8 +57,7 @@ public void testConscryptHTTP2Client() throws Exception sslContextFactory.setProvider("Conscrypt"); Conscrypt.setDefaultHostnameVerifier((certs, hostname, session) -> true); - HTTP2Client client = new HTTP2Client(); - try + try (HTTP2Client client = new HTTP2Client()) { client.addBean(sslContextFactory); client.start(); @@ -97,10 +96,6 @@ public void onDataAvailable(Stream stream) assertTrue(latch.await(15, TimeUnit.SECONDS)); } - finally - { - client.stop(); - } } private boolean canConnectTo(String host, int port) diff --git a/jetty-core/jetty-alpn/jetty-alpn-conscrypt-server/src/test/java/org/eclipse/jetty/alpn/conscrypt/server/ConscryptHTTP2ServerTest.java b/jetty-core/jetty-alpn/jetty-alpn-conscrypt-server/src/test/java/org/eclipse/jetty/alpn/conscrypt/server/ConscryptHTTP2ServerTest.java index 3e3280764750..10c5dfa18659 100644 --- a/jetty-core/jetty-alpn/jetty-alpn-conscrypt-server/src/test/java/org/eclipse/jetty/alpn/conscrypt/server/ConscryptHTTP2ServerTest.java +++ b/jetty-core/jetty-alpn/jetty-alpn-conscrypt-server/src/test/java/org/eclipse/jetty/alpn/conscrypt/server/ConscryptHTTP2ServerTest.java @@ -132,17 +132,12 @@ public void testSimpleRequest() throws Exception ClientConnector clientConnector = new ClientConnector(); clientConnector.setSslContextFactory(newClientSslContextFactory()); HTTP2Client h2Client = new HTTP2Client(clientConnector); - HttpClient client = new HttpClient(new HttpClientTransportOverHTTP2(h2Client)); - client.start(); - try + try (HttpClient client = new HttpClient(new HttpClientTransportOverHTTP2(h2Client))) { + client.start(); int port = ((ServerConnector)server.getConnectors()[0]).getLocalPort(); ContentResponse contentResponse = client.GET("https://localhost:" + port); assertEquals(200, contentResponse.getStatus()); } - finally - { - client.stop(); - } } } diff --git a/jetty-core/jetty-alpn/jetty-alpn-java-client/src/test/java/org/eclipse/jetty/alpn/java/client/JDK9HTTP2ClientTest.java b/jetty-core/jetty-alpn/jetty-alpn-java-client/src/test/java/org/eclipse/jetty/alpn/java/client/JDK9HTTP2ClientTest.java index 14187c9c6333..d3508ec9aa7c 100644 --- a/jetty-core/jetty-alpn/jetty-alpn-java-client/src/test/java/org/eclipse/jetty/alpn/java/client/JDK9HTTP2ClientTest.java +++ b/jetty-core/jetty-alpn/jetty-alpn-java-client/src/test/java/org/eclipse/jetty/alpn/java/client/JDK9HTTP2ClientTest.java @@ -45,8 +45,7 @@ public void testJDK9HTTP2Client() throws Exception Assumptions.assumeTrue(canConnectTo(host, port)); - HTTP2Client client = new HTTP2Client(); - try + try (HTTP2Client client = new HTTP2Client()) { SslContextFactory.Client sslContextFactory = new SslContextFactory.Client(); client.addBean(sslContextFactory); @@ -87,10 +86,6 @@ public void onDataAvailable(Stream stream) latch.await(15, TimeUnit.SECONDS); } - finally - { - client.stop(); - } } private boolean canConnectTo(String host, int port) diff --git a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java index 7edbf36e20b2..b6c40a2eb1da 100644 --- a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java +++ b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java @@ -104,7 +104,7 @@ * } */ @ManagedObject("The HTTP client") -public class HttpClient extends ContainerLifeCycle +public class HttpClient extends ContainerLifeCycle implements AutoCloseable { public static final String USER_AGENT = "Jetty/" + Jetty.VERSION; private static final Logger LOG = LoggerFactory.getLogger(HttpClient.class); @@ -1141,4 +1141,10 @@ public ClientConnectionFactory newSslClientConnectionFactory(SslContextFactory.C sslContextFactory = getSslContextFactory(); return new SslClientConnectionFactory(sslContextFactory, getByteBufferPool(), getExecutor(), connectionFactory); } + + @Override + public void close() throws Exception + { + stop(); + } } diff --git a/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/jmx/HttpClientJMXTest.java b/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/jmx/HttpClientJMXTest.java index 6ab77392c3ea..02eb4a62f07e 100644 --- a/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/jmx/HttpClientJMXTest.java +++ b/jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/jmx/HttpClientJMXTest.java @@ -31,12 +31,11 @@ public class HttpClientJMXTest @Test public void testHttpClientName() throws Exception { - String name = "foo"; - HttpClient httpClient = new HttpClient(); - httpClient.setName(name); - - try + try (HttpClient httpClient = new HttpClient()) { + String name = "foo"; + httpClient.setName(name); + MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); MBeanContainer mbeanContainer = new MBeanContainer(mbeanServer); // Adding MBeanContainer as a bean will trigger the registration of MBeans. @@ -59,9 +58,5 @@ public void testHttpClientName() throws Exception assertEquals(name, oName.getKeyProperty("context")); } } - finally - { - httpClient.stop(); - } } } diff --git a/jetty-core/jetty-http2/jetty-http2-client/src/main/java/org/eclipse/jetty/http2/client/HTTP2Client.java b/jetty-core/jetty-http2/jetty-http2-client/src/main/java/org/eclipse/jetty/http2/client/HTTP2Client.java index b49687bac794..89d2139c0373 100644 --- a/jetty-core/jetty-http2/jetty-http2-client/src/main/java/org/eclipse/jetty/http2/client/HTTP2Client.java +++ b/jetty-core/jetty-http2/jetty-http2-client/src/main/java/org/eclipse/jetty/http2/client/HTTP2Client.java @@ -99,7 +99,7 @@ *} */ @ManagedObject -public class HTTP2Client extends ContainerLifeCycle +public class HTTP2Client extends ContainerLifeCycle implements AutoCloseable { private final ClientConnector connector; private int inputBufferSize = 8192; @@ -492,4 +492,10 @@ private ClientConnectionFactory newClientConnectionFactory(SslContextFactory.Cli } return factory; } + + @Override + public void close() throws Exception + { + stop(); + } } diff --git a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HttpClientTransportOverHTTP2Test.java b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HttpClientTransportOverHTTP2Test.java index 0173430314a3..94abef518747 100644 --- a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HttpClientTransportOverHTTP2Test.java +++ b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HttpClientTransportOverHTTP2Test.java @@ -106,27 +106,26 @@ public class HttpClientTransportOverHTTP2Test extends AbstractTest public void testPropertiesAreForwarded() throws Exception { HTTP2Client http2Client = new HTTP2Client(); - HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP2(http2Client)); - Executor executor = new QueuedThreadPool(); - httpClient.setExecutor(executor); - httpClient.setConnectTimeout(13); - httpClient.setIdleTimeout(17); - httpClient.setUseInputDirectByteBuffers(false); - httpClient.setUseOutputDirectByteBuffers(false); - - httpClient.start(); - - assertTrue(http2Client.isStarted()); - assertSame(httpClient.getExecutor(), http2Client.getExecutor()); - assertSame(httpClient.getScheduler(), http2Client.getScheduler()); - assertSame(httpClient.getByteBufferPool(), http2Client.getByteBufferPool()); - assertEquals(httpClient.getConnectTimeout(), http2Client.getConnectTimeout()); - assertEquals(httpClient.getIdleTimeout(), http2Client.getIdleTimeout()); - assertEquals(httpClient.isUseInputDirectByteBuffers(), http2Client.isUseInputDirectByteBuffers()); - assertEquals(httpClient.isUseOutputDirectByteBuffers(), http2Client.isUseOutputDirectByteBuffers()); - - httpClient.stop(); - + try (HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP2(http2Client))) + { + Executor executor = new QueuedThreadPool(); + httpClient.setExecutor(executor); + httpClient.setConnectTimeout(13); + httpClient.setIdleTimeout(17); + httpClient.setUseInputDirectByteBuffers(false); + httpClient.setUseOutputDirectByteBuffers(false); + + httpClient.start(); + + assertTrue(http2Client.isStarted()); + assertSame(httpClient.getExecutor(), http2Client.getExecutor()); + assertSame(httpClient.getScheduler(), http2Client.getScheduler()); + assertSame(httpClient.getByteBufferPool(), http2Client.getByteBufferPool()); + assertEquals(httpClient.getConnectTimeout(), http2Client.getConnectTimeout()); + assertEquals(httpClient.getIdleTimeout(), http2Client.getIdleTimeout()); + assertEquals(httpClient.isUseInputDirectByteBuffers(), http2Client.isUseInputDirectByteBuffers()); + assertEquals(httpClient.isUseOutputDirectByteBuffers(), http2Client.isUseOutputDirectByteBuffers()); + } assertTrue(http2Client.isStopped()); } @@ -833,16 +832,16 @@ public void testExternalServer() throws Exception { ClientConnector clientConnector = new ClientConnector(); HTTP2Client http2Client = new HTTP2Client(clientConnector); - SslContextFactory.Client sslContextFactory = new SslContextFactory.Client(); - clientConnector.setSslContextFactory(sslContextFactory); - HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP2(http2Client)); - Executor executor = new QueuedThreadPool(); - clientConnector.setExecutor(executor); - httpClient.start(); - - ContentResponse response = httpClient.GET("https://webtide.com/"); - assertEquals(HttpStatus.OK_200, response.getStatus()); - - httpClient.stop(); + try (HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP2(http2Client))) + { + Executor executor = new QueuedThreadPool(); + clientConnector.setExecutor(executor); + SslContextFactory.Client sslContextFactory = new SslContextFactory.Client(); + clientConnector.setSslContextFactory(sslContextFactory); + httpClient.start(); + + ContentResponse response = httpClient.GET("https://webtide.com/"); + assertEquals(HttpStatus.OK_200, response.getStatus()); + } } } diff --git a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/ResponseTrailerTest.java b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/ResponseTrailerTest.java index 81416f5e0405..eb38da17b8ee 100644 --- a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/ResponseTrailerTest.java +++ b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/ResponseTrailerTest.java @@ -73,10 +73,9 @@ public boolean handle(Request request, Response response, Callback callback) } }); - HTTP2Client http2Client = new HTTP2Client(); - http2Client.start(); - try + try (HTTP2Client http2Client = new HTTP2Client()) { + http2Client.start(); String host = "localhost"; int port = connector.getLocalPort(); InetSocketAddress address = new InetSocketAddress(host, port); @@ -116,9 +115,5 @@ public void onDataAvailable(Stream stream) assertNotNull(frame); assertTrue(frame.getMetaData().isResponse()); } - finally - { - http2Client.stop(); - } } } diff --git a/jetty-core/jetty-http3/jetty-http3-client/src/main/java/org/eclipse/jetty/http3/client/HTTP3Client.java b/jetty-core/jetty-http3/jetty-http3-client/src/main/java/org/eclipse/jetty/http3/client/HTTP3Client.java index 922c051ed3e4..a813de45f6c7 100644 --- a/jetty-core/jetty-http3/jetty-http3-client/src/main/java/org/eclipse/jetty/http3/client/HTTP3Client.java +++ b/jetty-core/jetty-http3/jetty-http3-client/src/main/java/org/eclipse/jetty/http3/client/HTTP3Client.java @@ -125,7 +125,7 @@ *
HTTP/3+QUIC support is experimental and not suited for production use. * APIs may change incompatibly between releases.
*/ -public class HTTP3Client extends ContainerLifeCycle +public class HTTP3Client extends ContainerLifeCycle implements AutoCloseable { public static final String CLIENT_CONTEXT_KEY = HTTP3Client.class.getName(); public static final String SESSION_LISTENER_CONTEXT_KEY = CLIENT_CONTEXT_KEY + ".listener"; @@ -217,4 +217,10 @@ public CompletableFuture