栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot+xxl-job定时器

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot+xxl-job定时器

导入核心公共包



    com.xuxueli
    xxl-job-core
    2.3.0

先配置服务端(调度中心)

yml配置

### web
server.port=8080
server.servlet.context-path=/xxl-job-admin


### actuator
management.server.servlet.context-path=/actuator
management.health.mail.enabled=false


### resources
spring.mvc.servlet.load-on-startup=0
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/static/


### freemarker
spring.freemarker.templateLoaderPath=classpath:/templates/
spring.freemarker.suffix=.ftl
spring.freemarker.charset=UTF-8
spring.freemarker.request-context-attribute=request
spring.freemarker.settings.number_format=0.##########


### mybatis
mybatis.mapper-locations=classpath:/mybatis-mapper/*Mapper.xml
#mybatis.type-aliases-package=com.xxl.job.admin.core.model


### xxl-job, datasource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver


### datasource-pool
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.maximum-pool-size=30
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.pool-name=HikariCP
spring.datasource.hikari.max-lifetime=900000
spring.datasource.hikari.connection-timeout=10000
spring.datasource.hikari.connection-test-query=SELECt 1
spring.datasource.hikari.validation-timeout=1000


### xxl-job, email
spring.mail.host=smtp.qq.com
spring.mail.port=25
spring.mail.username=xxx@qq.com
spring.mail.from=xxx@qq.com
spring.mail.password=xxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory


### 客户端连接服务端的token
xxl.job.accessToken=


### xxl-job, i18n (default is zh_CN, and you can choose "zh_CN", "zh_TC" and "en")
xxl.job.i18n=zh_CN


## xxl-job, triggerpool max size
xxl.job.triggerpool.fast.max=200
xxl.job.triggerpool.slow.max=100


### xxl-job, log retention days
xxl.job.logretentiondays=30

客户端配置类

@Configuration
@ConfigurationProperties(prefix = "xxljob")
public class XxlJobConfig {
    private static Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);


    private String adminAddresses;
    private String address;
    private String appname;
    private String ip;
    private int port;
    private String accessToken;
    private String logPath;
    private int logRetentionDays;
    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        logger.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
        xxlJobSpringExecutor.setAppname(appname);
        xxlJobSpringExecutor.setIp(ip);
        xxlJobSpringExecutor.setPort(port);
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setLogPath(logPath);
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
        return xxlJobSpringExecutor;
    }
    set..
    get..
}

单机客户端yml配置

xxljob:
  adminAddresses: http://127.0.0.1:8080/xxl-job-admin #多个服务端可以逗号分割
  appname: mybatis-01
  address: 域名 
  ip: 
  port: 9998
  access-token:
  log-path: /data/xxl-job/jobhandler
  log-retention-days: 30

(可选)集群客户端yml配置

拷贝两份单机客户端->修改tomcat端口->修改job客户端port其他不变

创建定时任务

@Component
public class HelloJob {
    private static Logger logger = LoggerFactory.getLogger(HelloJob.class);

    @XxlJob("helloJob")
    public String hello(){
        System.out.println("当前端口"+"time:"+new Date());
        return "success";
    }
}

使用

1.登录web后台
2.执行器管理->新增appName=mybatis-01->名称=随便->自动注册
3.任务管理器新增 执行器=mybatis-01(项目名字) JobHandler=helloJob(方法注解名称) 运行模式=bean

注意:调度中心的执行器地址要加http协议 如http://127.0.0.1:8081 而不是127.0.0.1:8081

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/877152.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号