开发者

How to run JSP code every 15mins once without running it [duplicate]

开发者 https://www.devze.com 2023-02-14 01:46 出处:网络
This question already has answers here: How to run a background task in a servlet based web application?
This question already has answers here: How to run a background task in a servlet based web application? (5 answers) 开发者_开发问答 Closed 7 years ago.

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


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消