xxl-job入门实践


源码在码云上(git@gitee.com:lynch168/spring-boot.git)

1、下载xxl-job源码

xxl-job源码地址:https://github.com/xuxueli/xxl-job

解压导入到Eclipse,如下图所示:

2、修改pom.xml文件
2.1、修改/xxl-job-2.0.2/pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>${maven-source-plugin.version}</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>jar-no-fork</goal>
            </goals>
        </execution>
    </executions>
</plugin>

 

2.2、修改/xxl-job-admin/pom.xml

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring-boot.version}</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

注意:如果jdk为7此步骤忽略;源码默认用jdk7编译,这里改成jdk8。

3、MySQL数据库
新建数据库及相关表结构:
执行xxl-job/doc/db/tables_xxl_job.sql数据库脚本;

修改配置
修改/xxl-job-admin/src/main/resources/xxl-job-admin.properties脚本中关于数据库的配置:

修改/xxl-job-admin/src/main/resources/xxl-job-admin.properties脚本中关于数据库的配置: xxl.job.db.driverClass=com.mysql.jdbc.Driver xxl.job.db.url=jdbc:mysql://localhost:3306/xxl-job?useUnicode=true&characterEncoding=UTF-8
xxl.job.db.user=root xxl.job.db.password=

 

4、运行xxl-job-admin服务
4.1、打成war包然后放到tomcat容器中执行;
mvn clean package -U

4.2、若是调试,则可以直接在IDEA/Eclipse中配置一下tomcat,然后直接运行
启动完成后在浏览器输入:http://localhost:8080/xxl-job-admin/
默认账户:admin/123456
可在/xxl-job-admin/src/main/resources/xxl-job-admin.properties脚本中进行修改配置;

5、任务调度测试
选用/xxl-job/xxl-job-executor-samples/xxl-job-executor-sample-springbootdemo作为客户端测试demo;

5.1、新建JobHandler任务

package com.xxl.job.executor.service.jobhandler; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; 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; /** * 任务Handler示例(Bean模式) * <p> * 开发步骤: * 1、继承"IJobHandler":“com.xxl.job.core.handler.IJobHandler”; * 2、注册到Spring容器:添加“@Component”注解,被Spring容器扫描为Bean实例; * 3、注册到执行器工厂:添加“@JobHandler(value="自定义jobhandler名称")”注解,注解value值对应的是调度中心新建任务的JobHandler属性的值。 * 4、执行日志:需要通过 "XxlJobLogger.log" 打印执行日志; */ @JobHandler(value = "helloJobHandler") @Component public class HelloJobHandler extends IJobHandler { @Override public ReturnT<String> execute(String param) { String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); System.out.println(now + "XXL-JOB, Hello World."); return SUCCESS; } }

 

 

 

5.2、配置
配置脚本/xxl-job/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.properties

xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin

 

5.3、构建打包
$ cd /xxl-job/xxl-job-executor-samples/xxl-job-executor-sample-springboot/
$ mvn clean package -U
$ cd target

5.4、启动多个客户端服务
java -jar xxl-job-executor-sample-springboot-2.0.2.jar --server.port=8901 --xxl.job.executor.port=8801

java -jar xxl-job-executor-sample-springboot-2.0.2.jar --server.port=8902 --xxl.job.executor.port=8802

或者通过IDE启动XxlJobExecutorApplication入口类

自此要确保xxl-job-admin、xxl-job-executor-sample-springboot成功运行。

6、后台管理页面配置任务
6.1、执行器
执行器管理 -> 新建执行器

新建执行器的参数,跟/xxl-job-executor-sample-springboot/src/main/resources/application.properties文件一致,如下:

### xxl-job executor address xxl.job.executor.appname=xxl-job-executor-sample xxl.job.executor.ip= xxl.job.executor.port=9999

6.2、任务
任务管理 -> 新建任务

运行模式:@JobHandler(value = "helloJobHandler")中配置的value

点击"启动"按钮,执行任务,此时在控制台会看到如下打印信息:

2019-04-28 17:02:10XXL-JOB, Hello World. 2019-04-28 17:02:15XXL-JOB, Hello World. 2019-04-28 17:02:20XXL-JOB, Hello World. 2019-04-28 17:02:25XXL-JOB, Hello World. 2019-04-28 17:02:30XXL-JOB, Hello World. 2019-04-28 17:02:35XXL-JOB, Hello World. 2019-04-28 17:02:40XXL-JOB, Hello World. 2019-04-28 17:02:45XXL-JOB, Hello World.

7、查看调度日志


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM