Open new thread in Tomcat
I'm just asking theoretical question.
I' have an op开发者_高级运维eration which takes some period of time which is performed in some Servlet doGet/doPost method or inside of Spring MVC controller . is is good idea to open new background thread an perform it there? Won't it cause performance issue?
Would I get some befits if I were using jBoss JMS system in this case?
It's OK (Tomcat is not a JavaEE container). Since you mention Spring MVC, I will suggest using @Async
on the method you want to execute - the new thread will be spawned by spring (which is very much the same as the equivalent JavaEE annotation)
Servlet 3.0 introduced async processing, which would help here.
Basically, you mark the servlet as supporting async processing, then using the context to start a new thread - the container does it for you so that you don't need to handle anything with threads yourself.
See this blog article for an idea. The advantage of doing this, is that your servlet will be portable to full Java EE containers which won't let you start your own threads.
Otherwise, consider using the ExecutionService and friends from java.util.concurrent. There is no real reason to use "new Thread()" yourself these days.
Well, since Tomcat is not a Java EE container, but just a Servlet Container, you can open new threads without any problem (apart from the tipical issues of multithreading you should deal with ;).
JMS is a solution to decouple modules full blown middleware (a whole new run-time) that takes care of messages. This approach could be too complex for you if you just want to decouple a single process for efficiency reasons.
If you are using Spring 3.0, i would suggest you to use asynchronous calls. You just annotate a method of a bean with @Async, and you are done :).
精彩评论