zoukankan      html  css  js  c++  java
  • quartz简单定时任务【可以处理完一个任务才开启下一个线程】【我】

    maven jar project项目一个

    pom文件:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.test</groupId>
      <artifactId>test-jar-quartz</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      
      
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.quartz-scheduler</groupId>
                <artifactId>quartz</artifactId>
                <version>2.2.3</version>
            </dependency>
        </dependencies>
    
    </project>

    java代码:

    package com.test1;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.quartz.DisallowConcurrentExecution;
    import org.quartz.Job;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    
    @DisallowConcurrentExecution//此注解目的为一定是已经执行完一个任务才按照时间去开启下一个任务
    public class MyJob implements Job {
     
        public void execute(JobExecutionContext context) throws JobExecutionException {
            //打印当前的执行时间,格式为yyyy-MM-dd HH:mm:ss
            // 编写具体的业务逻辑。
            Date date=new Date();
            SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//规范格式
            System.out.println("当前时间为:"+sf.format(date));
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Hello World!---线程名---: "+Thread.currentThread().getName()+"--当前对象--:"+this);
        }
     
    }
    package com.test1;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.quartz.JobBuilder;
    import org.quartz.JobDetail;
    import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
    import org.quartz.SchedulerFactory;
    import org.quartz.SimpleScheduleBuilder;
    import org.quartz.Trigger;
    import org.quartz.TriggerBuilder;
    import org.quartz.impl.StdSchedulerFactory;
     
    public class MyScheduler {
        public static void main(String[] args) throws SchedulerException
        {
            //创建一个JobDetail实例,将该实例与MyJob Class绑定。
            JobDetail jobDetail=JobBuilder.newJob(MyJob.class).withIdentity("myJob", "group1").build();
            //创建一个Trigger实例,定义该Job立即执行,并且每隔3秒钟重复一次,
            Trigger trigger=TriggerBuilder
                    .newTrigger()
                    .withIdentity("myTrigger","group1")
                    .startNow()
                    .withSchedule(SimpleScheduleBuilder
                            .simpleSchedule()
                            .withIntervalInSeconds(3)
                            .repeatForever())
                    .build();
            SchedulerFactory sf=new StdSchedulerFactory();
            Scheduler scheduler=sf.getScheduler();
            scheduler.start();
            Date date=new Date();
            SimpleDateFormat sfd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//规范格式
            //System.out.println("当前时间为:"+sfd.format(date));
            scheduler.scheduleJob(jobDetail, trigger);
        }
    }
  • 相关阅读:
    js正则表达式常见规则整理
    struts2标签 遍历map集合
    RabbitMQ面试问题
    vue基础学习
    flowableの历史查询
    flowableの日志打印
    flowableのID生成器
    flowableの流程发起人
    SpringBoot+Dubbo(XML配置方式)
    linux安装zookeeper伪分布式
  • 原文地址:https://www.cnblogs.com/libin6505/p/10571623.html
Copyright © 2011-2022 走看看