如何解决延迟队列迭代器因 NoSuchElementException 而失败
我有一个包含大约 1000 条记录的延迟队列。我的用例是读取队列并将记录写入已过期和未过期的文件。所以我尝试使用迭代器下面的代码,但它在写入后因“NoSuchElementException”异常而失败文件中的几条记录。
private final BlockingQueue<DelayedRecord> retryMessages = new DelayQueue<DelayedRecord>();
FileWriter fw = null;
try {
if (retryMessages.size() > 0) {
fw = new FileWriter("/home/file.txt");
Iterator<DelayedRecord> rr = retryMessages.iterator();
while (rr.hasNext()) {
String filename = rr.next().getRecord();
fw.write(filename);
}
} else {
LOGGER.info("No records in the queue");
}
} catch (final Exception e) {
LOGGER.warn("Error reading queue: " + e.getMessage(),e);
} finally {
//close resources
try {
fw.close();
} catch (IOException e) {
e.printstacktrace();
}
有什么指点吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。