【发布时间】:2019-07-15 06:58:05
【问题描述】:
我正在维护看起来像这样的代码
@Asynchronous
@TransactionTimeout(value = 1, unit = TimeUnit.HOUR)
public void downloadFile(Long fileId) {
//This method takes more than 1hour
service.download(fileId)
//this method should be called even when download finished with error
service.fileDownloadedFinishedNotification(fileId);
}
这只是一个示例代码,我们正在向fileDownloadedFinished 传递我们想要显示的消息等,在其中我们想要将进程标记为错误/成功完成。
正如您在下载时看到的那样,我们可以得到超时,然后fileDownloadedFinishedNotification 将不会被调用,因为事务由于超时而失败。
我正在考虑将通知提取到其他方法并这样调用它:
@Asynchronous
@TransactionTimeout(value = 1, unit = TimeUnit.HOUR)
public Future<String> downloadFile(Long fileId) {
//This method takes more than 1hour
service.download(fileId)
return new AsyncResult<String>("Test");
}
public void example(){
long id = 15;
String msg = "default stuff";
try {
msg = downloadFile(id).get();
}
catch (Exception e) {
e.printStackTrace();
}
service.fileDownloadedFinishedNotification(fileId, string);
}
但我不确定这是否是个好主意,或者可能还有其他一些功能,我可以在超时时调用它们。类似onTimeout。
【问题讨论】:
标签: java jakarta-ee jboss transactions ejb