Coder Social home page Coder Social logo

xxl-job-spring-boot-starter's Introduction

简介

XXL-JOB Spring Boot 开箱即用。

如果你还没有尝试过XXL-JOB,可以去XXL-JOB 官网看下,或者看我写的分布式任务调度XXL-JOB初体验

功能

  • 与SpringBoot无缝集成,实现接近零配置
  • 在代码层面实现任务的增删改查和简单管理,方便动态的修改任务

使用

1. 将xxl-job-spring-boot-starter依赖项添加到项目中:

Maven

...
<repositories>
	<repository>
	  <snapshots>
		<enabled>
		  false
		</enabled>
	  </snapshots>
	  <id>
		bintray-gcdd1993-maven
	  </id>
	  <name>
		bintray
	  </name>
	  <url>
		https://dl.bintray.com/gcdd1993/maven
	  </url>
	</repository>
</repositories>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<version>${springBootVersion}</version>
</dependency>
<dependency>
	<groupId>com.xuxueli</groupId>
	<artifactId>xxl-job-core</artifactId>
	<version>${xxlJobVersion}</version>
</dependency>
<dependency>
	<groupId>io.github.gcdd1993</groupId>
	<artifactId>xxl-job-spring-boot-starter</artifactId>
	<version>v0.1.0</version>
</dependency>

Gradle

repositories {
	maven {
		url  "https://dl.bintray.com/gcdd1993/maven"
	}
}

// 为了引入 RestTemplate
compile 'org.springframework.boot:spring-boot-starter-web'
compile "com.xuxueli:xxl-job-core:${xxlJobVersion}"
	
compile 'io.github.gcdd1993:xxl-job-spring-boot-starter:v0.1.0'

2. 将设置添加到application.yml文件中:

可供配置项如下:

xxl:
  job:
	enabled: true # 控制是否自动装配
    admin-addresses: "http://127.0.0.1:8080/xxl-job-admin"
    access-token:
    executor:
      app-name: "xxl-job-spring-boot-starter-test"
      ip:
      port: 8989
      log-path: "/data/xxl-job/log/job-handler"
      log-retention-days: 7

最简配置可以如下:

xxl:
  job:
    admin-addresses: "http://127.0.0.1:8080/xxl-job-admin"

详细的配置项说明可以在这里找到

3. 取消Admin端的登录校验

如果你不需要在代码层面进行任务的管理,请跳过。

由于使用RestTemplate模拟Http请求来操作Job,但是没有模拟登录获取Cookie(懒惰),所以需要把相关接口的登录校验去除。

修改com.xxl.job.admin.controller.JobInfoController,在相关接口上添加注解@PermissionLimit(limit = false)

@RequestMapping("/add")
@ResponseBody
+ @PermissionLimit(limit = false)
public ReturnT<String> add(XxlJobInfo jobInfo) {
	return xxlJobService.add(jobInfo);
}

@RequestMapping("/update")
@ResponseBody
+ @PermissionLimit(limit = false)
public ReturnT<String> update(XxlJobInfo jobInfo) {
	return xxlJobService.update(jobInfo);
}

@RequestMapping("/remove")
@ResponseBody
+ @PermissionLimit(limit = false)
public ReturnT<String> remove(int id) {
	return xxlJobService.remove(id);
}

@RequestMapping("/stop")
@ResponseBody
+ @PermissionLimit(limit = false)
public ReturnT<String> pause(int id) {
	return xxlJobService.stop(id);
}

@RequestMapping("/start")
@ResponseBody
+ @PermissionLimit(limit = false)
public ReturnT<String> start(int id) {
	return xxlJobService.start(id);
}

登录鉴权相关代码位于com.xxl.job.admin.controller.interceptor包下,可以说,包结构非常清晰了,一目了然。

做完这一切后打包Admin,搭建Admin端

测试

测试代码可以在这里找到 https://github.com/gcdd1993/xxl-job-spring-boot-starter/blob/master/src/test/

xxl-job-spring-boot-starter's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

xxl-job-spring-boot-starter's Issues

xxlJobClient 无法初始化,由于找不到RestTemplate Bean

Jul 25 20:04:17 node02 dbpe[26361]: Field xxlJobClient in com.maxtropy.dbpe.timer.service.ReportStatisticsService required a bean of type 'io.github.gcdd1993.client.XxlJobClient' that could not be found.
Jul 25 20:04:17 node02 dbpe[26361]: - Bean method 'xxlJobClient' in 'XxlJobAutoConfiguration' not loaded because @ConditionalOnBean (types: org.springframework.web.client.RestTemplate; SearchStrategy: all) did not find any beans

XxlJobSpringExecutor初始化两遍

XxlJobAutoConfiguration类中xxlJobExecutor方法上的@bean(initMethod = "start", destroyMethod = "destroy")注解,

可以把initMethod参数去掉,不然会导致XxlJobSpringExecutor初始化两遍,日志中会有bind错误出现。

不影响正常功能,就是有个报错信息。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.