如何在Liferay中以编程方式中断服务器请求



在导出到 Excel 文件时,用户需要能够停止当前的导出操作。

有没有办法在Liferay中以编程方式做到这一点?

下面是预期的方案:

  1. 用户点击"取消导出">
  2. 操作已取消

我正在使用Liferay 6.2,对于Excel,我正在使用POI库。

附言导出操作向数据库发出多个请求,以便读取数据并将其写入 excel 文件(循环(。

灵感来自我对如何取消正在运行的 SQL 查询的回答?

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();
}
}

最大的问题:会话无法通过附加的线程进行序列化。但只要您不尝试序列化会话(例如在集群中(,这就没有问题。

最新更新