所需材料
compile "com.dangdang:elastic-job-lite-core:2.1.5"
compile "com.dangdang:elastic-job-lite-spring:2.1.5"
compile 'org.springframework.cloud:spring-cloud-starter-consul-discovery
compile 'org.springframework.cloud:spring-cloud-starter-consul-config:2.1.1.RELEASE'
compile 'org.springframework.cloud:spring-cloud-starter-consul-discovery:2.1.1.RELEASE'
启用sirius-quartz项目,总体结构如下
server:
port: 8011
spring:
profiles:
active: dev
application:
name: quartz-server
security:
user:
name: admin
password: 12345
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://10.0.21.66:3306/sirius?useUnicode=true&characterEncoding=UTF-8
username: 9now_dev
password: 9now.321
mail:
host: ${mail.config.host}
port: ${mail.config.port}
username: ${mail.config.username}
password: ${mail.config.password}
properties:
mail.smtp.auth: true
mybatis:
type-aliases-package:
com.cloud.sirius.demo.service.entity
mapper-locations:
classpath:mybatis/*.xml
mapper:
mappers:
tk.mybatis.mapper.common.Mapper
notEmpty:
true
spring:
cloud:
consul:
discovery:
hostname: localhost
port: 8500
tags: version=1.0,author=hmdbb
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
enabled: true
service-name: ${spring.application.name}
config:
# 快速失败
fail-fast: true
#启用
enabled: true
#consul上面文件的格式 YAML FILES PROPERTIES KEY-VALUE
format: YAML
# 基本文件夹
prefix: config
# 所有应用程序默认文件夹名
defaultContext: application
#设置分隔符的值,用于使用配置文件在属性源中分隔配置文件名称
profileSeparator: ','
data-key: data
watch:
#调用的频率 1000 毫秒
delay: 1000
logging:
config:
classpath:logback-test.xml
%d %p [%t] (%c:%line\): %m%n
UTF-8
log/sys.log
log/sys.%d.%i.log
30
100MB
%d %p [%t] (%c:%line\): %m%n
UTF-8
自定义抽象类AbstractJob,实现SimpleJob,方便抽象出公共的工具类,以及方便后续aop的使用
package com.cloud.sirius.demo.quartz.job;
import com.cloud.sirius.demo.utils.log.SiriusLogger;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import org.slf4j.Logger;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
public abstract class AbstractJob implements SimpleJob {
protected Logger logger = new SiriusLogger(this.getClass());
// protected void clear() {
// DbContextHolder.clearDbType();
// TraceUtil.INSTANCE.clear();
// }
/**
* 执行定时任务
*/
public abstract void run(ShardingContext context);
@Override
public void execute(ShardingContext context) {
run(context);
}
protected void springBeanAutowiringSupport() {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
}
DemoJob
package com.cloud.sirius.demo.quartz.job;
import com.dangdang.ddframe.job.api.ShardingContext;
import org.springframework.stereotype.Component;
/**
* @author xu.guanguo
* @time 2019/6/28
* @description
*/
@Component("bieYangRemindJob")
public class BieYangRemindJob extends AbstractJob{
@Override
public void run(ShardingContext shardingContext) {
System.out.println(String.format("------Thread ID: %s, 任务总片数: %s, " +
"当前分片项: %s.当前参数: %s,"+
"当前任务名称: %s.当前任务参数: %s" ,
Thread.currentThread().getId(),
shardingContext.getShardingTotalCount(),
shardingContext.getShardingItem(),
shardingContext.getShardingParameter(),
shardingContext.getJobName(),
shardingContext.getJobParameter()));
}
}
zk的配置
package com.cloud.sirius.demo.quartz.config;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration
public class ElasticJobConfig {
@Value("${quartz.app.zkAddress}")
private String zkNodes;
@Value("${quartz.app.namespace}")
private String namespace;
@Bean
public ZookeeperConfiguration zkConfig() {
return new ZookeeperConfiguration(zkNodes, namespace);
}
@Bean(initMethod = "init")
public ZookeeperRegistryCenter regCenter(ZookeeperConfiguration config) {
return new ZookeeperRegistryCenter(config);
}
}
job总管理类
package com.cloud.sirius.demo.quartz.config;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author xu.guanguo
* @time 2019/6/28
* @description
*/
@Configuration
public class SiriusJobs {
@Autowired
private ZookeeperRegistryCenter regCenter;
@Bean(initMethod = "init")
public JobScheduler bieYangRemindJobScheduler(@Autowired @Qualifier("bieYangRemindJob") SimpleJob simpleJob,
@Value("${quartz.app.billCronExpress}") final String cron,
@Value("${quartz.app.shardingCount}") int shardingCount,
@Value("${quartz.app.shardingItemParameters}") String shardingItemParameters) {
return new SpringJobScheduler(simpleJob, regCenter, getLiteJobConfiguration(simpleJob.getClass(), cron, shardingCount, shardingItemParameters));
}
/**
* @Description 任务配置类
*/
private LiteJobConfiguration getLiteJobConfiguration(final Class extends SimpleJob> jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters) {
return LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(JobCoreConfiguration.newBuilder(
jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).build(), jobClass.getCanonicalName())).overwrite(true).build();
}
}
项目的总初始化类BeanInjector
package com.cloud.sirius.demo.quartz.config;
import com.cloud.sirius.demo.base.AbstractInjector;
import com.cloud.sirius.demo.utils.mail.MailConfig;
import com.cloud.sirius.demo.utils.mail.MailUtil;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
@Component
@RefreshScope
public class BeanInjector extends AbstractInjector {
@Autowired
private JavaMailSender javaMailSender;
@Autowired
private MailConfig mailConfig;
@Autowired
private QuartzAppConfig appConfig;
@Bean
@RefreshScope
public MailUtil mailUtil() {
MailUtil mailUtil = new MailUtil(javaMailSender, mailConfig);
return mailUtil;
}
private ZkClient zkClient = null;
@Bean(name = "zkClient")
public ZkClient zkClient() {
if (zkClient != null) {
return zkClient;
}
zkClient = new ZkClient(appConfig.getZkAddress());
return zkClient;
}
}