借助ApplicationListener进行Quartz和SpringBoot整合

前言

网上搜索各种博客案例我们可以发现大多数Quartz和Spring的整合都是借助spring的xml配置文件来定义各种bean(Trigger、Scheduler、JobDetail等)来实现Quartz进行作业调度,其实现原理是通过配置文件或者注解来定义各种bean之间的引用关系,从而编码实现Quartz的整合。但是这种方式需要书写的配置文件太多,并且需要添加新的作业进行调度时的可扩展性太差,为此接下来借助Springboot来减少配置,自己编码实现可以更好的进行新增作业借助Quartz调度。

正题(案列)

首先Quartz的三要素是必不可少的(Trigger、Scheduler、JobDetail),
接下来需要考虑的就是如何使得这三者之间的关系通过编码动态实现,而不是像我们学Quartz的时候写demo一样,每写一个JobDetail还得写一个Trigger,还得通过Scheduler.scheduler注册两者。

预先定义Job
将所有的Job实现IntegerCallable接口作为标识,便于动态获取所有的Job实现类

public interface IntegerCallable extends Callable<Integer> {
        int STATUS_CODE_OK = 0;
    }
@Component
public class Duty implements IntegerCallable {
    private static Logger LOGGER = LoggerFactory.getLogger(Duty.class);
    @Autowired
    public MapreduceDuty(MapreduceConfig mapreduceConfig) {
        this.mapreduceConfig = mapreduceConfig;
    }

    public MapreduceDuty() {
    }
    @Override
    public Integer call() throws Exception {
    	//此处定义Job中的execute方法中的执行逻辑即Job所要执行的任务
        return STATUS_CODE_OK;
    }
}

定义TriggerFactory
createTrigge根据配置文件中不同的前缀创建不同的Trigger

public static class TriggerFactory {
	private static Trigger createTrigger(){}
	private static Trigger createCronTrigger{}
	private static Trigger createIntervalTrigger{}
	private static Trigger createOneTimeTrigger{}
}

配置文件格式
将类名写成如xxx-xxxx-xxx的格式,
duty-on-call=cron:* * * * * ? 定时执行(cron表达式)
xxx-xxxx-xxx=interval:2h 间隔执行
xxx-xxxx-xxx=one-time 执行一次

统一Job抽象类

public class UnifiedJob implements Job {
	public void execute(){
		//这里反射调用每个标识Job即IntegerCallable子类的call方法
	}
	public  UnifiedJob newUnifiedJob(){
		//创建Job
	}
}

实现ApplicationListener 完成动态调度
根据ApplicationListener的原理,其onApplicationEvent(ContextRefreshedEvent event) 方法会在初始化所有的bean之后被调用,因此我们可以在这里进行scheduler的创建、启动,以及注册trigger和job

public class JobScheduler implements ApplicationListener<ContextRefreshedEvent> {

	@Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        refresh();
    }
	private void refresh() {
		//1.获取所有的标识Job即IntegerCallable子类,生成UnifiedJob
		//2.创建每个Job的Trigger 关联配置文件的表达式
		//3.创建scheduler
		//4.注册trigger和job
		//5.启动scheduler
	}
	@PreDestroy
    public void onStop() {
		//关闭scheduler
    }
}

以上只是巧妙的借助ApplicationListener来实现Quartz的整合,只写了大概的思路,写的有点糙,大家有更好的整合方法欢迎分享交流。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以通过实现 `ApplicationListener` 接口来添加 `SpringBoot` 启动时的监听器。以下是一个示例: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication app = new SpringApplication(MyApplication.class); app.addListeners(new MyApplicationListener()); // 添加自定义的监听器 app.run(args); } // 自定义的监听器 public static class MyApplicationListener implements ApplicationListener<ApplicationEvent> { @Override public void onApplicationEvent(ApplicationEvent event) { // 在应用程序启动时触发的逻辑处理 System.out.println("应用程序启动了!"); } } } ``` 在上面的示例中,我们创建了一个 `MyApplicationListener` 类,实现了 `ApplicationListener` 接口,并重写了其 `onApplicationEvent` 方法,用于在应用程序启动时执行自定义逻辑。 在 `main` 方法中,我们创建了一个 `SpringApplication` 对象,并通过 `addListeners` 方法将自定义的监听器添加到应用程序中,然后调用 `run` 方法启动应用程序。 当应用程序启动时,`onApplicationEvent` 方法将被调用,并执行我们定义的逻辑处理。这里只是简单地打印一条消息,您可以根据实际需求进行相应的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值