zoukankan      html  css  js  c++  java
  • quartz之hello(java)

    quartz    任务调度框架

         简单的说:就是在特定的时间,干指定的事件,然后具体到某个对象去做

    quartz初之体验:

      1.pom.xml文件(导入jar包)

    <dependencies>
    		<dependency>
    			<groupId>org.quartz-scheduler</groupId>
    			<artifactId>quartz</artifactId>
    			<version>2.2.1</version>
    		</dependency>
    		<dependency>
    			<groupId>org.quartz-scheduler</groupId>
    			<artifactId>quartz-jobs</artifactId>
    			<version>2.2.1</version>
    		</dependency>
    		<dependency>
    			<groupId>org.slf4j</groupId>
    			<artifactId>slf4j-log4j12</artifactId>
    			<version>1.7.12</version>
    		</dependency>
    	</dependencies>
    

      2. hello   创建一个任务调度

    public static void main(String[] args) throws Exception {
    		//创建定时器对象
    		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
    		//开启定时器对象
    		scheduler.start();
    		System.out.println("1234");
    		//关闭定时器
    		scheduler.shutdown();
    	}

    在上面的基础上进行添加触发器和工作对象

      创建一个类来实现job方法

    public class HelloJob implements Job {
    	public void execute(JobExecutionContext context)
    			throws JobExecutionException {
    		System.out.println("test01");
    	}
    }
    

      使用触发器来调用工作对象

    	public static void main(String[] args) throws Exception {
    		// 创建定时器对象
    		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
    		// 定义一个工作对象
    		JobDetail job = JobBuilder.newJob(HelloJob.class)
    				.withIdentity("job1", "group1").build();
    		// 定义一个触发器
    		Trigger withSchedule = TriggerBuilder.newTrigger()
    				.withIdentity("trigger1", "group1").startNow()
    				.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(1))
    				.build();
    		scheduler.scheduleJob(job, withSchedule);
    		// 开启定时器对象
    		scheduler.start();
    		// 关闭定时器
    		// scheduler.shutdown();
    	}
    

      

    对象 TriggerBuilder 启动任务时间
      startNow 立即启动
      startAt (Date) 指定时间启动
    对象 SimpleScheduleBuilder 进行简单任务重复执行
      repeatSecondly …() 多少秒后重复执行
      repeatminutely …() 多少分钟后重复执行
      repeatHourly …() 多少小时后重复执行
    

      使用上面的会产生对于时间的处理达不到理想的需求

    public static void main(String[] args) throws Exception {
    		// 创建定时器对象
    		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
    		// 定义一个工作对象
    		JobDetail job = JobBuilder.newJob(HelloJob.class)
    				.withIdentity("job1", "group1").build();
    		// 定义一个触发器  每一秒都需要进行触发
    		Trigger withSchedule = TriggerBuilder.newTrigger()
    				.withIdentity("trigger1", "group1").startNow()
    				.withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ?"))
    				.build();
    		scheduler.scheduleJob(job, withSchedule);
    		// 开启定时器对象
    		scheduler.start();
    		// 关闭定时器
    		// scheduler.shutdown();
    	}
    

      

     使用日历的模式进行处理 这样就会是的我们对于定时的时间可以实现我们的大部分的需求

    那些字符代表什么意思呢  从位数上讲

    1. Seconds 秒
    2. Minutes 分钟
    3. Hours 小时
    4. Day-of-Month 月中的天
    5. Month 月
    6. Day-of-Week 周中的天
    7. Year (optional field) 年(可选的域)
    

      其中使用通配符来进行设置(*,/,?,L,w,#)

    通配符('*')
      可以被用来表示域中“每个”可能的值。因此在"Month"域中的*表示每个月,
    而在 Day-Of-Week 域中的*则表示“周中的每一天”。
          '/'   字符

      表示值的增量,例如, 如果分钟域中放入'0/15',它表示“每隔 15 分钟,
      从 0 开始”,如果在份中域中使用'3/20',则表示“小时中每隔 20 分钟,从第 3 分钟开
      始”或者另外相同的形式就是'3,23,43'。

      '?'字符

      可以用在 day-of-month 及 day-of-week 域中,它用来表示“没有指定值”。
      这对于需要指定一个或者两个域的值而不需要对其他域进行设置来说相当有用

      'L'字符

      可以在 day-of-month 及 day-of-week 中使用,这个字符是"last"的简写,
      但是在两个域中的意义不同。例如,在 day-of-month 域中的"L"表示这个月的最后一天,
      即,一月的 31 日,非闰年的二月的 28 日。如果它用在 day-of-week 中,则表示"7"或
      者"SAT"。但是如果在 day-of-week 域中,这个字符跟在别的值后面,则表示"当月的最
      后的周 XXX"。例如:"6L" 或者 "FRIL"都表示本月的最后一个周五。当使用'L'选项时,
      最重要的是不要指定列表或者值范围,否则会导致混乱

      'W' 字符

      用来指定距离给定日最接近的周几(在 day-of-week 域中指定)。例如:
      如果你为 day-of-month 域指定为"15W",则表示“距离月中 15 号最近的周几”

      '#'字符

      表示表示月中的第几个周几。例如:day-of-week 域中的"6#3" 或者 "FRI#3"
      表示“月中第三个周五”。

  • 相关阅读:
    项目实战12.1—企业级监控工具应用实战-zabbix安装与基础操作
    项目实战11—企业级nosql数据库应用与实战-redis的主从和集群
    项目10.2-企业级自动化运维工具---puppet详解
    项目实战10.1—企业级自动化运维工具应用实战-ansible
    项目实战9—企业级分布式存储应用与实战MogileFS、FastDFS
    项目实战2.3-Nginx的“远方表哥”—Tengine
    项目实战2.2—nginx 反向代理负载均衡、动静分离和缓存的实现
    项目实战8.2-Linux下Tomcat开启查看GC信息
    项目实战8.1—tomcat企业级Web应用服务器配置与会话保持
    项目实战7—Mysql实现企业级数据库主从复制架构实战
  • 原文地址:https://www.cnblogs.com/fjkgrbk/p/quartz_java.html
Copyright © 2011-2022 走看看