spring + quartz 分布式自定义注解
相关技术
本文采用spring + quartz的方案。使用mysql作为任务的持久化,支持分布式。
自定义注解
1.启用定时任务
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(QuartzConfig.class) //引入配置
@Documented
public @interface EnableMScheduling { } //该注解需要放在application启动类上,标识启用定时任务,它的作用就是配置、解析任务以及启动调
2.标识调度类
@Target(value = ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface MScheduleClass { /**
* 任务分组,页面显示作用
* @return
*/
String module() default "系统"; /**
* 描述,提示作用
* @return
*/
String desc() default ""; }
//该注解放置在类上,标识指定的类是一组定时任务,其中需要设置任务分组,描述
3.标识执行的方法
@Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MSchedule { /**
* 任务名称
* @return
*/
String title() default ""; /**
* 调度触发的corn表达式 : 用作Job的触发器,目前只支持一个触发器表达式。
*/
String corn(); /**
* 描述
*/
String desc() default ""; /**
* 参数
*/
String param() default "";
}
//该注解标识在@MScheduleClass标识的类中的方法中,标识指定的方式是任务执行的方法。
配置类
import javax.sql.DataSource; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager; public class QuartzConfig { /**
* 配置任务调度器
* 使用项目数据源作为quartz数据源
* @param jobFactory 自定义配置任务工厂
* @param dataSource 数据源实例
* @return
* @throws Exception
*/
@Bean(destroyMethod = "destroy")
public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource,
ObjectProvider<PlatformTransactionManager> transactionManager) throws Exception { SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
// 项目启动完成后,等待10秒后开始执行调度器初始化
//schedulerFactoryBean.setStartupDelay(10);
// 设置调度器自动运行
schedulerFactoryBean.setAutoStartup(false);
// 设置数据源,使用与项目统一数据源
schedulerFactoryBean.setDataSource(dataSource); PlatformTransactionManager txManager = transactionManager.getIfUnique();
if (txManager != null) {
schedulerFactoryBean.setTransactionManager(txManager);
}
// 设置上下文spring bean name
schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContext");
// 设置配置文件位置
schedulerFactoryBean.setConfigLocation(new ClassPathResource("/quartz.properties"));
return schedulerFactoryBean;
} @Bean
public MScheduleBeanPostProcessor mScheduleBeanPostProcessor() {
return new MScheduleBeanPostProcessor();
}
}
其中dataSource我自己用的阿里的druid。 具体配置自行处理
quartz.properites
#调度器实例名称
org.quartz.scheduler.instanceName = quartzScheduler #调度器实例编号自动生成
org.quartz.scheduler.instanceId = AUTO #持久化方式配置
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX #持久化方式配置数据驱动,MySQL数据库
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate #quartz相关数据表前缀名
org.quartz.jobStore.tablePrefix = QRTZ_ #开启分布式部署
org.quartz.jobStore.isClustered = true
#配置是否使用
org.quartz.jobStore.useProperties = false #分布式节点有效性检查时间间隔,单位:毫秒
org.quartz.jobStore.clusterCheckinInterval = 20000 #线程池实现类
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool #执行最大并发线程数量
org.quartz.threadPool.threadCount = 10 #线程优先级
org.quartz.threadPool.threadPriority = 5 #配置是否启动自动加载数据库内的定时任务,默认true
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
quartz初始化的数据库表在org/quartz/impl/jdbcjobstore/tables_@@platform@@.sql
注解解析beanPosrProcessor
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Set; import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent; import com.mustr.cluster.annotation.MSchedule;
import com.mustr.cluster.annotation.MScheduleClass; import lombok.extern.slf4j.Slf4j; @Slf4j
public class MScheduleBeanPostProcessor implements BeanPostProcessor, ApplicationListener<ContextRefreshedEvent>, DisposableBean { @Autowired
private Scheduler scheduler; private List<MustrTask> tasks = new ArrayList<>(); @Override
public void destroy() throws Exception {
scheduler.shutdown();
} @Override
public void onApplicationEvent(ContextRefreshedEvent event) {
log.info("all scheduler tasks total {}", tasks.size()); try {
//先把原来的都删除
Set<JobKey> jobKeys = scheduler.getJobKeys(GroupMatcher.anyGroup());
scheduler.deleteJobs(new ArrayList<>(jobKeys));
} catch (SchedulerException e1) {
e1.printStackTrace();
} //重新添加新的
tasks.forEach(task -> {
try {
scheduler.scheduleJob(task.getJobDetail(), task.getTrigger());
} catch (SchedulerException e) {
e.printStackTrace();
}
}); try {
scheduler.start(); //启动调度器
} catch (SchedulerException e) {
e.printStackTrace();
}
} @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
MScheduleClass msClass = bean.getClass().getAnnotation(MScheduleClass.class);
if (msClass == null) {
return bean;
} String group = bean.getClass().getSimpleName();
Method[] methods = bean.getClass().getDeclaredMethods();
if (methods == null) {
return bean;
} for (Method method : methods) {
MSchedule mSchedule = method.getAnnotation(MSchedule.class);
if (mSchedule == null) {
continue;
}
hanlderSchedule(group, mSchedule, method, bean);
} return bean;
} private void hanlderSchedule(String group, MSchedule mSchedule, Method method, Object bean) {
String jobName = method.getName(); JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("targetClass", bean);
jobDataMap.put("targetMethod", method.getName());
jobDataMap.put("arguments", mSchedule.param()); JobDetail jobDetail = JobBuilder.newJob(MustrCommonJob.class)
.setJobData(jobDataMap)
.withIdentity(new JobKey(jobName, group))
.withDescription(mSchedule.desc())
.storeDurably()
.build(); Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity(new TriggerKey(jobName, group))
.withDescription(mSchedule.desc())
.withSchedule(CronScheduleBuilder.cronSchedule(mSchedule.corn()).withMisfireHandlingInstructionDoNothing())
.forJob(jobDetail)
.build(); tasks.add(new MustrTask(jobDetail, trigger));
}
}
该类就是解析自定义注解的@MScheduleClass和@MSchedule标识的任务。封装jobDetail和Trigger。
任务job统一使用MustrCommonJob通过反射来执行配置的指定类的指定方法
MustrTask
import org.quartz.JobDetail;
import org.quartz.Trigger; import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter; @Setter
@Getter
@AllArgsConstructor
public class MustrTask { private JobDetail jobDetail;
private Trigger trigger;
}
任务类
import java.io.Serializable; import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.util.MethodInvoker; public class MustrCommonJob implements Job, Serializable{
private static final long serialVersionUID = 8651275978441122356L; @Override
public void execute(JobExecutionContext context) throws JobExecutionException {
Object targetClass = context.getMergedJobDataMap().get("targetClass");
String targetMethod = context.getMergedJobDataMap().getString("targetMethod");
String param = context.getMergedJobDataMap().getString("arguments"); //前置处理
// do .... try {
MethodInvoker methodInvoker = new MethodInvoker();
methodInvoker.setTargetObject(targetClass);
methodInvoker.setTargetMethod(targetMethod);
if (param != null && !"".equals(param)) {
String[] params = param.split(",");
Object[] temp = new Object[params.length];
for (int i = 0; i < params.length; i++) {
temp[i] = params[i];
}
methodInvoker.setArguments(temp);
}
methodInvoker.prepare();
methodInvoker.invoke();
} catch (Exception e) {
e.printStackTrace();
} finally { //后置处理
// do ... 如记录日志
}
} }
该类实现了quartz的job接口。通过反射调用指定的方法
一个简单的demo
import java.io.Serializable;
import java.time.LocalDateTime; import com.mustr.cluster.annotation.MSchedule;
import com.mustr.cluster.annotation.MScheduleClass; @MScheduleClass(module = "系统", desc = "测试组")
public class HelloSchedule implements Serializable{
private static final long serialVersionUID = 3619058186885794136L; /*@MSchedule(corn = "0/30 * * * * ?", desc = "打印hello world")
public void hello() {
System.out.println("hello mustr..... <<<:::>>>" + LocalDateTime.now());
}*/ @MSchedule(corn = "0/10 * * * * ?", desc = "打印hello world")
public void hello1() {
System.out.println("<<<<hello1 mustr..... <<<:::>>>" + LocalDateTime.now());
}
}
最后一步
在程序启动类中加入 @EnableMScheduling注解,启动项目即可看到控制台打印
本文代码:https://github.com/Mustr/mustr-quartz-boot
spring + quartz 分布式自定义注解的更多相关文章
- spring quartz分布式任务计划
spring quartz分布式任务计划 环境: 通过maven管理的spring mvc工程,且已经成功连接数据库. 数据库表结构 /*Table structure for table `qrtz ...
- redis分布式锁-spring boot aop+自定义注解实现分布式锁
接这这一篇redis分布式锁-java实现末尾,实现aop+自定义注解 实现分布式锁 1.为什么需要 声明式的分布式锁 编程式分布式锁每次实现都要单独实现,但业务量大功能复杂时,使用编程式分布式锁无疑 ...
- 使用spring aspect控制自定义注解
自定义注解:这里是一个处理异常的注解,当调用方法发生异常时,返回异常信息 /** * ErrorCode: * * @author yangzhenlong * @since 2016/7/21 */ ...
- spring AOP 和自定义注解进行身份验证
一个SSH的项目(springmvc+hibernate),需要提供接口给app使用.首先考虑的就是权限问题,app要遵循极简模式,部分内容无需验证,用过滤器不能解决某些无需验证的方法 所以最终选择用 ...
- Spring实现封装自定义注解@Trimmed清除字符串前后的空格
在Spring中实现字符串清除的方法有很多,原生方法String自带trim()方法,或者使用StringUtils提供的trim...方法. 通常可以将上面的方式封装成自定义注解的形式去实现来节省更 ...
- Spring Boot中自定义注解+AOP实现主备库切换
摘要: 本篇文章的场景是做调度中心和监控中心时的需求,后端使用TDDL实现分表分库,需求:实现关键业务的查询监控,当用Mybatis查询数据时需要从主库切换到备库或者直接连到备库上查询,从而减小主库的 ...
- Spring Boot实现自定义注解
在Spring Boot项目中可以使用AOP实现自定义注解,从而实现统一.侵入性小的自定义功能. 实现自定义注解的过程也比较简单,只需要3步,下面实现一个统一打印日志的自定义注解: 1. 引入AOP依 ...
- spring boot通过自定义注解和AOP拦截指定的请求
一 准备工作 1.1 添加依赖 通过spring boot创建好工程后,添加如下依赖,不然工程中无法使用切面的注解,就无法对制定的方法进行拦截 <dependency> <group ...
- spring mvc实现自定义注解
实现方式:使用@Aspect实现: 1. 新建注解接口:CheckSign package com.soeasy.web.utils; import org.springframework.core. ...
随机推荐
- 《Web接口开发与自动化测试》学习笔记(一)
一.Django的入门 学习思路:先安装Django,然后在建立一个项目,接着运行这个项目,最后修改一下这个项目的数据,学习一下Django的原理之类的. 1.安装Django $pip instal ...
- leetcode133:3sum-closest
题目描述 给出含有n个整数的数组s,找出s中和加起来的和最接近给定的目标值的三个整数.返回这三个整数的和.你可以假设每个输入都只有唯一解. 例如,给定的整数 S = {-1 2 1 -4}, 目标值 ...
- 编程,向内存0:200~0:23F依次传送数据0~63(3FH),程序中只能使用9条指令,9条指令包括 mov ax,4c00h 和 int 21h
assume cs:code code segment mov bx,020H mov ds,bx mov bx,0 mov cx,63 s:mov [bx],bx inc bx loop s mov ...
- 【Mycat】Mycat核心开发者带你轻松掌握Mycat路由转发!!
写在前面 熟悉Mycat的小伙伴都知道,Mycat一个很重要的功能就是路由转发,那么,这篇文章就带着大家一起来看看Mycat是如何进行路由转发的,好了,不多说了,我们直接进入主题. 环境准备 软件版本 ...
- Goldstone's theorem(转载)
Goldstone's theorem是凝聚态物理中的重要定理之一.简单来说,定理指出:每个自发对称破缺都对应一个无质量的玻色子(准粒子),或者说一个zero mode. 看过文章后,我个人理解这其实 ...
- Internet 网络协议族
1.linux目前支持多种协议族,每个协议族用一个net_porto_family结构实例来表示,在初始化时,会调用sock_register()函数初始化注册到net_families[NPROTO ...
- select限制之文件描述符限制
1.一个进能够打开的最大文件描述符限制.可以通过两种方式修改ulimit -n :获取最大文件描述符个数ulimit -n 2048:修改为2048个 该限制的测试代码: 客户端程序: /* 1.se ...
- MYSQL渗透测试
部分来源于:先知社区 MYSQL-getshell篇 通过日志getshell 查看日志的物理路径(绝对路径) show variables like '%general%'; 打开日志记录内容 se ...
- [web安全原理]PHP命令执行漏洞基础
前言 PHP命令执行漏洞 应用程序的某些功能功能需要调用可以执行系统命令的函数,如果这些函数或者函数的参数被用户控制,就有可能通过命令连接符将恶意命令拼接到正常的函数中,从而随意执行系统命令,这就是命 ...
- 面试官:小伙子,你给我说一下HashMap 为什么线程不安全?
前言:我们都知道HashMap是线程不安全的,在多线程环境中不建议使用,但是其线程不安全主要体现在什么地方呢,本文将对该问题进行解密. 1.jdk1.7中的HashMap 在jdk1.8中对HashM ...