参考git上面的 springboot demo
创建maven工程:
pom:
<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.toov5</groupId> <artifactId>springboot-xxl-job</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <javax.servlet-api.version>3.0.1</javax.servlet-api.version> <jsp-api.version>2.2</jsp-api.version> <spring.version>4.3.14.RELEASE</spring.version> <jackson.version>2.9.4</jackson.version> <aspectjweaver.version>1.8.13</aspectjweaver.version> <slf4j-api.version>1.7.25</slf4j-api.version> <freemarker.version>2.3.23</freemarker.version> <junit.version>4.12</junit.version> <jetty-server.version>9.2.24.v20180105</jetty-server.version> <hessian.version>4.0.51</hessian.version> <httpclient.version>4.5.5</httpclient.version> <commons-exec.version>1.3</commons-exec.version> <commons-collections4.version>4.1</commons-collections4.version> <commons-lang3.version>3.7</commons-lang3.version> <commons-email.version>1.5</commons-email.version> <c3p0.version>0.9.5.2</c3p0.version> <mysql-connector-java.version>5.1.45</mysql-connector-java.version> <mybatis-spring.version>1.3.1</mybatis-spring.version> <mybatis.version>3.4.5</mybatis.version> <groovy-all.version>2.4.13</groovy-all.version> <quartz.version>2.3.0</quartz.version> <spring-boot.version>1.5.10.RELEASE</spring-boot.version> </properties> <dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot (依赖管理:继承一些默认的依赖,工程需要依赖的jar包的管理,申明其他dependency的时候就不需要version) --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- jetty --> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>${jetty-server.version}</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-util</artifactId> <version>${jetty-server.version}</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-http</artifactId> <version>${jetty-server.version}</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-io</artifactId> <version>${jetty-server.version}</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- spring-boot-starter-web (spring-webmvc + tomcat) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- xxl-job-core --> <dependency> <groupId>com.xuxueli</groupId> <artifactId>xxl-job-core</artifactId> <version>1.9.2-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <!-- spring-boot-maven-plugin (提供了直接运行项目的插件:如果是通过parent方式继承spring-boot-starter-parent则不用此插件) --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
controller
注:
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
是调度中心的地址
修改job名字:与注解一致
config 通过config这个类 读取properties中的信息 然后注入到spring boot 容器中
项目结构:
Icontroller (做spring boot测试用的)
package com.toov5.api.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class IndexController { @RequestMapping("/index") public String index() { return "ok"; } }
config (读取 properties文件 并且注入到spring 容器中的)
package com.toov5.job.executor.core.config; import com.xxl.job.core.executor.XxlJobExecutor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; /** * xxl-job config * * @author xuxueli 2017-04-28 */ @Configuration @ComponentScan(basePackages = "com.xxl.job.executor.service.jobhandler") public class XxlJobConfig { private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class); @Value("${xxl.job.admin.addresses}") private String adminAddresses; @Value("${xxl.job.executor.appname}") private String appName; @Value("${xxl.job.executor.ip}") private String ip; @Value("${xxl.job.executor.port}") private int port; @Value("${xxl.job.accessToken}") private String accessToken; @Value("${xxl.job.executor.logpath}") private String logPath; @Value("${xxl.job.executor.logretentiondays}") private int logRetentionDays; @Bean(initMethod = "start", destroyMethod = "destroy") public XxlJobExecutor xxlJobExecutor() { logger.info(">>>>>>>>>>> xxl-job config init."); XxlJobExecutor xxlJobExecutor = new XxlJobExecutor(); xxlJobExecutor.setAdminAddresses(adminAddresses); xxlJobExecutor.setAppName(appName); xxlJobExecutor.setIp(ip); xxlJobExecutor.setPort(port); xxlJobExecutor.setAccessToken(accessToken); xxlJobExecutor.setLogPath(logPath); xxlJobExecutor.setLogRetentionDays(logRetentionDays); return xxlJobExecutor; } }
Job
package com.toov5.TestJob; import org.springframework.stereotype.Component; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.handler.IJobHandler; import com.xxl.job.core.handler.annotation.JobHandler; @JobHandler(value="myJob") @Component public class MyJob extends IJobHandler { @Override public ReturnT<String> execute(String param) throws Exception { System.out.println("XXL-Job传递的参数是:"+param); return SUCCESS; } }
logback:
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false" scan="true" scanPeriod="1 seconds"> <contextName>logback</contextName> <property name="log.path" value="/data/applogs/xxl-job/xxl-job-executor-sample-springboot.log"/> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter" > <level>WARN</level> </filter>--> <encoder> <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${log.path}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}.%d{yyyy-MM-dd}.zip</fileNamePattern> </rollingPolicy> <encoder> <pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n </pattern> </encoder> </appender> <root level="info"> <appender-ref ref="console"/> <appender-ref ref="file"/> </root> <!--<logger name="com.xxl.job.executor.service.info" level="WARN" additivity="false"> <appender-ref ref="console"/> <appender-ref ref="file"/> </logger>--> </configuration>
启动admin的server,然后启动job的spring boot
运行成功:
相关配置 入config 、 logback application.properties 参考github上的demo 修改就可以了