我在EJB.OK中创建线程有一个小问题我理解为什么我不能在EJB中使用它们,但不知道如何用相同的功能替换它们。我正在尝试下载30-40个网页/文件我需要同时(大约)开始下载所有文件。这是必要的,因为如果我在队列中的一个线程中运行它们。它将超过3分钟。
我尝试@Asyncronious anotation,但没有发生任何事情。
public void execute(String lang2, String lang1,int number) {
Stopwatch timer = new Stopwatch().start();
htmlCodes.add(URL2String(URLs.get(number)));
timer.stop();
System.out.println( number +":"+ Thread.currentThread().getName() + timer.elapsedMillis()+"miseconds");
}
private void findMatches(String searchedWord, String lang1, String lang2) {
articles = search(searchedWord);
for (int i = 0; i < articles.size(); i++) {
execute(lang1,lang2,i);
}
答案 0 :(得分:0)
以下是两个非常好的SO答案,可以提供帮助。 This one gives you your options和this one explains why you shouldn't spawn threads in an ejb。第一个答案的问题是它不包含很多关于EJB 3.0选项的知识。所以,here's a tutorial on using @Asynchronous.
没有冒犯,但我在您的代码中没有看到您已阅读本教程的任何证据。您的异步方法应返回Future
。 As the tutorial says:
客户端可以使用Future.get方法之一检索结果。如果处理调用的会话bean尚未完成处理,则调用其中一个get方法将导致客户端暂停执行,直到调用完成。在调用其中一个get方法之前,使用Future.isDone方法确定处理是否已完成。