-3

I am using the following code as scheduler in java web deployment

public class ReportScheduler implements ServletContextListener {

    private ScheduledExecutorService scheduler;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        scheduler = Executors.newSingleThreadScheduledExecutor();
        scheduler.scheduleAtFixedRate(new ReportLoader(sce.getServletContext()), 0, 10, TimeUnit.SECONDS);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        scheduler.shutdownNow();
    }

The ReportLoader class which implements runnable is as follows:

public class ReportLoader implements Runnable {

    ServletContext context;

    public ReportLoader(ServletContext context) {
        System.out.println("1");
        this.context = context;
        StartUp();
    }
     private void StartUp() {
    System.out.println("start");
    (new Thread(this)).start();
    }

    @Override
    public void run() {
        System.out.println("scheduled");
        try {
            //Files.delete(path);

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}

But my Scheduler does not seem to be working as nothing is getting printed on the tomcat (which is my server) log, as one would expect to.

Is there any mistake that i am making, or something more that i need to ensure so that the scheduler works properly ?

The intention of the scheduler is to delete folders at regular intervals on the local PC.

pcs
  • 1,864
  • 4
  • 25
  • 49
Ambkrish
  • 837
  • 1
  • 6
  • 7
  • Have you registered the listener,in the web.xml or using annotations? Why do you start a thread from the constructor of your runnable? – JB Nizet May 25 '15 at 06:08
  • i was just trying out the thread part. should have removed it. my mistake. and could you elaborate on what you mean by "registered" to web.xml ? – Ambkrish May 25 '15 at 06:10
  • Are you looking [in the right logs](http://stackoverflow.com/a/1548974/1079354)? I wouldn't recommend using `System.out` anyway; use a proper logging service. – Makoto May 25 '15 at 06:10
  • Just because you have a class implementing ServletContextListener in your application doesn't mean Tomcat will find it and call it. Just like servlets, filters, etc. listeners must be declared in the web.xml (or using annotations since servlet 3.0). Use google to find out how. – JB Nizet May 25 '15 at 06:13

1 Answers1

-1

why use threadpool to run a Thread your code:

private void StartUp() {
    System.out.println("start");
    (new Thread(this)).start();
 }