Java(cron4j)
-Cron is a type of resident program (daemon) that is used as standard in many UNIX-based OSs, and periodically starts the specified program according to the schedule set by the user. (* See IT Glossary)
jar Download the jar file from the link below and set it in your IDE! !! https://www.sauronsoftware.it/projects/cron4j/download.php
First, let's create a class to write the process you want to execute! !!
CronTask
import java.util.Date;
public class CronTask implements Runnable{
@Override
public void run() {
System.out.println(new Date()+": Hiiiii! cron4j!");
}
}
MyCronAppTest
import it.sauronsoftware.cron4j.Scheduler;
public class MyCronAppTest {
public static void main(String[] args) {
MyCronAppTest app = new MyCronAppTest();
try {
app.schedulerSimple();
System.out.println("Press Ctrl+C to stop.");
Thread.sleep(100000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void schedulerSimple() {
Scheduler scheduler = new Scheduler();
// every minute.
scheduler.schedule("* * * * *", new CronTask());
// start cron4j scheduler.
scheduler.start();
}
}
HelloServlet
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import it.sauronsoftware.cron4j.Scheduler;
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void schedulerSimple() {
Scheduler scheduler = new Scheduler();
// every minute.
scheduler.schedule("* * * * *", new CronTask());
// start cron4j scheduler.
scheduler.start();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
MyCronApp app = new MyCronApp();
try {
app.schedulerSimple();
System.out.println("Press Ctrl+C to stop.");
Thread.sleep(100000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
response.getWriter().append("Hello Servlet");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Recommended Posts