0

I have a small problem with creating threads in EJB.OK I understand why i can not use them in EJB, but dont know how to replace them with the same functionality.I am trying to download 30-40 webpages/files and i need to start downloading of all files at the same time(approximately).This is need ,because if i run them in one thread in queue.It will excecute more than 3 minutes.

I try with @Asyncronious anotation, but nothing happened.

 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);
    }
Mihail
  • 5
  • 1

1 Answers1

0

Here are two really good SO answers that can help. This one gives you your options, and this one explains why you shouldn't spawn threads in an ejb. The problem with the first answer is it doesn't contain a lot of knowledge about EJB 3.0 options. So, here's a tutorial on using @Asynchronous.

No offense, but I don't see any evidence in your code that you've read this tutorial yet. Your asynchronous method should return a Future. As the tutorial says:

The client may retrieve the result using one of the Future.get methods. If processing hasn’t been completed by the session bean handling the invocation, calling one of the get methods will result in the client halting execution until the invocation completes. Use the Future.isDone method to determine whether processing has completed before calling one of the get methods.

Community
  • 1
  • 1
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356