Comments (9)
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
from unirest-java.
Can you try to call the close()
method on the HttpClient before terminating the application?
from unirest-java.
There is no close()
method existing on the HttpClient! The strange thing about this: The threads won't be created before calling Unirest.setHttpClient
. The pure creation of a new HttpClient works - but as soon as calling setHttpClient
these 8 threads appear (1 thread per CPU core).
What I've found out now:
//A call to the asyncClient shutdown stops these threads
ClientFactory.getAsyncClient().shutdown();
I'm not even using AsynClient!
For now I can do right at application startup:
try {
ClientFactory.getAsyncHttpClient().shutdown();
Unirest.setAsyncHttpClient(null);
} catch (InterruptedException e) {
}
The real error
Okay, I've further investigated and looked at the changes:
Previously the AsynClient was created in ClientFactory:
public static HttpAsyncClient getAsyncClient() {
if (asyncHttpClient == null) {
try {
asyncHttpClient = new DefaultHttpAsyncClient();
asyncHttpClient.start();
} catch (IOReactorException e) {
throw new RuntimeException(e);
}
asyncHttpClient = (HttpAsyncClient) Options.getOption(Option.ASYNCHTTPCLIENT);
}
return asyncHttpClient;
}
This changed to (Options.java)
CloseableHttpAsyncClient asyncClient = HttpAsyncClientBuilder.create().setDefaultRequestConfig(clientConfig).build();
asyncClient.start();
You can see the start()
method? The async client is started by Unirest regardless if the client is even used.
I suppose the asyncClient is never shutdown.
A quickfix would be lazy initialization but I think further work needs to be done here, to shutdown the client properly.
from unirest-java.
This issue isn't resolved. You accidentally closed it!
from unirest-java.
@dennisfischer You're right, I apologize for this, I committed a wrong message (I meant issue #12). In the meanwhile with commit eb342f1 I fixed the auto-start of the async client (if you don't need to make async requests, the async client won't start).
from unirest-java.
@thefosk Hey, you don't have to apologize for that mistake :) Happens 👍
The change you've made now looks working (untested).
The question is - how do you want to handle the shutdown of the async client now?
(I don't need this feature, but maybe others do)
from unirest-java.
I could introduce a shutdown method. Can you create a repo on GitHub with sample code demonstrating your scenario?
from unirest-java.
We're talking about the scenario: "App can't shutdown because AsyncClient was used"? - Sure, but need some time to do this. Currently very busy :D
from unirest-java.
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();
}
}
from unirest-java.
Related Issues (20)
- connectionTTL configuration ignored HOT 5
- Low level interceptor with shared context HOT 10
- bug query get with version 4 HOT 5
- Jackson MismatchedInputException for successful response with no body, after migrating to Unirest 4 HOT 2
- Automatic Retries not being used HOT 2
- MockClient: allow fine grained request verification or "un-expected" routes HOT 2
- Request cancellation (such as download cancellation) HOT 2
- Can't turn auto retries standard on HOT 10
- Incorrect Unirest version in README.md HOT 1
- Incorrect link to Interceptor file HOT 1
- Supported windows versions
- Configured HttpClient.Version ignored HOT 10
- MockClient: Mocking same route with different methods only invokes first registered/expected mocking response HOT 2
- multipart/form-data POST is missing content types HOT 6
- How to set read timeout duration HOT 4
- Can we add a response failure retry function? HOT 1
- Making header method to avoid redundant typing. HOT 2
- Make all HttpRequest.asObject work the same way HOT 2
- Strange behavior of response.getRequestSummary().getUrl() HOT 3
- Support the Partitioned flag on cookies HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from unirest-java.