Java Unirest关闭以退出程序

Java Unirest关闭以退出程序,java,unirest,Java,Unirest,我尝试使用Unirest.get(…).asObjectAsync(…)使用计划任务更新资源。要停止使用Unirest的程序,需要调用Unirest.shutdown()退出其事件循环和客户端。但是,如果某些线程在成功关闭后调用Unirest的请求方法,则程序无法退出 下面的代码是一个非常简单的示例:我启动一个线程,该线程在1.5秒后执行GET请求,并在成功时打印状态消息。同时,在主线程上,Unirest被关闭。(注意,这个示例使用了asStringAsync(…)和一个非常简单的线程来简化。)

我尝试使用
Unirest.get(…).asObjectAsync(…)
使用计划任务更新资源。要停止使用Unirest的程序,需要调用
Unirest.shutdown()
退出其事件循环和客户端。但是,如果某些线程在成功关闭后调用Unirest的请求方法,则程序无法退出

下面的代码是一个非常简单的示例:我启动一个线程,该线程在1.5秒后执行GET请求,并在成功时打印状态消息。同时,在主线程上,Unirest被关闭。(注意,这个示例使用了
asStringAsync(…)
和一个非常简单的线程来简化。)

import com.mashape.unirest.http.HttpResponse;
导入com.mashape.unirest.http.unirest;
导入com.mashape.unirest.http.async.Callback;
导入com.mashape.unirest.http.exceptions.uniresteption;
导入java.io.IOException;
公共班机{
公共静态void main(字符串…参数)引发IOException、InterruptedException{
新线程(()->{
试一试{
睡眠(1500);
}捕捉(中断异常e){
e、 printStackTrace();
}
Unirest.get(“http://example.orgasStringAsync(新回调(){
@凌驾
公共无效已完成(HttpResponse响应){
System.out.println(response.getStatusText());
}
@凌驾
public void失败(unireste异常){
System.out.println(“失败”);
}
@凌驾
公众假期取消(){
系统输出打印项次(“取消”);
}
});
}).start();
Unirest.shutdown();
}
}
我所期望的是这些案例中的任何一个:

  • 程序关闭,没有输出
  • 程序关闭,我得到以下任何输出:状态消息,失败或取消
  • 程序关闭但抛出异常,因为当GET请求发生时Unirest已经关闭
我得到的是:

  • 程序未关闭,GET请求成功,打印“OK”
如何使用Unirest处理优雅的退出?我是否应该重组计划(如果是,如何重组)

我在Windows上使用Java 8,运行IntelliJ Idea 14.1.5中的代码。 我使用的unirest依赖项是:

<dependency>
    <groupId>com.mashape.unirest</groupId>
    <artifactId>unirest-java</artifactId>
    <version>1.4.7</version>
</dependency>

com.mashape.unirest
unirest java
1.4.7

在您的例子中,您已经生成了一个运行异步调用的线程。
shutdown()

这是对实例化最终需要关闭的线程池的
.Async()
的第一次调用-在调用shutdown方法时没有任何东西需要关闭,因此它是禁止操作的。它将在您创建的线程中实例化

这里的解决方案是删除您创建的线程,并使用Unirest提供给您的
Future
对象。在执行异步调用时,Unirest处理线程本身,并且可以根据需要输入回调逻辑

    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 also use Future.isDone(), etc
    // System.out.println(httpResponse.getBody());
    Unirest.shutdown();
}
publicstaticvoidmain(String…args)抛出IOException、interruptedeexception、ExecutionException{
Future asyncCall=Unirest.get(“http://thecatapi.com/api/images/get?format=xml&results_per_page=20asStringAsync(新回调(){
@凌驾
公共无效已完成(HttpResponse响应){
System.out.println(response.getStatusText());
}
@凌驾
public void失败(unireste异常){
System.out.println(“失败”);
}
@凌驾
公众假期取消(){
系统输出打印项次(“取消”);
}
});
HttpResponse HttpResponse=asyncCall.get();//还可以使用Future.isDone()等
//System.out.println(httpResponse.getBody());
Unirest.shutdown();
}
asyncCall.get()将阻止调用,因此最好按以下操作:

asyncCall.thenAcceptAsync((result)->{   
if (null != result && !result.isEmpty()) {
        // do your business logic after response and then close Unirest
        Unirest.shutdown();
         }
    });
   }
但是在清理资源的情况下,您始终可以重用相同的创建资源,并注册一个关闭钩子,该钩子将在JVM关闭期间清理所有内容

参考:

asyncCall.thenAcceptAsync((result)->{   
if (null != result && !result.isEmpty()) {
        // do your business logic after response and then close Unirest
        Unirest.shutdown();
         }
    });
   }