troubleshooting Question

How to run a class that loops forever on Tomcat startup?

Avatar of killdurst
killdurst asked on
JavaJava EEApache Web Server
2 Comments1 Solution981 ViewsLast Modified:
Hi, I would like to run a program that loops forever, in the background, when Tomcat starts. I created, and in its init() method, I included the following code...

    System.out.println("Hello world!");
    Thread t = new Thread();

So basically the code will output "Hello world!" to the console every minute.

In my web.xml, I included the following tag...


So my Tomcat will execute MyServlet when it starts.

The problem is that I can't access the web application at all since MyServlet is still running. Since it will run continuously until the Tomcat shuts down, that means I can't access the web application at all. How do I run a program, that loops continuously, in the background, whenever Tomcat starts, and which will not affect the other servlets in my web application? Thanks!

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros