在导出到Excel文件时,用户需要能够停止当前的导出操作。
有没有办法在Liferay中以编程方式执行此操作?
以下是预期的情况:
我正在使用Liferay 6.2和Excel我正在使用POI库。
P.S。导出操作向数据库发出多个请求,以便读取数据并将其写入excel文件(在循环中)。
答案 0 :(得分:0)
灵感来自我对How to cancel a running SQL query?
的回答public void export() {
// Store the current thread in the HttpSession
HttpSession httpSession = servletRequest.getSession()
httpSession.setAttribute("exportThread", Thread.currentThread());
try {
// Run your export loop
for (...) {
if (Thread.interrupted()) {
// Export was called
return;
}
// Do your export
...
}
} finally {
// Clear the session attribute, if it is still our thread
if (httpSession.getAttribute("exportThread") == Thread.currentThread()) {
httpSession.removeAttribute("exportThread");
}
}
}
public void cancel() {
// Get the thread from the HTTP session
HttpSession httpSession = servletRequest.getSession();
Thread exportThread = (Thread) httpSession.getAttribute("exportThread");
if (exportThread != null) {
// Cancel the previous export
exportThread.interrupt();
}
}
最大问题:会话不可序列化并附加了一个线程。但只要您不尝试序列化会话(例如在群集中),就没有问题。