Discussions

Web tier: servlets, JSP, Web frameworks: New thread in a servlet?

  1. New thread in a servlet? (2 messages)

    Hi,

    I have the following problem:
    In our produtive system we have to send Emails at a particular time. So I've written a class which loops, compares the time and if it's time starts the email sending. But my problem is, how to integrate that class in Websphere 3.0.2. My plan was to instantate the MailClass in the init()-method of an InitializationServlet, but unfortunately that method determines and my MailClass is "gone". Has anybody any suggestions how to implement such a task in an J2EE environment?
    Any help would be appreciated.

    Thanks in advance,

    Michael

    Threaded Messages (2)

  2. New thread in a servlet?[ Go to top ]

    In the past, I've used new Threads started from Servlets to do things like asynch mail sending, custom db connection pool maintenance (when I'm using a home-grown connection pool implementation), etc.

    However, the Servlet spec does warn you that some Servlet containers may prohibit the creation of new threads. WebLogic support once told me, "Yeah, it works, but it isn't supported."

    I first noticed this limitation in the EJB spec as a hard-and-fast rule. I think it's just a suggestion in the Servlet 2.2 spec:

    "A Servlet Container may place security restrictions on the environment that a servlet executes in. In
    a Java 2 Platform Standard Edition 1.2 (J2SE) or Java 2 Platform Enterprise Edition 1.2 (J2EE)
    environment, these restrictions should be placed using the permission architecture defined by Java 2
    Platform. For example, high end application servers may limit certain action, such as the creation of
    a Thread object, to insure that other components of the container are not negatively impacted."

    I don't know if WebSphere limits the creation of Thread objects.
  3. New thread in a servlet?[ Go to top ]

    DO NOT create threads in servlet engines. The server has enough work to do managing it's HTTP accept threads without your threads (which it doesn't control) taking it's resources away.

    The looping thread in this case should run in it's own virtual machine, independant of the servlet engine.

    If you have no choice then make sure:

    a) That you mark the thread as daemon. It should not cause the VM to keep going if there are no other threads running.
    b) It spends most of it's life asleep and yields regularly.
    c) Start it using a startup class somewhere. Such as a WebLogic startup class (I'm sure Websphere has it's own version.)

    Chz

    Tony