java - Unirest shutdown to exit program -
i try update resources scheduled tasks using unirest.get(...).asobjectasync(...). stop program employing unirest, need call unirest.shutdown(); exit event loops , clients. however, if threads call unirest's request methods after successful shutdown, program can not exit.
the following code simple example: start thread request after 1.5 seconds , prints status message when successful. meanwhile on main thread, unirest shut down. (note example uses asstringasync(...) , simple thread simplicity.)
import com.mashape.unirest.http.httpresponse; import com.mashape.unirest.http.unirest; import com.mashape.unirest.http.async.callback; import com.mashape.unirest.http.exceptions.unirestexception; import java.io.ioexception; public class main { public static void main(string... args) throws ioexception, interruptedexception { new thread(() -> { try { thread.sleep(1500); } catch (interruptedexception e) { e.printstacktrace(); } unirest.get("http://example.org").asstringasync(new callback<string>() { @override public void completed(httpresponse<string> response) { system.out.println(response.getstatustext()); } @override public void failed(unirestexception e) { system.out.println("failed"); } @override public void cancelled() { system.out.println("cancelled"); } }); }).start(); unirest.shutdown(); } } what expected of these cases:
- the program shuts down , no output there.
- the program shuts down , of these outputs: status message, failed or cancelled.
- the program shuts down throws exception because unirest shut when request occurs.
what got:
- the program not shut down , request succeeds, printing "ok".
how can handle graceful exit unirest? should restructure program (and if so, how)?
i using java 8 on windows, running code inside intellij idea 14.1.5. unirest dependency use is:
<dependency> <groupid>com.mashape.unirest</groupid> <artifactid>unirest-java</artifactid> <version>1.4.7</version> </dependency>
in case, have spawned thread runs async call. shutdown() call in main thread, time call thread spawns, shutdown() have been called before asstringasync() method of unirest can first called.
it first call ..async() instantiates thread pool need shut down - there nothing shut down @ time call shutdown method, , no-op. instantiated in thread created.
the solution here remove thread have created, , make use future object unirest gives you. unirest handles thread when async call, , can input callback logic necessary.
public static void main(string... args) throws ioexception, interruptedexception, executionexception { future<httpresponse<string>> asynccall = unirest.get("http://thecatapi.com/api/images/get?format=xml&results_per_page=20").asstringasync(new callback<string>() { @override public void completed(httpresponse<string> response) { system.out.println(response.getstatustext()); } @override public void failed(unirestexception e) { system.out.println("failed"); } @override public void cancelled() { system.out.println("cancelled"); } }); httpresponse<string> httpresponse = asynccall.get(); // can use future.isdone(), etc // system.out.println(httpresponse.getbody()); unirest.shutdown(); }
Comments
Post a Comment