spring integrated quartz

1. Usage scenarios

  1. Timed tasks
    Periodic execution (how long does it take)
    Timed execution (what time every day)

  2. Data exchange
    between systems Data between two systems

  3. Data sorting,
    report printing...

2. Corn expression

Corn expression online generation

秒 分钟 小时
0 0/30 0/1 * * ? 代表每天每30分钟运行一次

0 0/3 0/1 * * ?  代表每天每3分钟运行一次

0/1 0/1 0/1 * * ? 代表每天每1秒钟运行一次

0 11 23 * * ?   代表每天晚上23:11运行一次
"0 0 12 * * ?" 每天中午12点触发

"0 15 10 ? * *" 每天上午10:15触发

"0 15 10 * * ?" 每天上午10:15触发

"0 15 10 * * ? *" 每天上午10:15触发

"0 15 10 * * ? 2005" 2005年的每天上午10:15触发

"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发


"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发

"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发


"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发


"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发

"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发

"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发

"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发

"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发


每天早上6点
0 6 * * *

每两个小时
0 */2 * * *

晚上11点到早上7点之间每两个小时,早上八点
0 23-7/2,8 * * *

每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 11 4 * 1-3

1月1日早上4点
0 4 1 1 *

秒 分??时???日??月 星期
0??30 11,3??????*??1-5??11:30 和3:30

0 45 23,11 * * ? 每天11:45和23:45触发

3. Simple to use

The core annotation is scheduled, with corn expression written inside, and the bean is dropped into the ioc

  • jar package, need to integrate spring to use

Here is the operation with the controller, the method is no parameter and no return, annotation method

  • Head naming
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
<!-- quartz注解驱动 -->
	<task:annotation-driven/>
package com.test.action;


import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

@Controller
public class DemoAction {
    
    
	
	/**
	 * 系统间的数据交换 (放到公共的地方法)
	 * 数据整理
	 * 
	 */
	
	boolean task1Flag = true;
	
	// 任务积压  很短
	@Scheduled(cron = "0/1 * * * * ? ")
	public void show() {
    
    
		if (task1Flag) {
    
    
			task1Flag = false;
			System.out.println("hhhhh to do sth..");
			task1Flag = true;
		}
		
	}

}

Starting the server will...

4. Precautions

If quartz is set to execute once per second, it will cause a task backlog (1s loses the method of quartz takeover, the execution of the service inside may be greater than 1s), and the server memory will explode.
To solve it, make a flag like above.
However, each method cannot be judged, marked, and appears redundant.

So use aop to surround the quartz in the controller.

package com.test.aspect;

import java.lang.reflect.Field;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;

/**
 * 防止定时任务积压 切面
 */
public class TaskAspect {
    
    
	//环绕
	public Object doRound(ProceedingJoinPoint pjp){
    
    
		//返回结果
		Object returnObj = null;
		
//		Object[] args = pjp.getArgs();
//		Class class1 = pjp.getClass();
		
		//目标对象
		Object target = pjp.getTarget();
//		Object this1 = pjp.getThis();
		
		//获得目标方法名
		Signature sig = pjp.getSignature();
        MethodSignature msig = null;
        if (!(sig instanceof MethodSignature)) {
    
    
            throw new IllegalArgumentException("该注解只能用于方法");
        }
        msig = (MethodSignature) sig;
        
        //方法名称
        String methodName = msig.getName();
        
        //根据方法名称获得对应的属性  task1Flag, task2Flag... 
        try {
    
    
			Field field = target.getClass().getDeclaredField(methodName+"Flag");
			//设置允许访问
			field.setAccessible(true);
			
			//获得属性值
			boolean flag = Boolean.parseBoolean(field.get(target).toString());
			
			if(flag){
    
    
				//将标记改成false
				field.set(target, false);
				//执行目标
				try {
    
    
					returnObj = pjp.proceed();
				} catch (Throwable e) {
    
    
					e.printStackTrace();
				}
				//将标记改成true
				field.set(target, true);
			}else{
    
    
				System.out.println("目标正在执行......");
			}
		} catch (SecurityException e1) {
    
    
			e1.printStackTrace();
		} catch (NoSuchFieldException e1) {
    
    
			e1.printStackTrace();
		} catch (IllegalArgumentException e) {
    
    
			e.printStackTrace();
		} catch (IllegalAccessException e) {
    
    
			e.printStackTrace();
		} 
		
		return returnObj;
	}
	
}

Guess you like

Origin blog.csdn.net/qq_44783283/article/details/108987973