• XxlJobConfig


    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;
    
    import com.cloudwise.job.core.executor.impl.XxlJobSpringExecutor;
    import org.springframework.stereotype.Component;
    
    /**
     * 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.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
        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;
        }
    
        /**
         * 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP;
         *
         *      1、引入依赖:
         *          <dependency>
         *             <groupId>org.springframework.cloud</groupId>
         *             <artifactId>spring-cloud-commons</artifactId>
         *             <version>${version}</version>
         *         </dependency>
         *
         *      2、配置文件,或者容器启动变量
         *          spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
         *
         *      3、获取IP
         *          String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
         */
    
    
    }
    # web port
    server.port=8081
    # 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=xxl-job-executor-sample
    ### 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=9999
    ### 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
            <!-- xxl-job-core -->
            <dependency>
                <groupId>com.xuxueli</groupId>
                <artifactId>xxl-job-core</artifactId>
                <version>${project.parent.version}</version>
            </dependency>

    2.2.0

    package com.xxl.job.executor.service.jobhandler;
    
    import com.xxl.job.core.biz.model.ReturnT;
    import com.xxl.job.core.handler.annotation.XxlJob;
    import com.xxl.job.core.log.XxlJobLogger;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    
    import java.util.concurrent.TimeUnit;
    
    /**
     * XxlJob开发示例(Bean模式)
     *
     * 开发步骤:
     * 1、在Spring Bean实例中,开发Job方法,方式格式要求为 "public ReturnT<String> execute(String param)"
     * 2、为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")",注解value值对应的是调度中心新建任务的JobHandler属性的值。
     * 3、执行日志:需要通过 "XxlJobLogger.log" 打印执行日志;
     *
     * @author xuxueli 2019-12-11 21:52:51
     */
    @Component
    public class SampleXxlJob {
        private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class);
    
    
        /**
         * 1、简单任务示例(Bean模式)
         */
        @XxlJob("demoJobHandler")
        public ReturnT<String> demoJobHandler(String param) throws Exception {
            XxlJobLogger.log("XXL-JOB, Hello World.");
    
            for (int i = 0; i < 5; i++) {
                XxlJobLogger.log("beat at:" + i);
                TimeUnit.SECONDS.sleep(2);
            }
            return ReturnT.SUCCESS;
        }
    }

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

    src/main/resources/applicationcontext-xxl-job.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context.xsd">
    
        <bean id="propertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
            <property name="fileEncoding" value="utf-8" />
            <property name="locations">
                <list>
                    <value>classpath*:xxl-job-executor.properties</value>
                </list>
            </property>
        </bean>
    
        <!-- ********************************* 基础配置 ********************************* -->
    
        <!-- 配置01、JobHandler 扫描路径 -->
        <context:component-scan base-package="com.xxl.job.executor.service.jobhandler" />
    
        <!-- 配置02、执行器 -->
        <bean id="xxlJobSpringExecutor" class="com.xxl.job.core.executor.impl.XxlJobSpringExecutor" >
            <!-- 执行器注册中心地址[选填],为空则关闭自动注册 -->
            <property name="adminAddresses" value="${xxl.job.admin.addresses}" />
            <!-- 访问令牌[选填],非空则进行匹配校验 -->
            <property name="accessToken" value="${xxl.job.accessToken}" />
            <!-- 执行器AppName[选填],为空则关闭自动注册 -->
            <property name="appname" value="${xxl.job.executor.appname}" />
            <!-- 注册地址[选填],优先使用该配置作为注册地址,为空时使用内嵌服务 ”IP:PORT“ 作为注册地址 -->
            <property name="address" value="${xxl.job.executor.address}" />
            <!-- 执行器IP[选填],为空则自动获取 -->
            <property name="ip" value="${xxl.job.executor.ip}" />
            <!-- 执行器端口号[选填],小于等于0则自动获取 -->
            <property name="port" value="${xxl.job.executor.port}" />
            <!-- 执行器日志路径[选填],为空则使用默认路径 -->
            <property name="logPath" value="${xxl.job.executor.logpath}" />
            <!-- 日志保存天数[选填],值大于3时生效 -->
            <property name="logRetentionDays" value="${xxl.job.executor.logretentiondays}" />
        </bean>
    
    </beans>
  • 相关阅读:
    Unix环境高级编程—进程关系
    如何学习CCIE
    Unix环境高级编程—进程控制(三)
    Unix环境高级编程—进程控制(二)
    _THROW 何解?
    Unix高级环境编程—进程控制(一)
    ifndef/define/endif 和 #ifdef 、#if 作用和用法
    内存MCE错误导致暴力扩充messages日志 以及chattr记录
    Intellij IDEA的安装和激活
    Xshell和Xftp间隔一段时间不操作就自动断开连接?
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/15232002.html
Copyright © 2020-2023  润新知