2

I am confused if we should make our own threads in servlet or not,as they have threading mechanism internally?. If yes how can we make sure if the program thread safe? How to implement thread safe mechanism in servlets.

2
  • 1
    Even though it is not explicitly illegal, I would try to avoid creating threads in a servlet. The results may be unpredictable or depend on the container you use. Can you explain a bit more about your requirements and why you would need to create your own threads? In general though, your code needs to be thread-safe, as the container might call the same servlet-object from different threads. There is a lot of information about this in the servlet specification. Commented Jan 25, 2012 at 12:23
  • The thing is i'm reading log files and after that sending mails according to severity.this process is happening continuously,but i noticed sometimes it is missing the mails.so i thought to use a different thread for mail part, and what if i have to read multiple files at the sam time.How can that be acheived without using threads?? Commented Jan 25, 2012 at 12:39

3 Answers 3

2

You are asking two different questions:

I am confused if we should make our own threads in servlet or not,as they have threading mechanism internally?.

Normally, you should not start threads in a Java EE application. If you need seperate threads, make sure you use a Scheduler Service that your application knows about, so that it has the chance to shut down the threads when the application is shut down. Quartz is what's used most of the time.

If yes how can we make sure if the program thread safe? How to implement thread safe mechanism in servlets.

Servlets are just like any other Java class. Find a tutorial on thread safety or read Java Concurrency in Practice.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm using Timer class class and i am not able to stop the threads when i stop the tomcat server on which my program is deployed
@raghav Timer is an ancient relic. You should switch to the ExecutorService interface introduced in Java 1.5. Executors.newScheduledThreadPool(poolSize) should be what you need
That way you can call ExecutorService.shutdown() when your web app shuts down.
@Sean Patrick Floyd:you should not start threads in a JEE application.Did you ever had an issue with a container?I have used threads in Tomcat many times without an issue.
1

From what you write in the comment, I understand that you have a set of threads continuusly monitoring log-files and sending email if something interresting is found in the log.

First question: why is this a servlet? Is there a web-gui? What is this used for?

For the log-scanning part, I would have implemented that as a separate process outside of the servlet-container. For everything this process found which it needs to send somewhere, I would add a message to a JMS-queue. Then I would create a messagedriven bean to recieve messages from this queue and send them as email. (This is really an integration problem, transforming messages from JMS to email, you might want to look into something like Mule to solve this).

As for how to integrate this with your servlet, it depends on what your servlet does in addition to scanning logs (I suppose it presents the user with some kind of interface)

With this design, you can chose to re-write the programs generating the log in the future. Instead of having one program writing log and another program parsing the log, the first program might as well put the interresting message directly on the JMS-queue. In other words, you can change the log-generation part of your architecture in the future, without having to re-write the mail-sending part.

2 Comments

Yes you are right. i am scanning the log files using timer and sending mails if something of concern occurs. now the question ,why is this a servlet,so that it when i want to know that it is running fine,i could get the response. now the thing is when i send mails using new threads it is giving memory leakage problem as it is not able to stop the thread when i stop the app or server.so you are saying that i should use JMS queue for this,right?
If you let implement log-scanning, you won't have issues with your log-scanning threads interfering with the app-server threading. When implementing communication with JMS, the app-server handles threading for you and invokes your message-handling code only when there is an actual message. To monitor the external log-scanning process, it could send "i-am-alive"-messages regularly, and the servlet could just report when the last "i-am-alive" occured (and it could obvioulsy report other things as well, such as number of messages recieved and number of mails sent)
1

I also had a similar concern.

Only EJB specification disallows the creation of threads from the application.

It is ok to start a thread from a servlet.
I have done it many times with no problems but to be honest I am not 100% sure:

  • that this is allowed by container but is violating a standard

or

  • it is allowed by all containers.

But in Tomcat I never had an issue starting threads from a servlet.

You can make it thread safe the same way you do in every multithreading program.

You will use all the available constructs offered by Java for synchronization.

1 Comment

From the servlet-spec (version 3.0, chapter 1.2): "For example, high-end application servers may limit the creation of a Thread object to insure that other components of the container are not negatively impacted."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.