2

I have a java function which checks and modify values in my SQL database to avoid errors, i need it to be executed automatically on server startup as well as on restart. i created a jsp page to call this function as jsp support "setInterval" in which i can run it automatically after every 3 minutes to remove errors from my database now i need it to be executed automatically on server startup. can anyone guide me on this?

the following is my jsp code:

setInterval(function(){Autolf();},60000);

function Autolf()
{

$.post('autolgfn.jsp',
        {
    abc:1
        },
        function(response,status,xhr)
        {
            alert(response.trim());

        });

}

the above code calls function from a java page which is connected to database. please help me to run it automatically on server startup and keep running after every 3 minutes. thanks in advance

Pracede
  • 3,890
  • 14
  • 54
  • 101
jasim
  • 429
  • 1
  • 6
  • 23

2 Answers2

7

You can write a ServletContextListener that uses a ScheduledExecutorService (or Timer) to start your process in the contextInitialized method and stops it in the contextDestroyed method.

It could look something like this:

private volatile ScheduledExecutorService executor;

public void contextInitialized(ServletContextEvent sce)
{
    executor = Executors.newScheduledThreadPool(2);
    executor.scheduleAtFixedRate(myRunnable, 0, 3, TimeUnit.MINUTES);
}

public void contextDestroyed(ServletContextEvent sce)
{
    final ScheduledExecutorService executor = this.executor;

    if (executor != null)
    {
        executor.shutdown();
        this.executor = null;
    }
}
Brett Okken
  • 5,929
  • 1
  • 17
  • 24
  • can you tell me what i have to write under context destroyer? – jasim Jul 04 '14 at 14:02
  • ok, but where do i have to put my java code? – jasim Jul 04 '14 at 14:20
  • in your war - either directly or in a jar in the war – Brett Okken Jul 04 '14 at 14:23
  • how the above scheduled executor service identify my specific java function defined else where? sorry, am a beginner to programming. – jasim Jul 04 '14 at 14:27
  • The `myRunnable` would need to be a runnable you defined to call your function. – Brett Okken Jul 04 '14 at 14:36
  • How can i define a runnable? – jasim Jul 04 '14 at 14:38
  • You write a class implementing runnable. http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html – Brett Okken Jul 04 '14 at 14:43
  • i had created a new class "myRunnable.java" implemented runnable and under public void run(), i had written my codes. Now how to connect myRunnable class with class which we implemented ServletContextListener? myRunnable is still underlined with red color in it, please help me to finish this – jasim Jul 04 '14 at 15:02
3

To elaborate on Brett's answer:

Configure your web.xml:

<servlet>
    ......
    <!-- force initialization as soon as Tomcat starts -->
    <load-at-startup>1</load-at-startup> 
</servlet>

<!-- configure the context listener -->
<listener>
   <listener-class>com.acme.MyContextListener</listener-class>
</listener>

MyContextListener looks something like:

public class MyContextListener implements ServletContextListener {
    // where the work happens
    final Runnable myRunnable = new Runnable() {
        public void run() { System.out.println("hello world"); }
    };

    // Brett's code here
    //....

}
jekennedy
  • 992
  • 8
  • 14