- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我尝试使用 Unirest.get(...).asObjectAsync(...)
使用计划任务更新资源。要停止使用 Unirest 的程序,您需要调用 Unirest.shutdown();
以退出其事件循环和客户端。但是,如果某些线程在成功关闭后调用了Unirest的request方法,程序将无法退出。
以下代码是一个非常简单的示例:我启动了一个线程,该线程在 1.5 秒后执行 GET 请求,并在成功时打印状态消息。同时在主线程上,Unirest 被关闭。 (请注意,该示例使用 asStringAsync(...)
和一个非常简单的线程以简单起见。)
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();
}
}
我期望的是以下任何一种情况:
我得到的:
我如何处理 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>
最佳答案
在您的例子中,您生成了一个运行异步调用的线程。 shutdown()
调用在您的主线程中,因此在调用线程产生时,shutdown()
将在 之前被调用可以先调用Unirest的asStringAsync()
方法。
这是对实例化最终需要关闭的线程池的 ..Async()
的第一次调用 - 在您调用关闭时没有任何东西可以关闭方法,所以它是一个空操作。它将在您创建的线程中实例化。
这里的解决方案是删除您创建的线程,并使用 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();
}
关于java - unirest shutdown 退出程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32873070/
假设我的 Linux 系统中运行着 20 个进程/deqamons,与 SHUTDOWN 相比,HALT 对我的进程/守护进程的影响有何不同 最佳答案 通常,人们使用 shutdown command
我无法理解差异。帮我看看这个区别。而ProcessPoolExecutor呢,他的行为是一样的吗? def func(task): do_something(task) tasks = [ta
在某个时候,在代码推送后不久,我们看到我们的 Web 应用程序多次重启,但没有任何日志记录表明存在任何问题。所以我找到了这篇文章:http://weblogs.asp.net/scottgu/4331
这个问题已经有答案了: Difference between shutdown and shutdownNow of Executor Service (3 个回答) 已关闭 4 年前。 以下是Exe
这个问题在这里已经有了答案: close vs shutdown socket? (9 个回答) 关闭 5 年前。 在此 MSDN 页面上: Sending and Receiving Data o
我想知道使用elasticsearch shutdown API和终止elasticsearch进程(ctrl + c)之间的区别。我实际上不以正常关闭为答案,因为我想知道发出关闭API调用时ES实际
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
Java 定时器调度任务:servive JVM 关闭? Timer timer = new Timer(); timer.scheduleAtFixedRate(...) JVM关闭后,调度的任务还
这个问题已经有答案了: Difference between shutdown and shutdownNow of Executor Service (3 个回答) 已关闭 5 年前。 我正在检查j
我正在 Windows 10 上的 CPython 3.7 中实现一个 Server 类,其中使用永久启动服务的 Server.serve 方法和 Server.shutdown code> 停止服务
为了避免延长等待时间,建议您在调用 awaitTermination() 之前先调用 shutdown(),因为后者不适合杀死执行者。但是,为了等待任务完成,您是否应该使用 invokeAll() 来
我正在用 Python 为老式计算机系统编写一个模拟器,在摆脱停止状态后尝试“重新启动”模拟器核心线程时,我遇到了一些抛出异常的问题。 “运行处理器”方法是一个更大类的一部分,如下所示: def ru
我有以下代码,用于在端口 8000 上运行本地 HTTP 服务器: # Omitted imports and irrelevant things for brevity. class Threade
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util
我制作了一个程序,它会在设定的时间后关闭 PC。用户使用 2 个旋转框(一个用于小时,另一个用于分钟)输入时间,然后我将它转换为毫秒: ms = (ui->hBox->text().toInt() *
class System { public: virtual ~System() final { Shutdown(); }; virtual void Shu
看下面的代码: public void pinger() { ScheduledThreadPoolExecutor executor = new Schedule
我开发了一个小型 Java 应用程序,在其中使用 Quartz.jar 库。 但是当执行我的应用程序并调用 Quartz 时,我在控制台中收到以下错误消息: org.quartz.SchedulerE
我有 parking 系统的代码。当空格数超过 20 时,它会发送错误消息,因为 20 是空格的限制。我想在此之后尝试关闭程序。 我已尝试做您可以在 Python 中做的事情。这是: import s
我们在 RHEL (jdk 1.7.. 51) 上使用 Neo4j2.0.1 - 我们在一个同步线程上调用 GraphDatabaseService.shutDown(),并且此方法永远不会返回或引发
我是一名优秀的程序员,十分优秀!