Java Code Examples for com.ning.http.client.AsyncHttpClient#close()
The following examples show how to use
com.ning.http.client.AsyncHttpClient#close() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestServerFixture.java From fixd with Apache License 2.0 | 6 votes |
@Test public void testSimpleGetWithRegexPathParam() throws Exception { server.handle(Method.GET, "/name/:name<[A-Za-z]+>") .with(200, "text/plain", "Hello :name"); Response resp = client.prepareGet("http://localhost:8080/name/Tim").execute().get(); assertEquals("Hello Tim", resp.getResponseBody().trim()); AsyncHttpClient otherClient = new AsyncHttpClient(); try { resp = otherClient.prepareGet("http://localhost:8080/name/123").execute().get(); assertEquals(404, resp.getStatusCode()); } finally { otherClient.close(); } }
Example 2
Source File: InvokeJaxrsResourceInTomcat.java From glowroot with Apache License 2.0 | 6 votes |
public void executeApp(String webapp, String contextPath, String url) throws Exception { int port = getAvailablePort(); Tomcat tomcat = new Tomcat(); tomcat.setBaseDir("target/tomcat"); tomcat.setPort(port); Context context = tomcat.addWebapp(contextPath, new File("src/test/resources/" + webapp).getAbsolutePath()); WebappLoader webappLoader = new WebappLoader(InvokeJaxrsResourceInTomcat.class.getClassLoader()); context.setLoader(webappLoader); tomcat.start(); AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); int statusCode = asyncHttpClient.prepareGet("http://localhost:" + port + contextPath + url) .execute().get().getStatusCode(); asyncHttpClient.close(); if (statusCode != 200) { throw new IllegalStateException("Unexpected status code: " + statusCode); } tomcat.stop(); tomcat.destroy(); }
Example 3
Source File: TestServerFixture.java From fixd with Apache License 2.0 | 6 votes |
@Test public void testSplatPathParametersOccurringMultipleTimes() throws Exception { server.handle(Method.GET, "/say/*/to/*") .with(200, "text/plain", "[request.path]"); AsyncHttpClient client1 = new AsyncHttpClient(); AsyncHttpClient client2 = new AsyncHttpClient(); AsyncHttpClient client3 = new AsyncHttpClient(); try { Response resp = client1.prepareGet("http://localhost:8080/hello").execute().get(); assertEquals(404, resp.getStatusCode()); resp = client2.prepareGet("http://localhost:8080/say/hello/to/world").execute().get(); assertEquals("/say/hello/to/world", resp.getResponseBody().trim()); resp = client3.prepareGet("http://localhost:8080/say/bye/to/Tim").execute().get(); assertEquals("/say/bye/to/Tim", resp.getResponseBody().trim()); } finally { client1.close(); client2.close(); client3.close(); } }
Example 4
Source File: NingAsyncHttpClientIT.java From pinpoint with Apache License 2.0 | 6 votes |
@Test public void test() throws Exception { AsyncHttpClient client = new AsyncHttpClient(); try { Future<Response> f = client.preparePost(webServer.getCallHttpUrl()).addParameter("param1", "value1").execute(); Response response = f.get(); } finally { client.close(); } PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); verifier.printCache(); String destinationId = webServer.getHostAndPort(); String httpUrl = webServer.getCallHttpUrl(); verifier.verifyTrace(event("ASYNC_HTTP_CLIENT", AsyncHttpClient.class.getMethod("executeRequest", Request.class, AsyncHandler.class), null, null, destinationId, annotation("http.url", httpUrl))); verifier.verifyTraceCount(0); }
Example 5
Source File: JettyHandlerIT.java From glowroot with Apache License 2.0 | 5 votes |
@Override public void executeApp() throws Exception { Server server = new Server(0); server.setHandler(new HelloHandler()); server.start(); int port = server.getConnectors()[0].getLocalPort(); AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); int statusCode = asyncHttpClient.prepareGet("http://localhost:" + port + "/hello") .execute().get().getStatusCode(); asyncHttpClient.close(); if (statusCode != 200) { throw new IllegalStateException("Unexpected status code: " + statusCode); } server.stop(); }
Example 6
Source File: TestServerFixture.java From fixd with Apache License 2.0 | 5 votes |
@Test public void testUponAndMarshallingWithoutHandler() throws Exception { server.marshal("application/json") .with(new JSONMarshaller()); server.handle(Method.GET, "/subscribe") .with(200, "application/json", new SimplePojo("marshalledJSON")) .upon(Method.PUT, "/broadcast"); final List<String> broadcasts = new ArrayList<String>(); ListenableFuture<Integer> f = client.prepareGet("http://localhost:8080/subscribe") .execute(new AddToListOnBodyPartReceivedHandler(broadcasts)); /* need some time for the above request to complete * before the broadcast requests can start */ Thread.sleep(100); for (int i = 0; i < 2; i++) { AsyncHttpClient otherClient = new AsyncHttpClient(); try { otherClient.preparePut("http://localhost:8080/broadcast").execute().get(); /* sometimes the last broadcast request is not * finished before f.done() is called */ Thread.sleep(200); } finally { otherClient.close(); } } f.done(null); assertEquals("[{\"val\":\"marshalledJSON\"}, {\"val\":\"marshalledJSON\"}]", broadcasts.toString()); }
Example 7
Source File: InvokeSpringControllerInTomcat.java From glowroot with Apache License 2.0 | 5 votes |
private void exec(String contextPath, String url, int port) throws InterruptedException, ExecutionException, IOException { AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); int statusCode = asyncHttpClient.prepareGet("http://localhost:" + port + contextPath + url) .execute().get().getStatusCode(); asyncHttpClient.close(); if (statusCode != 200) { throw new IllegalStateException("Unexpected status code: " + statusCode); } }
Example 8
Source File: AsyncServletIT.java From glowroot with Apache License 2.0 | 5 votes |
@Override protected void doTest(int port) throws Exception { AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); int statusCode = asyncHttpClient.prepareGet("http://localhost:" + port + contextPath + "/async3") .execute().get().getStatusCode(); asyncHttpClient.close(); if (statusCode != 200) { throw new IllegalStateException("Unexpected status code: " + statusCode); } }
Example 9
Source File: TestServerFixture.java From fixd with Apache License 2.0 | 5 votes |
@Test public void testUponUsingRequestBody() throws Exception { server.handle(Method.GET, "/subscribe") .with(200, "text/plain", "message: [request.body]") .upon(Method.PUT, "/broadcast"); final List<String> broadcasts = new ArrayList<String>(); ListenableFuture<Integer> f = client.prepareGet("http://localhost:8080/subscribe") .execute(new AddToListOnBodyPartReceivedHandler(broadcasts)); /* need some time for the above request to complete * before the broadcast requests can start */ Thread.sleep(200); for (int i = 0; i < 2; i++) { AsyncHttpClient otherClient = new AsyncHttpClient(); try { otherClient.preparePut("http://localhost:8080/broadcast") .setBody("hello" + i) .execute().get(); /* sometimes the last broadcast request is not * finished before f.done() is called */ Thread.sleep(200); } finally { otherClient.close(); } } f.done(null); assertEquals("[message: hello0, message: hello1]", broadcasts.toString()); }
Example 10
Source File: AsyncServletIT.java From glowroot with Apache License 2.0 | 5 votes |
@Override protected void doTest(int port) throws Exception { AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); int statusCode = asyncHttpClient.prepareGet("http://localhost:" + port + contextPath + "/async") .execute().get().getStatusCode(); asyncHttpClient.close(); if (statusCode != 200) { throw new IllegalStateException("Unexpected status code: " + statusCode); } }
Example 11
Source File: ServletDispatcherIT.java From glowroot with Apache License 2.0 | 5 votes |
@Override protected void doTest(int port) throws Exception { AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); int statusCode = asyncHttpClient .prepareGet("http://localhost:" + port + contextPath + "/first-include") .execute().get().getStatusCode(); asyncHttpClient.close(); if (statusCode != 200) { throw new IllegalStateException("Unexpected status code: " + statusCode); } }
Example 12
Source File: ServletDispatcherIT.java From glowroot with Apache License 2.0 | 5 votes |
@Override protected void doTest(int port) throws Exception { AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); int statusCode = asyncHttpClient .prepareGet( "http://localhost:" + port + contextPath + "/first-forward-using-named") .execute().get().getStatusCode(); asyncHttpClient.close(); if (statusCode != 200) { throw new IllegalStateException("Unexpected status code: " + statusCode); } }
Example 13
Source File: ServletDispatcherIT.java From glowroot with Apache License 2.0 | 5 votes |
@Override protected void doTest(int port) throws Exception { AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); int statusCode = asyncHttpClient .prepareGet("http://localhost:" + port + contextPath + "/first-forward") .execute().get().getStatusCode(); asyncHttpClient.close(); if (statusCode != 200) { throw new IllegalStateException("Unexpected status code: " + statusCode); } }
Example 14
Source File: TestServerFixture.java From fixd with Apache License 2.0 | 5 votes |
@Test public void testSettingMaxCapturedRequestsLimitsStoredCapturedRequests() throws Exception { server.handle(Method.GET, "/:id").with(200, "text/plain", ":id"); server.setMaxCapturedRequests(2); AsyncHttpClient client1 = new AsyncHttpClient(); AsyncHttpClient client2 = new AsyncHttpClient(); AsyncHttpClient client3 = new AsyncHttpClient(); try { client1.prepareGet("http://localhost:8080/1").execute().get(); client2.prepareGet("http://localhost:8080/2").execute().get(); client3.prepareGet("http://localhost:8080/3").execute().get(); assertEquals(2, server.capturedRequests().size()); CapturedRequest captured = server.request(); assertEquals("GET /2 HTTP/1.1", captured.getRequestLine()); captured = server.request(); assertEquals("GET /3 HTTP/1.1", captured.getRequestLine()); } finally { client1.close(); client2.close(); client3.close(); } }
Example 15
Source File: TestServerFixture.java From fixd with Apache License 2.0 | 5 votes |
@Test public void testUpon() throws Exception { server.handle(Method.GET, "/subscribe") .with(200, "text/plain", "message: :message") .upon(Method.GET, "/broadcast/:message"); final List<String> broadcasts = new ArrayList<String>(); ListenableFuture<Integer> f = client.prepareGet("http://localhost:8080/subscribe") .execute(new AddToListOnBodyPartReceivedHandler(broadcasts)); /* need some time for the above request to complete * before the broadcast requests can start */ Thread.sleep(200); for (int i = 0; i < 2; i++) { AsyncHttpClient otherClient = new AsyncHttpClient(); try { otherClient.prepareGet("http://localhost:8080/broadcast/hello" + i).execute().get(); /* sometimes the last broadcast request is not * finished before f.done() is called */ Thread.sleep(200); } finally { otherClient.close(); } } f.done(null); assertEquals("[message: hello0, message: hello1]", broadcasts.toString()); }
Example 16
Source File: AsyncHttpClientPluginIT.java From glowroot with Apache License 2.0 | 5 votes |
@Override public void transactionMarker() throws Exception { AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); final CountDownLatch latch = new CountDownLatch(1); final AtomicInteger statusCode = new AtomicInteger(); asyncHttpClient.prepareGet("http://localhost:" + getPort() + "/hello3/") .execute(new AsyncHandler<Response>() { @Override public STATE onBodyPartReceived(HttpResponseBodyPart part) { return null; } @Override public Response onCompleted() throws Exception { latch.countDown(); return null; } @Override public STATE onHeadersReceived(HttpResponseHeaders headers) { return null; } @Override public STATE onStatusReceived(HttpResponseStatus status) { statusCode.set(status.getStatusCode()); return null; } @Override public void onThrowable(Throwable t) {} }); latch.await(); asyncHttpClient.close(); if (statusCode.get() != 200) { throw new IllegalStateException("Unexpected status code: " + statusCode); } }
Example 17
Source File: AsyncHttpClientPluginIT.java From glowroot with Apache License 2.0 | 5 votes |
@Override public void transactionMarker() throws Exception { AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); int statusCode = asyncHttpClient.prepareGet("http://localhost:" + getPort() + "/hello1/") .execute().get().getStatusCode(); asyncHttpClient.close(); if (statusCode != 200) { throw new IllegalStateException("Unexpected status code: " + statusCode); } }
Example 18
Source File: JsfRenderIT.java From glowroot with Apache License 2.0 | 5 votes |
@Override protected void doTest(int port) throws Exception { AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); int statusCode = asyncHttpClient.prepareGet("http://localhost:" + port + "/hello.xhtml") .execute().get().getStatusCode(); asyncHttpClient.close(); if (statusCode != 200) { throw new IllegalStateException("Unexpected status code: " + statusCode); } }
Example 19
Source File: AsyncHttpClientTest.java From vw-webservice with BSD 3-Clause "New" or "Revised" License | 4 votes |
private void doTest(Request request) throws InterruptedException, ExecutionException, IOException { final PipedOutputStream pipedOutputStream = new PipedOutputStream(); final PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream); AsyncHandler<Response> asyncHandler = new AsyncHandler<Response>() { private final Response.ResponseBuilder builder = new Response.ResponseBuilder(); @Override public STATE onBodyPartReceived(final HttpResponseBodyPart content) throws Exception { content.writeTo(pipedOutputStream); return STATE.CONTINUE; } @Override public STATE onStatusReceived(final HttpResponseStatus status) throws Exception { builder.accumulate(status); return STATE.CONTINUE; } @Override public STATE onHeadersReceived(final HttpResponseHeaders headers) throws Exception { builder.accumulate(headers); return STATE.CONTINUE; } @Override public Response onCompleted() throws Exception { LOGGER.info("On complete called!"); pipedOutputStream.flush(); pipedOutputStream.close(); return builder.build(); } @Override public void onThrowable(Throwable arg0) { // TODO Auto-generated method stub LOGGER.error("Error: {}", arg0); onTestFailed(); } }; Future<Void> readingThreadFuture = Executors.newCachedThreadPool().submit(new Callable<Void>() { @Override public Void call() throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(pipedInputStream)); String readPrediction; int numPredictionsRead = 0; while ((readPrediction = reader.readLine()) != null) { //LOGGER.info("Got prediction: {}", readPrediction); numPredictionsRead++; } LOGGER.info("Read a total of {} predictions", numPredictionsRead); Assert.assertEquals(roundsOfDataToSubmit * 272274, numPredictionsRead); return null; } }); Builder config = new AsyncHttpClientConfig.Builder(); config.setRequestTimeoutInMs(-1); //need to set this to -1, to indicate wait forever. setting to 0 actually means a 0 ms timeout! AsyncHttpClient client = new AsyncHttpClient(config.build()); client.executeRequest(request, asyncHandler).get(); readingThreadFuture.get(); //verify no exceptions occurred when reading predictions client.close(); Assert.assertFalse(getTestFailed()); }
Example 20
Source File: TestServerFixture.java From fixd with Apache License 2.0 | 4 votes |
@Test public void testUponHandlesDifferentSubscriptions() throws Exception { server.handle(Method.GET, "/subscr1") .with(200, "text/plain", "message1") .upon(Method.GET, "/broadc1"); server.handle(Method.GET, "/subscr2") .with(200, "text/plain", "message2") .upon(Method.GET, "/broadc2"); AsyncHttpClient client1 = new AsyncHttpClient(); AsyncHttpClient client2 = new AsyncHttpClient(); try { final List<String> broadcasts1 = new ArrayList<String>(); ListenableFuture<Integer> f1 = client1.prepareGet("http://localhost:8080/subscr1") .execute(new AddToListOnBodyPartReceivedHandler(broadcasts1)); final List<String> broadcasts2 = new ArrayList<String>(); ListenableFuture<Integer> f2 = client2.prepareGet("http://localhost:8080/subscr2") .execute(new AddToListOnBodyPartReceivedHandler(broadcasts2)); /* need some time for the above request to complete * before the broadcast requests can start */ Thread.sleep(200); AsyncHttpClient otherClient = new AsyncHttpClient(); try { new AsyncHttpClient() .prepareGet("http://localhost:8080/broadc2") .execute().get(); /* sometimes the last broadcast request is not * finished before f.done() is called */ Thread.sleep(200); } finally { otherClient.close(); } f1.done(null); f2.done(null); assertEquals("[]", broadcasts1.toString()); assertEquals("[message2]", broadcasts2.toString()); } finally { client1.close(); client2.close(); } }