Git Product home page Git Product logo

Comments (9)

subnetmarco avatar subnetmarco commented on July 28, 2024 1

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.

subnetmarco avatar subnetmarco commented on July 28, 2024

Can you try to call the close() method on the HttpClient before terminating the application?

from unirest-java.

dennisfischer avatar dennisfischer commented on July 28, 2024

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.

dennisfischer avatar dennisfischer commented on July 28, 2024

This issue isn't resolved. You accidentally closed it!

from unirest-java.

subnetmarco avatar subnetmarco commented on July 28, 2024

@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.

dennisfischer avatar dennisfischer commented on July 28, 2024

@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.

subnetmarco avatar subnetmarco commented on July 28, 2024

I could introduce a shutdown method. Can you create a repo on GitHub with sample code demonstrating your scenario?

from unirest-java.

dennisfischer avatar dennisfischer commented on July 28, 2024

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.

dennisfischer avatar dennisfischer commented on July 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.