java - Java Spring Runnable 任务超时
问题描述
面对这样一个事实,当数据库不可用时,队列会因为任务停止运行而增长。为方法 run() 中执行的任务设置超时的最佳方法是什么?使用 ExecutorService 可能有一些好的方法吗?
@Service
public class AsyncWriter implements Writer, Runnable {
private LinkedBlockingQueue<Entry> queue = new LinkedBlockingQueue<>();
private volatile boolean terminate = false;
private AtomicInteger completedCounter = new AtomicInteger();
@PostConstruct
private void runAsyncWriter() {
Thread async = new Thread(this);
async.setName("Writer Thread");
async.setPriority(2);
async.start();
}
@Override
public void run() {
while (!terminate) {
try {
Entry entry = queue.take();
dao.save(entry);
completedCounter.incrementAndGet();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void write(Entry entry) {
queue.add(entry);
}
}
解决方案
也许你可以试试 RxJava https://www.baeldung.com/rx-java 你可以在 RxJava 中设置你的 aync 函数 超时
推荐阅读
- xml - 尝试将 shipfromlocationref 交换为 shiptolocationref 以获取以下 XML 并面临命名空间问题
- javascript - 使用 Promise 响应来自异步服务的 useContext
- rest - 通过 Web 应用程序进行动态 CRM 身份验证
- c++ - 如何使用 fifo 传递字符串对象(std::string)?
- javascript - 在 Electron 本地运行非内联 JS
- node.js - .react-scripts start : 'react-scripts' 不是内部或外部命令、可运行程序或批处理文件
- java - 如何打破约束圈并在Android的约束布局中重新初始化
- r - 计算每月在每个地点每次停留的平均天数 - dplyr
- sql-server - 基于模式的多租户
- fortran - 如何为 OpenSUSE Leap 15.0 获取 mpif90 编译器?