Threads spawning in Servlets allowed?

Hello to all servlet afficionados,
          I am following thread discussions already a while and there's always
          discussion whether to user threads or not (I am not talking about a
          servlet thread but about threads manually instantiated for other
          reasons like asynchroneous handling of tasks for loose coupling or for
          long running purposes).
          So here's my question:
          Is it definitely allowed to spawn threads from a servlet (and even put
          the reference into the application context to allow others to later on
          access that thread)?
          Does the jdk specify on this?
          Or Is it even specific for each servlet engine? What is BEA saying
          about this?
          And even further: Is this different on application servers that
          include an ejb-container? Again, does the specification say something
          about this? What does BEA say about this?
          Thanks for trying to find out the real truth :-)
          regards
          Stefan
          

Stefan,
          There is nothing in the Servlet spec that specifically prohibits the use
          of threads within a servlet, but most will tell you that you should avoid it
          if possible. Having said that, I've seen quite a few implimentations that
          use background threads for tasks such as log queue processing and have not
          really had any issues. To me it's a matter of degree. As long as your
          architecture makes sense and you are careful to avoid potential deadlocks,
          you should be ok. FWIW, BEA recommends not creating threads within a web
          application.
          -chris
          "Stefan Hoehn" <[email protected]> wrote in message
          news:[email protected]...
          > Hello to all servlet afficionados,
          >
          > I am following thread discussions already a while and there's always
          > discussion whether to user threads or not (I am not talking about a
          > servlet thread but about threads manually instantiated for other
          > reasons like asynchroneous handling of tasks for loose coupling or for
          > long running purposes).
          >
          > So here's my question:
          >
          > Is it definitely allowed to spawn threads from a servlet (and even put
          > the reference into the application context to allow others to later on
          > access that thread)?
          >
          > Does the jdk specify on this?
          >
          > Or Is it even specific for each servlet engine? What is BEA saying
          > about this?
          >
          > And even further: Is this different on application servers that
          > include an ejb-container? Again, does the specification say something
          > about this? What does BEA say about this?
          >
          >
          > Thanks for trying to find out the real truth :-)
          >
          > regards
          > Stefan
          

Similar Messages

  • Ejb lookups fail from threads spawned from servlet

    Hello,
    We have a servlet from which we are spawning a thread. In the thread we are
    trying to do a JNDI lookup for the database resource / EJBs. It does not
    work as if it is not able to find the context t do the lookup.
    I thought that the threads are spawned in the same JVM and context, so why
    does the lookup not work.
    Please need to figure out a solution to this problem, we need to call some
    business logic in the EJBS from the thread and we do not want to do RMI-IIOP
    lookups becuase they could be slower.
    Appreciate your help in advance.
    Regards,
    Sakib

    Custom threading is not supported. I think a workaround was posted on
    this alias (search for custom threading), where you can call the
    proprietary API's of the container to recreate the context, but I'm
    pretty sure that it's not a supported or recommended solution.
    The better (and more scalable) solution is to avoid spawning your own
    threads.
    David
    Sakib Mehasanewala wrote:
    Hello,
    We have a servlet from which we are spawning a thread. In the thread we are
    trying to do a JNDI lookup for the database resource / EJBs. It does not
    work as if it is not able to find the context t do the lookup.
    I thought that the threads are spawned in the same JVM and context, so why
    does the lookup not work.
    Please need to figure out a solution to this problem, we need to call some
    business logic in the EJBS from the thread and we do not want to do RMI-IIOP
    lookups becuase they could be slower.
    Appreciate your help in advance.
    Regards,
    Sakib

  • Starting new threads from a servlet

    Hi
    I read somewhere that starting new threads from a servlet is not supported in the future releases of J2EE .
    Any one aware of this.
    I have a listener program started as a new thread from the init method of a servlet which is set to load on startup.
    I have to think about another way to do this if this is not suppported in the future releases
    any comments

    Dr clap
    Actually I was getting some warning messages in my websphere server console when I tried to make JDBC connections from the thread I opened in the servlet.
    Everthing works fine but with the warning messages,
    Then someone from IBM answered me in one of the forums quote
    Cause: These messages are being produced due to an
    "unsupported" environment. When application is
    spinning its own threads from an EJB, spinning a
    thread and accessing a database is not supported (per
    the J2EE specification). One exception to this rule is
    if a Servlet is spinning its own threads and accessing
    a database. The J2EE specification is not clear on
    this, so WebSphere Application Server 5.0 must allow
    for it at this time. IBM is working with Sun to
    clarify this in the specification, so eventually (i.e.
    J2EE 1.4) spun threads from a Servlet accessing a
    database outside of a transaction will not be
    supported either.
    Since we don't want to promote the usage of these
    "naked" threads accessing databases, we are producing
    the warning messages. Although they may be filling up
    the logs, we feel that these are necessary to warn the
    user that they are doing something not in-line with
    the intended usage. Customer should consider changing
    their application to comply with the
    J2EE specification.
    """

  • Thread Spawn

              Hi,
              What is the best way to spawn a thread when the servlet context starts and terminates
              when the context stops or ends in Weblogic 6.1? At the moment, I use a startup
              on load servlet to kick off a thread to start my background process. This process
              does a JDBC SELECT, performs some calculations and finally, JDBC INSERT or UPDATE
              resutls into a few tables. The problem I'm faing now is that when the context
              is stopped, the thread still running. Pls. advise. Thank you.
              /lim/
              

              I have set a flag to end the thread since interrupt() is deprecated. However, destroy()
              method is not called when I unload my webapp thru the admin console. Therefore,
              the thread still running after I have unload my webapp.
              "Perianayagam.T" <[email protected]> wrote:
              >
              >Iam not getting what u mean by stopping the context...
              >But what I suggest you is in the destroy() method of the servlet (which
              >is bascially
              >the destructor of the servlet ) you can interrupt (thread.interrupt()
              > )the thread
              >which was spawned initially...
              >
              >pls let me know if my understanding was correct..
              >Ref:http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/Servlet.html#destroy()
              >Thanks
              >Perianayagam.T
              >
              >
              >"TH Lim" <[email protected]> wrote:
              >>
              >>Hi,
              >>
              >>What is the best way to spawn a thread when the servlet context starts
              >>and terminates
              >>when the context stops or ends in Weblogic 6.1? At the moment, I use
              >>a startup
              >>on load servlet to kick off a thread to start my background process.
              >>This process
              >>does a JDBC SELECT, performs some calculations and finally, JDBC INSERT
              >>or UPDATE
              >>resutls into a few tables. The problem I'm faing now is that when the
              >>context
              >>is stopped, the thread still running. Pls. advise. Thank you.
              >>
              >>/lim/
              >
              

  • Thread pool in servlet container

    Hello all,
    I'm working on this webapp that has some bad response times and I've identified an area were we could shave off a considerable amount of time. The app is invoking a component that causes data to be catched for subsequently targeted apps in the environemnt. Our app does not need to wait for a response so I'd like to make this an asyncronous call. So, how best to implement this?...I considered JMS, but started working on a solution using the Java 1.4 backport of JSR 166 (java.util.concurrent).
    I've been testing the use of a ThreadPoolExecutor, using an ArrayBlockingQueue. The work that each Runnable will perform involves a lot of waiting (the component we call invokes a web service, among a couple other distributed calls). So I figure the pool will be much larger than the queue. Our container has 35 execute threads, so I've been testing with a thread pool size of 25, and a queue of 10.
    Any thoughts on this approach? I understand that some of this work could be simplified by JMS, but if I don't need to be tied to the container, I'd prefer not to. The code if much easier to unit test, and plays nicely with our continious build integration (which runs our junit test for us and notifies on errors).
    Any thoughts are greatly appreciated...Thanks!!

    Well, if it works, that's by far the best way to go - but note that creating threads in a servlet container means those threads are outside of the container's control. Many containers will refuse to give the new threads access to the JNDI context, even, and some may prevent you from creating threads at all.

  • Servlet with Multiple Threads spawned automatically

    Hi,
    I have a problem involving threads in servlet as follows:
    Scenario: I am working on developing a web application using Tomcat and Netbeans IDE. I have a servlet which gets values from the session object and then constructs a server-side object and invoke a method on the same. This method on the server-side object executes an INSERT query in the mysql database.
    Problem Description: The INSERT query is invoked twice => the method on the server-side object is invoked twice, and 2 records with different keys are created in the database table.
    Observation: When I used the debugger in the IDE, I discovered that there are 2 different threads that are getting spawned (which I did not design/code for) and these threads call the insert query on the table twice.
    I am not sure why this is happening. Can someone please throw some light on this?
    Thank you in advance.

    The servlet has a number of multiple if-else blocks. In the last if-else block of the servlet code, I am getting some attributes from the session object, creating the server class object and setting the attributes of the object.
    The 2-thread problem is occurring only when the the last block is reached. The 1st thread creates the server class and the 2nd thread executes the rest of the lines in the servlet and completes the INSERT query. This is followed by the 1st thread to insert a new record again.

  • Spawning multiple threads inside a Servlet

    Hi All,
    We have a situtation where we need to crawl various sites/databases for information to perform a search. As we all know, the most efficient way is to multithread the application. Since J2EE discourages custom Multithreading, we had to search for alternatives to accomplish this task.
    We came across few interesting solutions using JMS/MDB. Other than using these is there a simpler way to accomplish this. Any help is appreciated...
    Thanks,
    Srini

    oc4j/orion is the fastest servlet engine on the planet. Those
    guys in Sweden don't let you mess around with the number of
    servlet instances available. The message here is that you do not
    have to resort to any tricks that other servlet engines use to
    make sure your servlet will be available to be served up...it
    will.
    The only thing that slows it down are lack of memory or lack of
    resources (the database isn't replying fast enough). You can
    solve these problems by clustering your servers on several
    machines.
    regards,
    the elephantwalker
    www.elephantwalker.com

  • Restricting Thread Pool for Servlet instances

    I am using WebLogic5.1.0 version wherein I am registering a Servlet which
    would be called by all the clients connecting
    to my aplication. The servlet's service methods is taking some time to
    execute. Since the servlet is following a SingleThreadModel, when there are
    a large number of Clients connected, the number instances of Servlets
    created are very high and the number of thread in the Application Server
    process becomes very high affecting the performance of the other
    applications.
    Is it possible to restrict the number of instances of the servlets that get
    created in the Application server ?
    What are the setting required to achieve this ?
    Sandeep

    Hi.
    I'm not aware of a property setting that allows you to restrict the number of
    servlet instances in WLS for the SingleThreadModel. The number of execute
    threads is fixed - it does not grow depending on load. You can set this value
    in the weblogic.properties file by setting weblogic.system.executeThreadCount.
    This value is 15 by default.
    It does sound like you are reaching the max capability of your
    server/hardware. You should try tuning WLS.
    Here are a couple of suggestions:
    1. Try adjusting the executeThreadCount value to a higher value. Start by
    setting it to 30 - see if that makes any difference.
    2. Check out the following tuning guide:
    http://www.weblogic.com/docs51/admindocs/tuning.html
    3. Here is a list of admin properties that might aid in tuning:
    http://www.weblogic.com/docs51/adminhelp/AdminPropertyHelp.html
    4. If you are still having problems you might do better by posting this issue
    to the performance newsgroup.
    Regards,
    Michael
    Sandeep Rajpathak wrote:
    I am using WebLogic5.1.0 version wherein I am registering a Servlet which
    would be called by all the clients connecting
    to my aplication. The servlet's service methods is taking some time to
    execute. Since the servlet is following a SingleThreadModel, when there are
    a large number of Clients connected, the number instances of Servlets
    created are very high and the number of thread in the Application Server
    process becomes very high affecting the performance of the other
    applications.
    Is it possible to restrict the number of instances of the servlets that get
    created in the Application server ?
    What are the setting required to achieve this ?
    Sandeep--
    Michael Young
    Developer Relations Engineer
    BEA Support

  • Thread management in servlet

    Here is the problem that i am trying to solve - and get better understanding of issues to look out for.
    1) Problem to solve
    Clients sends an http request to an distributor server. The request contains a list of tasks. Each such task can be sent to a particular handling server (out of a known set) which can handle the request. Each task has a task type, which identifies which server the request needs to be passed to. The responses from the handler servers are then to be collected and passed back the client as a consolidated response.
    On the distributor server, the servlet thread that is handling the request will
    a. need to spread the requests for various handling servers
    b. send the requests in parallel to handling servers
    b. get all the responses , consolidate and send back
    2)Proposed solution
    I was thinking of having a thread pool - one thread for each handling server, initialized within servlet . When the distributor server receives a request - the tasks would be spread to these handling threads. The distributor thread would then wait, until the handling threads notify.
    When a notification is received - the distributor thread will need to check if all responses are received. If not - it would need to wait (till a max_timeout) else return the response to client
    3)Questions
    - This involves waiting on the thread created by serlvet container (the distributor thread) and creating a threadpool. Thread creation and management by application in web-container is generally dissuaded - but given this issue - i see no other alternative
    suggestions ? Its been a long email - thanks for considering
    Fred

    How many clients for the entry server do you presume for the application ? I ask this because it is very possible that the server to remain without available threads and give errors. If you use different servers for the final requests I suppose that are some very time consuming processes. What kind of application are running on these servers so that you want to use this kind of distributed computing ?
    Why did you choose as an entry point a servlet container and http request for this kind of problem ? Do you need a graphic response in the browser ? If the response time is too long, the browser (Internet Explorer) will show exception and will not wait for the response (for this you must set some variabiles on client). Also the J2EE server must be set so that not to throw a time-out exception.
    To prevent these, you could do so that not to block the distributor thread and let it send the answer back to the client. Then you can tell to client browser to query the server from time to time to check the status of the execution (you can do that from javascript). When the execution is ready you can retrun the response to the client. Doing this way you will not block the application server threads.
    Now in the back end, you must somehow to take some identificators from the processes and sent them back to the client.
    You can use a database to make the bridge between the central server and the other processes (here you can put the processes identificators). When the processes end up they update their results in the database.
    The client, when will make the repetitive requests, will check the status of the processes in the database with given identificators.
    Sounds like an interesting project.

  • Java Logger incrementing thread ids for a thread spawnned via JNI

    Hi all,
    I have a Java Logger object that I am passing to a JNI dll written in C++.
    If I spawn a thread from the JNI dll and log from that thread, the thread id in the log file increments for each log entry.
    To test this I wrote an infinite loop on the thread to make sure that the logging originated from a single thread. The thread id keeps incrementing in the log and the memory usage of the process increases too. However, the thread count in Task Manager does not increase.
    I will let the process run overnight to determine if the thread id will overflow or if an out of memory exception occurs.
    Has anyone else seen this behavior? Does anyone know what I might be doing wrong? I'm using jre 1.5.1.
    Thanks,
    William

    (1)
    has anybody ever tried to use a native library from
    JNI, when the library (Windows DLL) is not thread safe?
    Now we want many Java clients.
    That would mean each client makes its calls
    to the library in its own thread. Because the library
    is not thread safe, this would cause problems.Right. And therefore you have to encapsulate the DLL behind a properly synchronized interface class.
    Now the details of how you have to do that depends: (a) does the DLL contain state information other than TLS? (b) do you know which methods are not thread-safe?
    Depending on (a), (b) two extremes are both possible:
    One extreme would be to get an instance of the interface to the DLL from a factory method you'll have to write, where the factory method will block until it can give you "the DLL". Every client thread would obtain "the DLL", then use it, then release it. That would make the whole thing a "client-driven" "dedicated" server. If a client forgets to release the DLL, everybody else is going to be locked out. :-(
    The other extreme would be just to mirror the DLL methods, and mark the relevant ones as synchronized. That should be doable if (a) is false, and (b) is true.
    (2)
    Now we discussed to load the library several times -
    separately for each client (for each thread).
    Is this possible at all? How can we do that?
    And do you think we can solve the problem in this
    way?The DLL is going to be mapped into the process address space on first usage. More Java threads just means adding more references to the same DLL instance.
    That would not result in thread-safe behavior.

  • No of threads for a servlet

    1. Is there any limit on maximum no of threads that would be created for a servlet by default? (I'm using Tomcat 3.2) If yes, what is the value of this limit?
    2. How can I change this limit in Tomcat 3.2? Is there any difference in the configuration of this limit for Tomcat 3 and Tomcat 4?
    3. If I increase the maximum no of threads, what OS level limitation would I need to take care of? Like default socket close timeout or what else ...
    Ankit

    Look in the server.xml file. If you are running tomcat in standalone on port 8080 search the server.xml file for '8080'. Here you will find the configuration options for the connector. The maxProcessors and minProcessors setting set the number of threads that tomcat will use for requests on this connector.
    For more information check the tomcat documentation.

  • Thread Button in servlet

    Hi All,
    I want to have a servlet or jsp page which has several buttons named 'Start'.
    On clicking a 'Start' button, it should generate a thread (in another class) and run a process and the button will become 'stop'.
    On clicking the stop button, the thread should stop. I can click any button at any time and start the thread and stop in at any time.
    Can anyone tell me any technique for this problem?
    Regards,
    Pradhip

    i can't find why a nullPointerException occurs if i don,t see the code!

  • Thread pool for Servlets

              Hi all,
              I am facing a big problem while doing a concurrent testing of my web application.
              I am testing with 60 concurrent users.
              I have set the database connection pool minimum capacity to 50, capacity increment
              to 10 and maximum capacity to 100.
              While there is no problem in getting database connection, the session variables
              are getting mixed up (i.e. the session variable for one user is coming up as that
              for another user!!!).
              Does it have something to do with configuring some pool for servlets threads or
              something?
              Please help.
              Thanks a lot in advance and best regards,
              Jaya
              

              Hi all,
              I am facing a big problem while doing a concurrent testing of my web application.
              I am testing with 60 concurrent users.
              I have set the database connection pool minimum capacity to 50, capacity increment
              to 10 and maximum capacity to 100.
              While there is no problem in getting database connection, the session variables
              are getting mixed up (i.e. the session variable for one user is coming up as that
              for another user!!!).
              Does it have something to do with configuring some pool for servlets threads or
              something?
              Please help.
              Thanks a lot in advance and best regards,
              Jaya
              

  • Where can we tell OC4J to load multiple threads of a servlet?

    I configured a web site on OC4J that uses a servlet. Everything is working fine.
    Now, I want to start, lets say, 50 instances of this servlet when the web site
    is started by OC4J. How can I do this? Which configuration files should
    I play with? Please note that my servlet is single-threaded and that I want
    all threads running in the same JVM.
    I want to do something similar than this parameter in JServ:
    singleThreadModelServlet.initialCapacity=100
    Thanks

    oc4j/orion is the fastest servlet engine on the planet. Those
    guys in Sweden don't let you mess around with the number of
    servlet instances available. The message here is that you do not
    have to resort to any tricks that other servlet engines use to
    make sure your servlet will be available to be served up...it
    will.
    The only thing that slows it down are lack of memory or lack of
    resources (the database isn't replying fast enough). You can
    solve these problems by clustering your servers on several
    machines.
    regards,
    the elephantwalker
    www.elephantwalker.com

  • Single Threaded Model JSP- Servlet

    Servelts can be set to have thread safe access (Single thread model). However, when a JSP is converted in to a servlet, is the servlet tagged as to use the single thread model?

    As of JSP specification 1.2 the default value for the "isThreadSafe" attribute of the page directive is true indicating that the container can direct multiple requests to the page simultaneously. If this value is set to false then the requests will be sent one at a time.
    Good Luck!
    Eshwar Rao
    Developer Technical Support
    Sun microsystems inc
    http://www.sun.com/developers/support

Maybe you are looking for

  • How do I transfer my files from iTunes to WindowsXP Pro or visa versa?

    I'd like to be able to figure out how to transfer music files out of iTunes to Windows Media, and from Windows media to itunes. I've got a bunch of lecutures in both, but both don't do what I need them to do. In a perfectworld, I'd be able to get iTu

  • SSD drive filling up due to video rendering?

    Hi. Apologies for what will no doubt be a stupid question but I'm not as computer savvy as many of you here. Basically I film ice hockey games for my local team and produce weekly highlights for YouTube. I edit the footage using Premier Pro CC and th

  • Restoring itunes library from a backup

    I copied my iTunes folder into a backup drive. My hard drive died, and now I want to "re-connect" my iTunes with its library. How can I do it? Simply pointing iTunes to the library location doesn't work. I need to be able to sync my iphone, etc... He

  • ITunes and iPod Classic issues

    I have a 120gb classic iPod. About a week ago, I got the red x. So I restored my it. Then anytime my iPod was plugged in, iTunes froze until I unplugged my ipod and then it went back to normal. I've read forum after forum and here's what I've done. -

  • Ipod Freezes must reset each time to mount

    HI, 8GB nano. Recently, every time I connect my nano it does not mount in itunes and it freezes. I can unplug it, hit the reset (center and menu), and replug it in and it will mount and sync fine. Next time though, same thing will happen. Even second