question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom HttpClient option blocks application shutdown

See original GitHub issue

Calling:

RequestConfig clientConfig = RequestConfig.custom().setConnectTimeout(600000).setSocketTimeout(600000).build();
Unirest.setHttpClient(HttpClientBuilder.create().setDefaultRequestConfig(clientConfig).build());

Which is the default configuration Unirest uses makes the application unable to exit. 8 Threads (I/O dispatcher 1-8) remain running.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
subnetmarcocommented, Dec 14, 2013

I’ve tested this locally and it seems to be fixed with new Unirest version 1.3.3.

Before terminating the application, close the event loop by executing Unirest.shutdown() first. In your example you can use it inside the completed callback function as well.

https://github.com/Mashape/unirest-java#exiting-an-application

0reactions
dennisfischercommented, Nov 10, 2013

2 things: I’ve written the example code and there’s a documentation mistake in the async example. Async examples method failed() has to use UnirestException

The example code - application will never terminate because of http-async-clients created threads:

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.async.Callback;
import com.mashape.unirest.http.exceptions.UnirestException;

import java.util.Map;
import java.util.concurrent.Future;

public class Main {
    public Main() {
        Future<HttpResponse<JsonNode>> future = Unirest.post("http://httpbin.org/post")
                .header("accept", "application/json")
                .field("param1", "value1")
                .field("param2", "value2")
                .asJsonAsync(new Callback<JsonNode>() {

                    public void failed(UnirestException e) {
                        System.out.println("The request has failed");
                    }

                    public void completed(HttpResponse<JsonNode> response) {
                        int code = response.getCode();
                        Map<String, String> headers = response.getHeaders();
                        JsonNode body = response.getBody();
                        System.out.println(body);
                    }

                    public void cancelled() {
                        System.out.println("The request has been cancelled");
                    }
                });
    }

    public static void main(String[] args) {
        new Main();
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

You're using HttpClient wrong and it is destabilizing your ...
Once the using block is complete then the disposable object, in this case HttpClient , goes out of scope and is disposed.
Read more >
Make HTTP requests using IHttpClientFactory in ASP.NET Core
Options. Use IHttpContextAccessor to access the current request. Create a custom AsyncLocal<T> storage object to pass the data. Use Polly-based ...
Read more >
Why is an OPTIONS request sent and can I disable it?
Chrome: Quit Chrome, open an terminal and paste this command: open /Applications/Google\ Chrome.app --args --disable-web-security --user-data-dir.
Read more >
Apache HttpClient Example - CloseableHttpClient | DigitalOcean
Create HttpGet or HttpPost instance based on the HTTP request type. ... Finally close the apache HttpClient resource.
Read more >
Don't use Go's default HTTP client (in production) - Medium
TL;DR: Go's http package doesn't specify request timeouts by default, allowing services to hijack your goroutines. Always specify a custom http.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found