springboot+quartz定时任务,实现从页面传时间进行任务调度

pom.xml

<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.gqbj</groupId>
	<artifactId>quartz-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>


	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
		<version>2.0.4.RELEASE</version>
	</parent>
	<dependencies>

		<!-- pom文件 Spring Quartz依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-quartz</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>6</source>
					<target>6</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Application

package com.gqbj.quartz_demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class Application
{
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

MyJob


import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 宫崎不骏
 * @className MyJob
 * @Version 1.0
 * @Description: TODO
 * @date 2020/1/720:50
 */
public class MyJob implements Job {

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println(jobExecutionContext.getJobDetail());
        System.out.println("我是业务,调用时间:" + sdf.format(new Date()));
    }
}

QuartzSchedulerManager


import com.gqbj.quartz_demo.task.MyJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

/**
 * @author 宫崎不骏
 * @className QuartzSchedulerManager
 * @Version 1.0
 * @Description: TODO
 * @date 2020/1/720:52
 */
@Configuration
public class QuartzSchedulerManager {

    // 任务调度
    @Autowired
    private Scheduler scheduler;
    /**
     * 开始执行定时器
     *
     * @throws SchedulerException
     */
    public void startJob(Date time) throws SchedulerException {
        startJobTest(scheduler,time);
        scheduler.start();
    }

    /**
     *
     * @Title: startJobTest
     * @Description: 启动 定时器 test
     * @param scheduler
     * @throws SchedulerException    参数
     * void    返回类型
     */
    private void startJobTest(Scheduler scheduler , Date time) throws SchedulerException {
        System.out.println(time + "TIME");
        // 通过JobBuilder构建JobDetail实例,JobDetail规定只能是实现Job接口的实例
        // JobDetail 是具体Job实例
        JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity("job1", "group1").build();
        // 基于表达式构建触发器
        SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule();

        // CronTrigger表达式触发器 继承于Trigger
        // TriggerBuilder 用于构建触发器实例
        SimpleTrigger simpleTrigger = TriggerBuilder.newTrigger().withIdentity("job1", "group1")
                .withSchedule(simpleScheduleBuilder).startAt(time).build();
        scheduler.scheduleJob(jobDetail, simpleTrigger);
    }
}

testController


import com.gqbj.quartz_demo.config.QuartzSchedulerManager;
import org.quartz.SchedulerException;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author 宫崎不骏
 * @className testController
 * @Version 1.0
 * @Description: TODO
 * @date 2020/1/720:53
 */
@RestController
public class testController {
    @Resource
    private QuartzSchedulerManager quartzSchedulerManager;

    @RequestMapping("/startJob")
    public void start(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date time) throws SchedulerException {
        System.out.println("开启定时器");
        quartzSchedulerManager.startJob(time);
    }
}

postman调用

在这里插入图片描述

控制台

在这里插入图片描述

大功告成!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值