springmvc+quartz定时任务

Spring-mvc.xml:
< task:annotation-driven >

代码:

@component
public class QuartzJob{

   @Scheduled(cron="0/10 *  *  *  *  ?")//每隔十秒执行一次
   public void  processOds(){
   //待处理数据
     List<String> list=new ArrayList<String>();
		ExecutorService executorService=Executors.newFixedThreadPool(10);
		for (int i = 0; i < list.size(); i++) {
			Runnable run=new Runnable() {
				@Override
				public void run() {
					//处理数据逻辑
					
					
					
				}
			};
			executorService.execute(run);
		}
	
		executorService.shutdown();
		try{
			executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
		}catch(InterruptedException e){
		Thread.currentThread().interrupt();
		}
}
}
分布式工程同时启动定时任务,如何防止不同节点不会处理同一条数据?
1.全局缓存,将数据放入Redis集群中或者MQ中。
2.uuid标识。每条数据插入处理节点生成的uuid,让后根据uuid标识匹配执行。

quartz时间格式
[秒] [分] [小时] [日] [月] [周] [年] 

1  秒  是  0-59    , - * / 
2  分  是  0-59 , - * / 
3 小时  是  0-23   , - * / 
4  日  是  1-31   , - * ? / L W 
5  月  是  1-12 or JAN-DEC   , - * / 
6  周  是  1-7 or SUN-SAT   , - * ? / L # 
7  年  否  empty 或 1970-2099  , - * / 

*表示所有值. 例如:在分的字段上设置 "*",表示每一分钟都会触发。 
? 表示不指定值。使用的场景为不需要关心当前设置这个字段的值。例如:要在每月的10号触发一个操作,但不关心是周几,所以需要周位置的那个字段设置为"?" 具体设置为 0 0 0 10 * ? 
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分每分触发 
0 0/5 14 * * ? 每天下午的 2点到2点59分(整点开始,每隔5分触发) 
0 0/5 14,18 * * ? 每天下午的 2点到2点59分(整点开始,每隔5分触发) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值