Discussions

EJB programming & troubleshooting: threads?

  1. threads? (1 messages)

    I've read that EJB's are distinctly single-threaded. But I have a question about this.

    In my particular case, I have a stateless session bean that implements the session facade pattern, handing off all work to some behind-the-scenes helper classes.

    One of the things the EJB has to do is connect via https to a URL and read some data. The problem is that these connections sometimes take too long. I have a list of servers that are available, and I need to be able to attempt a connection to a server, but give up after just a few seconds and move on to the next one, if it's taking too long.

    Since I've found no way to set a short timeout value on the java.net.URL or java.net.URLConnection objects, I have solved my problem with threads.

    In my helper class, I spawn a new thread, which does the work of connecting to the URL. Then I use the Thread.join() method to set a short wait time, and just discard the thread after the wait time is exceeded.

    So far, this is working perfectly. But, one of my coworkers told me just now that using threads with EJB is a "thou shalt not", and I'm looking for some guidance. I can understand rules about threads in Entity beans, or even threads inside of an EJB. But, what about minor threads deep down in helper classes? Is this still bad?

    If so, is there any other way to limit my wait time for a URL connection to go through?

    Threaded Messages (1)

  2. threads?[ Go to top ]

    Hi,

    There is no way we found to set a timeout on either a java.net.URL or java.net.URLConnection. We got around this issue by using the Appache Commons HTTPClient.HTTPConnection. This does have a timeout.

    I never figured out the reason why Sockets have timeouts but URL connections do not,

    David