0

How to run JSP code every 15mins once without running it. I want to execute a jsp program code periodically every 15mins once.

Wooble
  • 87,717
  • 12
  • 108
  • 131
Eswar
  • 1
  • This is weird requirement, you shouldn't relay on this. it seems you are trying to do this for some other site,and you don't have actual code – jmj Feb 28 '11 at 11:48

2 Answers2

2

Java code doesn't belong in a JSP file. Just move that code into a real Java class. This way you can use ScheduledExecutorService in a ServletContextListener to execute it periodically.

@WebListener
public class Config implements ServletContextListener {

    private ScheduledExecutorService scheduler;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        scheduler = Executors.newSingleThreadScheduledExecutor();
        scheduler.scheduleAtFixedRate(new Task(), 0, 15, TimeUnit.MINUTES);
    }

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

}

Where Task class implements Runnable.

public class Task implements Runnable {

    public void run() {
        // Do your job here.
    }

}

Or if your Java EE container is capable of this, use the container-provided job scheduling capabilities. The detailed answer depends on the container which you're using.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
-2

You can write a JAVA code which will trigger the JSP code periodically.If you have interest in this solution I can help you to do this (giving the sample code).

Abhishek
  • 2,095
  • 2
  • 21
  • 25
  • My point was to make an java application which will execute the JSP code(calling the URL) periodically. – Abhishek Feb 28 '11 at 13:59