How do you launch multiple threads from within Java EE?

后端 未结 6 1215
孤城傲影
孤城傲影 2021-01-01 04:02

I need to scale calls into Tomcat and it\'s been suggested to launch threads internally. Has anyone needed to do this and, if so, what solutions did they come up with?

相关标签:
6条回答
  • 2021-01-01 04:11

    The J2EE abstraction for managed multithreading is JCA. In particular, take look at the WorkManager and Work classes. See also this arcicle. Spring also provides JCA-backed work manager abstraction.

    0 讨论(0)
  • 2021-01-01 04:13

    Your question is a bit vague. Tomcat itself already uses a thread pool to service HTTP requests. You can increase the number of threads through Tomcat configuration - look to the Tomcat wiki for info on this.

    If you mean that in your code you want to launch threads, then I advise perusing the java.util.concurrent API introduced in Java 5. Also read "Java Concurrency in Practice", which is the text on this subject.

    0 讨论(0)
  • 2021-01-01 04:23

    Creating your own threads inside an application server is generally discouraged because the server should manage threads for better scalability. You can also run into problems if the container makes assumptions about what's available in a thread context, such as security information (e.g., authenticated Subject). That typically happens if you spawn a thread and then use a server resource from that thread which is unknown to the container.

    Check to see if there is a way to get container managed threads from Tomcat. WebLogic and WebSphere support the commonj.WorkManager, which allows you to schedule work on container managed threads. Spring can also use commonj, but I'm not sure if that support is available on Tomcat.

    0 讨论(0)
  • 2021-01-01 04:28

    as others asked, you should give more details as to what you're trying to accomplish.

    Otherwise, tomcat uses thread pools. increase the number of threads in the pool. Use a newer version of tomcat -- 6.x. Use Java 6.0_10. If needed, tune the application using a profiler and fiddle with the JVM settings, if required.

    0 讨论(0)
  • 2021-01-01 04:29

    You shouldn't really launch threads from within your webapp unless you have a very specific need to do so. Without more details on your problem it is hard to tell if this is the right approach to solve your problem.

    You might want to take a look at Quartz, which "is a full-featured, open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application".

    0 讨论(0)
  • 2021-01-01 04:32

    What is the problem you are trying to solve with threads?

    If have long running tasks you should use JMS + a full Java EE container.

    If you trying to handle excess load you could consider two tomcat instances, however, if you are using http sessions you will need to investigate session replication.

    If you are forced to use Tomcat consider using the Executors framework in java.util.concurrency.

    0 讨论(0)
提交回复
热议问题