• quartz 关联多个JobDetail和SimpleTrigger多任务调度


    import java.util.Date;
    
    import org.quartz.JobDetail;
    
    import org.quartz.Scheduler;
    
    import org.quartz.SchedulerFactory;
    
    import org.quartz.SimpleTrigger;
    
    import org.quartz.impl.StdSchedulerFactory;
    
    public class SimpleTriggerRunner {
    
    	public static void main(String args[]) {
    
    		try {
    
    			// ①创建一个JobDetail实例,指定SimpleJob
    
    			JobDetail jobDetail1 = new JobDetail("job1_1", "jGroup1",
    					SimpleJob.class);
    
    			// ②通过SimpleTrigger定义调度规则:马上启动,每2秒运行一次,共运行100次
    
    			SimpleTrigger simpleTrigger1 = new SimpleTrigger("trigger1_1",
    					"tgroup1");
    
    			simpleTrigger1.setStartTime(new Date());
    
    			simpleTrigger1.setRepeatInterval(1000);
    
    			simpleTrigger1.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
    			
    			
    			// ①创建一个JobDetail实例,指定SimpleJob
    
    			JobDetail jobDetail2 = new JobDetail("job1_2", "jGroup2",
    					DemoJob.class);
    
    			// ②通过SimpleTrigger定义调度规则:马上启动,每2秒运行一次,共运行100次
    
    			SimpleTrigger simpleTrigger2 = new SimpleTrigger("trigger1_2",
    					"tgroup2");
    
    			simpleTrigger2.setStartTime(new Date());
    
    			simpleTrigger2.setRepeatInterval(3000);
    
    			simpleTrigger2.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
    
    			// ③通过SchedulerFactory获取一个调度器实例
    
    			SchedulerFactory schedulerFactory = new StdSchedulerFactory();
    
    			Scheduler scheduler = schedulerFactory.getScheduler();
    
    			scheduler.scheduleJob(jobDetail1, simpleTrigger1);// ④ 注册并进行调度
    			scheduler.scheduleJob(jobDetail2, simpleTrigger2);// ④ 注册并进行调度
    
    			scheduler.start();// ⑤调度启动
    
    		} catch (Exception e) {
    
    			e.printStackTrace();
    
    		}
    
    	}
    
    }
    //===============================================
    import java.util.Date;
    
    import org.quartz.Job;
    
    import org.quartz.JobExecutionContext;
    
    import org.quartz.JobExecutionException;
    
    public class SimpleJob implements Job {
    
    	// ①实例Job接口方法
    
    	public void execute(JobExecutionContext jobCtx)
    			throws JobExecutionException {
    
    		System.out.println(jobCtx.getTrigger().getName()
    				+ " triggered. time is:" + (new Date()));
    
    	}
    
    }
  • 相关阅读:
    闭包
    关于this
    插件开发(对象级)
    IFC
    flex.css
    js移动端滑动事件
    Android 手机下输入框获取焦点时, 输入法挡住输入框的 bug
    vue 组件化spreadjs预览excel
    feign 熔断工厂 fallbackFactory的简单实现
    bat脚本批量启动程序
  • 原文地址:https://www.cnblogs.com/vaer/p/4754000.html
Copyright © 2020-2023  润新知