xxl-job引入实际项目中

源码下载地址:https://gitee.com/xuxueli0323/xxl-job

1、下载好之后解压本地目录,到入项目到idea中

xxl-job引入实际项目中_第1张图片

2、执行sql文件:

/xxl-job/doc/db/tables_xxl_job.sql

xxl-job引入实际项目中_第2张图片

 3、修改xxl-job-admin的配置文件:

/xxl-job-admin/src/main/resources/xxl-job-admin.properties,在里面修改密码、数据库连接账号密码

xxl-job引入实际项目中_第3张图片

4、访问xxl-job-admin首页,登录

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

admin/123456

5、引入实际项目

打包xxl-job-core到本地

mvn install:install-file -Dfile=D:\IdeaProjects\push\xxl-job\xxl-job-core\target\xxl-job-core-2.2.1-SNAPSHOT.jar -DgroupId=com.xuxueli -DartifactId=xxl-job-core -Dversion=2.1.1-SNAPSHOT -Dpackaging=jar

新建项目my-shop-member-job


        
            com.xuxueli
            xxl-job-core
            2.2.1-SNAPSHOT
        
        
            mysql
            mysql-connector-java
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.1
        
    

XxlJobConfig

package com.mayikt.job.config;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
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.Configuration;

/**
 * xxl-job config
 *
 * @author xuxueli 2017-04-28
 */
@Configuration
public class XxlJobConfig {
    private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;

    @Value("${xxl.job.accessToken}")
    private String accessToken;

    @Value("${xxl.job.executor.appname}")
    private String appname;

    @Value("${xxl.job.executor.address}")
    private String address;

    @Value("${xxl.job.executor.ip}")
    private String ip;

    @Value("${xxl.job.executor.port}")
    private int port;

    @Value("${xxl.job.executor.logpath}")
    private String logPath;

    @Value("${xxl.job.executor.logretentiondays}")
    private int logRetentionDays;


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

        return xxlJobSpringExecutor;
    }

    /**
     * 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP;
     *
     *      1、引入依赖:
     *          
     *             org.springframework.cloud
     *             spring-cloud-commons
     *             ${version}
     *         
     *
     *      2、配置文件,或者容器启动变量
     *          spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
     *
     *      3、获取IP
     *          String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
     */


}
WechatActivityJob,这里可以新建多个任务
package com.mayikt.job.jobhandler;

import com.mayikt.job.mapper.UserMapper;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.util.ShardingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: liuwq
 * @date: 2020/4/20 0020 下午 2:26
 * @version: V1.0
 */
@Component
@Slf4j
public class WechatActivityJob {

    @Autowired
    private UserMapper userMapper;
    @Value("${mayikt.member.job.WechatActivitiePageSize}")
    private Integer pageSize;

    @XxlJob("wechatActivityJobHandler")
    public ReturnT wechatActivityJobHandler(String param) {
        ShardingUtil.ShardingVO shardingVo = ShardingUtil.getShardingVo();
        int index = shardingVo.getIndex();
        log.info(">>>定时任务开始出发<< wechatActivityJobHandler222(String param) {
        ShardingUtil.ShardingVO shardingVo = ShardingUtil.getShardingVo();
        int index = shardingVo.getIndex();
        log.info(">>>wechatActivityJobHandler222定时任务开始出发<<

application.properties

# web port
server.port=8082
# no web
#spring.main.web-environment=false

# log config
logging.config=classpath:logback.xml


### 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

### xxl-job, access token
xxl.job.accessToken=

### xxl-job executor appname
xxl.job.executor.appname=mayikt-member-executor-job
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
xxl.job.executor.address=
### xxl-job executor server-info
xxl.job.executor.ip=
xxl.job.executor.port=9998
### xxl-job executor log-path
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
### xxl-job executor log-retention-days
xxl.job.executor.logretentiondays=30

bootstrap.yml

spring:
  cloud:
    nacos:
      discovery:
        ##服务的注册
        server-addr: 127.0.0.1:8848
        ###  nacos 配置中心
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
  redis:
    host: 127.0.0.1
    port: 6369
  #    password: 123456
  application:
    name: mayikt-member-job


mayikt:
  member:
    job:
      WechatActivitiePageSize: 2

6、配置admin-job

添加执行器

xxl-job引入实际项目中_第4张图片

添加任务 

xxl-job引入实际项目中_第5张图片

注意:必须要先运行admin-job

xxl-job引入实际项目中_第6张图片

你可能感兴趣的:(xxl-job,Java)