views:

1396

answers:

7

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? Thanks.

+1  A: 

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.

Steve McLeod
+1  A: 

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

If have long running tasks you should use JMS + a full JEE 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.

johnstok
+3  A: 

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.

David G
A: 

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".

matt b
A: 

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.

anjanb
A: 

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.

ddimitrov
A: 

I'm tryng to do the same thing.

An http request reaches ( for the sake of argument ) a servlet which wants to kick of an asynch task within the web server. In the application layer how can I get at tomcat's thread pool?

I am also using the spring framework, which has a nice abstraction websphere's container thread pool (lookup via JNDI), but doesn't for tomcat...