0

I have a little problem figuring this out the right way.

I have a Java application reading a log file continuously using threads. While that application is still reading the log file, a client should be able to query the current status (i.e. a certain key was found in the log file) through a java servlet.

My current issue is that I am having problems getting that status using the doGet-Method of the servlet. WHile running the thread is supposed to change a single boolean variable.

My question is:

How do I get the Log Reader Thread to start running when I deploy the Servlet on my Tomcat. In idle mode the Log Reader is listening for new files in a folder and starts to read them once they appear?

mgrstnr
  • 490
  • 1
  • 5
  • 23

2 Answers2

1

Please check below link you can use SevletcontexListener

In SevletcontexListener you can start you logger

Link

public void contextInitialized(ServletContextEvent servletContextEvent) 
    {
          System.out.println("ServletContextListener started"); 
         //start thread here
    }
public void contextDestroyed(ServletContextEvent servletContextEvent) {
     //stop thread here
}
sunleo
  • 10,589
  • 35
  • 116
  • 196
  • Okay. So I have that ContextListener running. How does the listener run my thread though? Thanks for your help! – mgrstnr Nov 08 '12 at 07:43
  • include your Thread work in a separate class and instantiate that class in Listener. – sunleo Nov 08 '12 at 07:59
  • you probably also want to shutdown the thread on `contextDestroyed`, otherwise the web server cannot undeploy or shutdown cleanly. – Thilo Nov 08 '12 at 08:06
  • Okay this starts my Thread with the web service. Now I have the problem that my Tomcat does not startup as it is waiting for the thread to finish. What do I do here? – mgrstnr Nov 08 '12 at 10:15
1

My current issue is that I am having problems getting that status using the doGet-Method of the servlet. WHile running the thread is supposed to change a single boolean variable.

That's probably because of concurrent update of non thread-safe boolean value. For more details on this topic you can read following tutorial on Java Concurrency

How do I get the Log Reader Thread to start running when I deploy the Servlet on my Tomcat. In idle mode the Log Reader is listening for new files in a folder and starts to read them once they appear?

Please refer to following answer, where it is described how to start threads from ServletContextListener using Executors, which are high-level abstractions over threads.

Hope this helps...

Community
  • 1
  • 1
Yuriy Nakonechnyy
  • 3,742
  • 4
  • 29
  • 41