springBoot项目中引入starter
项目引入xxljob,仅需要导入对应的starter包,即可进行快速开发
<dependency>
<groupId>com.ydl</groupId>
<artifactId>xxl-job-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
xxl-job-spring-boot-starter
目录结构:
pom.xml中仅导入对应的xxl-job-spring-boot-autoconfiguration依赖
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>xxl-job-starter</artifactId>
<groupId>com.xiaozhen</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>xxl-job-spring-boot-starter</artifactId>
<dependencies>
<dependency>
<groupId>com.ydl</groupId>
<artifactId>xxl-job-spring-boot-autoconfiguration</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
pom.properties
version=0.0.1-SNAPSHOT
groupId=com.xiaozhen
artifactId=xxl-job-spring-boot-starter
xxl-job-spring-boot-autoconfiguration
目录文件
xxlJobAutoConfiguration为配置类,springBoot启动时会加重
/**
* 注解解释:
*
* @Configuration //指定这个类是一个配置类
* @ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
* @AutoConfigureAfter //指定自动配置类的顺序
* @Bean //给容器中添加组件
* @ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置
* @EnableConfigurationProperties //让xxxProperties生效加入到容器中
*/
@Component
@EnableConfigurationProperties({XxlJobProperties.class})
public class XxlJobAutoConfiguration {
private static Logger logger = LoggerFactory.getLogger(XxlJobAutoConfiguration.class);
@Resource
private XxlJobProperties xxlJobProperties;
public XxlJobAutoConfiguration() {
}
/**
* 配置信息
**/
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
logger.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(this.xxlJobProperties.getAdminAddresses());
xxlJobSpringExecutor.setAppName(this.xxlJobProperties.getExecutorAppName());
xxlJobSpringExecutor.setIp(this.xxlJobProperties.getExecutorIp());
xxlJobSpringExecutor.setPort(this.xxlJobProperties.getExecutorPort());
xxlJobSpringExecutor.setAccessToken(this.xxlJobProperties.getAccessToken());
xxlJobSpringExecutor.setLogPath(this.xxlJobProperties.getExecutorLogPath() + this.xxlJobProperties.getExecutorAppName());
xxlJobSpringExecutor.setLogRetentionDays(this.xxlJobProperties.getExecutorLogRetentionDays());
return xxlJobSpringExecutor;
}
}
xxlJobProperties
@ConfigurationProperties(
prefix = "xxl.job"
)
public class XxlJobProperties {
private String adminAddresses = "http://localhost:8093/xxl-job-admin";
private String accessToken;
private String executorAppName = "xxl-job-executor-default";
private String executorIp;
private int executorPort = 9999;
private String executorLogPath = "/data/applogs/xxl-job/jobhandler/";
private int executorLogRetentionDays = 30;
public XxlJobProperties() {
}
}
pom.xml导入相关的jar包
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>xxl-job-starter</artifactId>
<groupId>com.xiaozhen</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.ydl</groupId>
<artifactId>xxl-job-spring-boot-autoconfiguration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>xxl-job-spring-boot-autoconfiguration</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- xxl-job-core -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
</project>
spring.factories文件(spring启动的时候会加载此文件中的配置类)
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ydl.xxljobspringbootautoconfiguration.XxlJobAutoConfiguration
另外这个spring.factories文件还可配置监听类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.alibaba.boot.dubbo.autoconfigure.DubboAutoConfiguration
org.springframework.context.ApplicationListener=\
com.alibaba.boot.dubbo.context.event.OverrideDubboConfigApplicationListener,\
com.alibaba.boot.dubbo.context.event.WelcomeLogoApplicationListener,\
com.alibaba.boot.dubbo.context.event.AwaitingNonWebApplicationListener
public class AwaitingNonWebApplicationListener implements ApplicationListener<ApplicationReadyEvent> {
@Order //最低优先级确保最后执行
public class OverrideDubboConfigApplicationListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
@Order(LoggingApplicationListener.DEFAULT_ORDER + 1)
public class WelcomeLogoApplicationListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {