参考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 修改就可以了

Spring Boot2.0之 整合XXL-Job的更多相关文章

  1. Spring Boot2.0之整合事物管理

    首先Spring 事务分类 1.声明事务  原理:基于编程事务的 2.编程事务  指定范围 扫包去解决 3.事务原理:AOP技术   通过环绕通知进行了拦截 使用Spring 事务注意事项: 不要tr ...

  2. Spring Boot2.0之整合JSP

    首先不建议整合JSP哈,spring boot 对jsp的支持力度不大.  内置tomcat不支持jsp. 注意:在创建项目时候一定是war类型的,而不是跟之前那个freemarker那种jar类型. ...

  3. Spring Boot2.0之整合Mybatis

    我在写这个教程时候,踩了个坑,一下子折腾到了凌晨两点半. 坑: Spring Boot对于Mysql8.1的驱动支持不好啊 我本地安装的是Mysql8.1版本,在开发时候.pom提示不需要输入驱动版本 ...

  4. Spring Boot2.0之整合多数据源

    一般公司分两个数据库: 一个放共同配置文件, 一个数据库垂直业务数据库 垂直拆分和水平拆分: 垂直是根据业务划分具体数据库 在一个项目中有多个数据源(不同库jdbc) 无限个的哈~ 根据包名 或者 注 ...

  5. Spring Boot2.0之 整合Zookeeper集群

    普通的连接: pom: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w ...

  6. Spring Boot2.0之 整合Redis集群

    项目目录结构: pom: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// ...

  7. Spring Boot2.0之 整合Redis事务

    Redis事物 Redis 事务可以一次执行多个命令, 并且带有以下两个重要的保证: 事务是一个单独的隔离操作:事务中的所有命令都会序列化.按顺序地执行.事务在执行的过程中,不会被其他客户端发送来的命 ...

  8. Spring Boot2.0之整合Redis

    需要的maven依赖 jar包,是对Jedis的封装 maven依赖: <project xmlns="http://maven.apache.org/POM/4.0.0" ...

  9. Spring Boot2.0之整合log4j

    传统方式打印日志比较复杂, 每次打印需要定义全局变量 private static final Logger logger = LoggerFactory.getLogger(SjpControlle ...

随机推荐

  1. 一套一般般的前端框架 layui

    http://www.layui.com/doc/

  2. Android 自定义权限 (<permission> <uses-permission>)

    在android系统的安全模型中,应用程序在默认的情况下不可以执行任何对其他应用程序,系统或者用户带来负面影响的操作.如果应用需要执行某些操作,就需要声明使用这个操作对应的权限. (在manifest ...

  3. python time与datetime.date/datetime模块

    https://docs.python.org/3/library/datetime.html 1.用于日期比较大小的方法 方法名 方法说明 用法 __eq__(…) 等于(x==y) x.__eq_ ...

  4. ubuntu虚拟化平台libvrit-bin

    http://download.cirros-cloud.net/0.3.5/ 下载 cirros-0.3.5-x86_64-disk.img 因为 KVM(准确说是 Libvirt)默认不接受远程管 ...

  5. Assertion (software development) -- 断言

    Wiki: In computer programming, an assertion is a predicate (a true–false statement) placed in a prog ...

  6. [Spring MVC]学习笔记--基础Servlet

    Servlet是一个用Java编写的应用程序,在服务器上运行,处理请求的信息并将其发送到客户端. Servlet的客户端提出请求并获得该请求的响应. 对于所有的客户端请求,只需要创建Servlet的实 ...

  7. js引入方式的弹框方法2

    html代码: <!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv=& ...

  8. Neighbor Discovery Protocol Address Resolution Protocol

    https://en.wikipedia.org/wiki/Address_Resolution_Protocol The Address Resolution Protocol (ARP) is a ...

  9. LINUX的LAMP环境搭配

    在ubuntu linux下以编译方式安装LAMP(apache mysql php)环境 最近转向到了使用ubuntu做 桌面,安装好系统以来一直都没配置lamp开发环境.由于很久以来没有自己编译安 ...

  10. MySQL 5.6 死锁演示 及 日志分析

    1.  表结构 CREATE TABLE dead_update ( a ) ', PRIMARY KEY (a) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ; ), ...