[LINUX] Let's make a cron program in Java! !! (Task Scheduler)

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

Task class

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!");
	  }

}

Scheduler class

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();
	  }
}

Bonus (Scheduler using Servlet)

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

Let's make a cron program in Java! !! (Task Scheduler)
Let's make a combination calculation in Python
Let's run a Bash script in Java
Let's make a spot sale service 9 (Task Queue edition)
Register a task in cron for the first time
Let's make a Discord Bot.
Make a bookmarklet in Python
Let's make a spot sale service 4 (in Python mini Hack-a-thon)
Let's make a number guessing game in your own language!
Let's make a remote rumba [Hardware]
Let's make a remote rumba [Software]
Let's make a breakout with wxPython
Let's make a spot sale service 1
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
Make a curtain generator in Blender
Let's make a spot sale service 3
When writing a program in Python
[Ev3dev] Let's make a remote control program by Python with RPyC protocol
Let's make a shiritori game with Python
I made a payroll program in Python!
Let's make a voice slowly with Python
Write a Caesar cipher program in Python
Let's make a simple language with PLY 1
Let's make a web framework with Python! (1)
Let's make a tic-tac-toe AI with Pylearn 2
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Let's make a Backend plugin for Errbot
Run cbc of "Let's make a normal compiler" with Java 8 or later + 64bit
I wrote a class in Python3 and Java
A program that removes duplicate statements in Python
Make a copy of the list in Python
Make a cocos2d game in a pixel double window
Make a rock-paper-scissors game in one line (python)
A simple Pub / Sub program note in Python
Let's make some notification processing samples in Python
Let's replace UWSC with Python (5) Let's make a Robot
Make a joyplot-like plot of R in python
Let's make a module for Python using SWIG
I made a Caesar cryptographic program in Python.
Let's make a leap in the manufacturing industry by utilizing the Web in addition to Python