Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating threads a Webapplication which is deployed in Tomcat

I am working on a web application which is deployed in Tomcat. We are using Spring, Hibernate, GWT at the client side).

One of the functionality of this application is to send alerts ( emails ) to Users when any entity is created, updated or deleted. ( Alerts can be added by Users on the fly, so there is some processing involved - to decide which users should be notified by email ).

Alert mechanism ideally should be asynchronous and it should be affect the performance of CRUD operation.

First thing which came in my mind is to create a thread and have a blocking queue. Thread keeps polling blocking queue to see if it has got any events. But creating thread in web application is something which is discouraged by many Containers.

Can someone advice/ suggest - is this the correct way of doing it ? or There are better ways of doing the same thing .

Any pointers would be highly appreciated.

Thanks in advance, Sachin

like image 904
Sachin Avatar asked Mar 23 '11 14:03

Sachin


People also ask

How many threads can be executed at a time in Tomcat?

By default, Tomcat sets maxThreads to 200, which represents the maximum number of threads allowed to run at any given time. You can also specify values for the following parameters: minSpareThreads : the minimum number of threads that should be running at all times.

How does Tomcat thread pool work?

Tomcat acceptor thread accepts connections from the completed connection queue. Checks if a worker thread is available in the free thread pool. If not, creates a worker thread if the number of active threads < maxThreads. Else wait for a worker thread to become free.

In which folder of Tomcat do you have to put your Web application?

Simply put, web applications are placed under $CATALINA_HOME\webapps, where $CATALINA_HOME is the Tomcat's installation directory. The context path refers to the location relative to the server's address which represents the name of the web application.

How do you increase thread pool size in spring?

max-threads property to control the size of the client request thread pool. Its default value is zero which leaves Tomcat to use its default of 200. To customise the size of this thread pool you should specify a non-zero value for the server.


2 Answers

The restriction on creating threads in a container was really just a suggestion to keep inexperienced developers from shooting themselves in the foot. No container actually prohibits you from doing this. With java.util.concurrent classes, creating threads should be less error prone and I wouldn't worry about this restriction too much.

If your requirements are simple, it's easy enough to just create a single thread / runnable in a ServletContextListener. Create and start the thread in contextInitialized() and shut it down in contextDestroyed(). . Use a ScheduledExecutorService created by Executors.newSingleThreadScheduledExecutor(). The Runnable you pass to the Executor would read from a BlockingQueue.

If your requirements change and you need something more complicated, you probably want to looks at JMS / MDBs or a scheduler like Quartz.

like image 92
AngerClown Avatar answered Sep 28 '22 07:09

AngerClown


You could use a scheduler to run jobs regularly or use the Spring equivalent of Message Driven Beans (some documentation on JMS and Spring) which are executed by the container which does the queue polling for you.

like image 24
Thomas Avatar answered Sep 28 '22 09:09

Thomas