Java timer (to realize automatic task execution on the 1st of every month, every day, every 15 minutes)

need:

In tomcat, the specified tasks are automatically executed on the first of every month, every day, and every minute.

Development environment:

java1.7 + tomcat

Implementation ideas:

In tomcat, add a listener and set a scheduled task in the listener.

1. Monitor:

Create a new listener class, impnts ServletContextListener, and implement the methods.

explain:

1.1: Create a base time defaultdate (8 o'clock every day) for reference, and how long the operation will be performed after this time.

1.2 : schedule(task, firstTime, period); Method parameter introduction:

task: TimerTask task, you can create a new one in the form of an internal anonymous class (of course, you can also create a class in an external class for writing tasks, which is troublesome to write), implement the run() method, and write what you want to execute in Run Just do it.

firstTime: The first execution time of the task. When the system time is greater than firstTime, a task will be executed immediately. When the system time is less than firstTime, it will not execute until the time is equal to firstTime. So using schedule to implement timed tasks, the most important thing is to control this firstTime.

period: Execution period, in milliseconds. One day writing: 24 * 60 * 60 * 1000

1.3 : How to judge it is the 1st of every month? Using Calendar's Calendar.DAY_OF_MONTH, the return value is 1 on the first day of each month. Execute the judgment every day, and on the 1st of each month, it can be executed once a month

Java listener code:

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;


public class SendWsListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("定时发送Xml信息监听--已关闭!");
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		calendar.set(year, month, day, 8, 00, 00);
		// 当天8点(默认执行时间)
		Date defaultdate = calendar.getTime();
		Date sendDate = null;
		if (defaultdate.before(new Date())) {
			// 若当前时间超过了defaultdate时间,当天不再执行,则将执行时间sendDate改为明天8点
			calendar.add(Calendar.DATE, 1);
			sendDate = calendar.getTime();
		}else {
			// 若当前时间没有超过defaultdate时间,则将执行时间sendDate改为defaultdate
			sendDate = defaultdate;
		}

		/**
		 * ----------------每刻任务 ---------------- 
		 * 启动服务器后,若此时时间没过8点,等待。到了8点自动执行一次,15分钟后再执行一次,周而复始
		 * 启动服务器后,若此时时间超过8点,会立刻执行一次,等到15分钟后再次执行一次,周而复始 到了第二天,不会再判断是否是8点,这个开始时间,只会判断一次而已
		 */
		Timer qTimer = new Timer();
		qTimer.schedule(new TimerTask() {

			@Override
			public void run() {
				System.out.println("每刻任务已执行");
				// TODO 写你的逻辑
			}
		}, defaultdate, 15 * 60 * 1000);// 定时每15分钟
		System.out.println("每刻定时发送Xml信息监听--已启动!");

		/**
		 * ----------------每日任务 ---------------- 
		 * 启动服务器后,若此时时间没过8点,等待。到了8点自动执行一次,24小时后(第二天8点)再执行一次,周而复始
		 * 启动服务器后,若此时时间已经超过8点,则等到24小时后(第二天8点)才执行一次,周而复始
		 */
		Timer dTimer = new Timer();
		dTimer.schedule(new TimerTask() {

			@Override
			public void run() {
				System.out.println("每日任务已经执行");
				// TODO 写你的逻辑
			}
		}, sendDate, 24 * 60 * 60 * 1000);// 定时24小时:24 * 60 * 60 * 1000
		System.out.println("每日定时发送Xml信息监听--已启动!");

		/**
		 * ----------------每月任务 ---------------- 
		 * 启动服务器后,若此时时间没过8点,等待。到了8点自动执行判断是否是当前月份的1号,若是则执行一次,
		 * 24小时后(第二天8点)再执行一次判断(每月1号以后后的29天或30天后才会是下月1号,再执行一次),周而复始 启动服务器后,若此时时间已经超过8点,会立刻执行一次,等到下个月1号再次执行一次,周而复始
		 */
		Timer mTimer = new Timer();
		mTimer.schedule(new TimerTask() {

			@Override
			public void run() {
				Calendar c = Calendar.getInstance();
				int day = c.get(Calendar.DAY_OF_MONTH);
				System.out.println("月任务 判断中");
				if (day == 1) {
					// 每天执行,若为每月1号才执行
					System.out.println("月任务执行已执行");
					// TODO 写你的逻辑
				}

			}
		}, sendDate, 24 * 60 * 60 * 1000);// 每天执行一次检查

		System.out.println("每月定时发送Xml信息监听--已启动!");

	}

}

 

 

2. Set the listening file in web.xml:

<web-app>  
  <listener>
    <listener-class>com.today.ems.listener.SendWsListener</listener-class>
  </listener>
</web-app>

3. Redeploy the project and start tomcat to execute it automatically.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324140591&siteId=291194637