Elastic-Job-Lite结合spring+eql实现定时任务

由于项目中定时任务采用了elasticjob+spring+eql,自己就学习分享下。

1、elasticjob

官网资料:  http://elasticjob.io/

2、没啥说明的,还是简单说说

先说说开发环境:win10、idea、tomcat8、jdk1.8

开发工具版本:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring.version>4.3.0.RELEASE</spring.version>
    <job.version>2.1.5</job.version>
    <zk.version>3.4.8</zk.version>
</properties>

jar包maven地址:http://www.mvnjar.com/org.apache.zookeeper/zookeeper/jar.html

后续需要什么包自己动手搜,选择自己版本添加到pom

3、动手实现

idea创建springmvc项目

后续步骤就不详细说明了,自己动手百度之。

创建好的项目

调整下项目结构,file->project structure->modules

引入jar:pom文件如下

 

<?xml version="1.0" encoding="UTF-8"?>

<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.study</groupId>
    <artifactId>elastic-job-project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>elastic-job-project Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>4.3.0.RELEASE</spring.version>
        <job.version>2.1.5</job.version>
        <zk.version>3.4.10</zk.version>
    </properties>

    <dependencies>
        <!-- spring核心包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- elasticJob相关包 -->
        <dependency>
            <groupId>com.dangdang</groupId>
            <artifactId>elastic-job-lite-core</artifactId>
            <version>${job.version}</version>
        </dependency>
        <dependency>
            <groupId>com.dangdang</groupId>
            <artifactId>elastic-job-lite-spring</artifactId>
            <version>${job.version}</version>
        </dependency>
        <!-- log相关包 -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.6.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.6</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>
        <!-- 底层数据库相关包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.github.bingoohuang/eql -->
        <dependency>
            <groupId>com.github.bingoohuang</groupId>
            <artifactId>eql</artifactId>
            <version>0.0.91</version>
        </dependency>

        <!-- redis包 -->
        <!-- alibb包 -->
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.51</version>
        </dependency>
        <!-- zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zk.version}</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>elastic-job-project</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

 

创建项目相关文件,项目结构如下:

首先配置spring的配置文件:spring.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-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!-- 开启spring的包扫描 -->
    <!-- 自动扫描包,实现支持注解的IOC -->
    <context:component-scan base-package="com.study.job"/>

    <!-- 加载elastic-job的任务配置信息 -->
    <import resource="classpath:./job-config/elastic-job-config.xml"/>

</beans>

接下来配置:elastic-job-config.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:reg="http://www.dangdang.com/schema/ddframe/reg"
       xmlns:job="http://www.dangdang.com/schema/ddframe/job"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.dangdang.com/schema/ddframe/reg
                        http://www.dangdang.com/schema/ddframe/reg/reg.xsd
                        http://www.dangdang.com/schema/ddframe/job
                        http://www.dangdang.com/schema/ddframe/job/job.xsd">

    <!--配置作业注册中心 -->
    <reg:zookeeper id="regCenter"
                   server-lists="127.0.0.1:2181"
                   namespace="study-elastic-job"
                   base-sleep-time-milliseconds="1000"
                   max-sleep-time-milliseconds="3000"
                   max-retries="3"
                   digest="digest"/>
    
</beans>

说明:elasticjob依赖于zookeeper的,要注册信息到zookeeper,各个参数的意义可以参考:http://elasticjob.io/docs/elastic-job-lite/02-guide/config-manual/

其中的  2. Spring命名空间配置

先实现demo:

简单任务:

package com.study.job.demo.job;

import com.alibaba.fastjson.JSON;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 简单任务,实现接口 SimpleJob
 */
@Component
public class DemoSimpleJob implements SimpleJob {
    private static final Logger log = LoggerFactory.getLogger(DemoSimpleJob.class);

    @Override
    public void execute(ShardingContext shardingContext) {
        log.info("进入当前简单任务");
        log.info("简单任务,ShardingContext信息:" + JSON.toJSONString(shardingContext));
    }
}

流式任务:

package com.study.job.demo.job;

import com.alibaba.fastjson.JSON;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.dataflow.DataflowJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 流式处理任务,实现接口 DataflowJob<T>
 */
@Component
public class DemoDataFlowJob implements DataflowJob<String> {
    private static final Logger log = LoggerFactory.getLogger(DemoDataFlowJob.class);

    @Override
    public List<String> fetchData(ShardingContext shardingContext) {
        log.info("进入当前--流式--任务");
        log.info("流式--任务,ShardingContext信息:" + JSON.toJSONString(shardingContext));

        /**
         *  当此方法返回为空或者size=0的时候,不会执行下边的processData
         */
        return null;
    }

    @Override
    public void processData(ShardingContext shardingContext, List<String> list) {

    }
}

 

配置任务到:elastic-job-config.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:reg="http://www.dangdang.com/schema/ddframe/reg"
       xmlns:job="http://www.dangdang.com/schema/ddframe/job"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.dangdang.com/schema/ddframe/reg
                        http://www.dangdang.com/schema/ddframe/reg/reg.xsd
                        http://www.dangdang.com/schema/ddframe/job
                        http://www.dangdang.com/schema/ddframe/job/job.xsd">

    <!--配置作业注册中心 -->
    <reg:zookeeper id="study-elastic-job-regCenter"
                   server-lists="127.0.0.1:2181"
                   namespace="study-elastic-job"
                   base-sleep-time-milliseconds="1000"
                   max-sleep-time-milliseconds="3000"
                   max-retries="3"
                   digest="digest"/>
    <!-- 配置简单作业   -->
    <job:simple id="demoSimpleJob"
                class="com.study.job.demo.job.DemoSimpleJob"
                registry-center-ref="study-elastic-job-regCenter"
                cron="0 0/1 * * * ?"
                sharding-total-count="1"
                overwrite="true"
                description="简单任务配置"/>
    <!-- 配置流式作业   -->
    <job:dataflow id="demoDataFlowJob"
                  class="com.study.job.demo.job.DemoDataFlowJob"
                  registry-center-ref="study-elastic-job-regCenter"
                  cron="0 0/1 * * * ?"
                  sharding-total-count="3"
                  sharding-item-parameters="0=A,1=B,2=C"
                  description="流式任务配置"/>
</beans>

说明:各个类型任务的参数说明就不细说,

各个参数的意义可以参考:http://elasticjob.io/docs/elastic-job-lite/02-guide/config-manual/

其中的  2. Spring命名空间配置

zookeeper的搭建,这就不过多的说明zookeeper的搭建了,本次采用的单机zk,需要注意的是,版本的控制需要注意,建议跟随官方文档。

启动项目:

以下显示成功启动,最后会有一些注册job的信息打印出

至此项目基本启动到定时任务的实现就完成了,结果如下:

zookeeper中任务节点信息如下:

 

 

接下来就说明eql与spring的结合

先说说eql轻量级框架的,个人认为,有错误请大家评论指出

eql是零侵入的一个框架,与spring的集合不像mybatis、hibernate那样一样需要过多的配置文件,只需要在指定目录建立指定的文件夹即可项目结构如下所示:

 

首先创建bean:

package com.study.job.demo.bean;

/**
 * 展示插入数据库 demo bean
 */
public class UserBean {
    private String userName;
    private int userAge;
    private String userSex;

    public UserBean(String userName, int userAge, String userSex) {
        this.userName = userName;
        this.userAge = userAge;
        this.userSex = userSex;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getUserAge() {
        return userAge;
    }

    public void setUserAge(int userAge) {
        this.userAge = userAge;
    }

    public String getUserSex() {
        return userSex;
    }

    public void setUserSex(String userSex) {
        this.userSex = userSex;
    }
}

数据库连接文件,eql-DEFAULT.properties

创建定时任务:

package com.study.job.demo.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.study.job.demo.bean.UserBean;
import org.n3r.eql.Eql;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 展示 eql的使用的 demo
 */
@Component
public class UserJob implements SimpleJob {
    private static final Logger log = LoggerFactory.getLogger(UserJob.class);

    @Override
    public void execute(ShardingContext shardingContext) {
        log.info("测试sql的数据库连接:");
        /**
         *  仅做展示
         */
        for (int i = 0; i < 10; i++) {
            UserBean userBean = new UserBean("demo" + i, i, i % 2 == 0 ? "man" : "woman");
            new Eql().insert("insertUser").params(userBean).execute();
        }

        /**
         * eql 详细用法参考
         *
         *  https://github.com/bingoohuang/eql
         */

    }
}

对应的eql文件 UserJob.eql

--[insertUser]
insert into user(user_name,user_age,user_sex)
values(#userName#,#userAge#,#userSex#);

配置到定时任务,elastic-job-config.xml

<job:simple id="userJob"
            class="com.study.job.demo.job.UserJob"
            registry-center-ref="study-job-regCenter"
            cron="0/10 * * * * ?"
            sharding-total-count="1"
            overwrite="true"
            description="测试eql数据库的链接,新增数据"/>

启动项目:

 

查看数据库信息:

 

到此,基本的使用流程已经完成,接下来给大家展示下elastic-job的控制台:

 

由于是本地,就win启动了

默认的端口就是8899,直接访问  http://localhost:8899/

账户密码就是配置文件里边配置的,

 

添加我们启动的定时任务

 

链接的信息就是项目中配置zookeeper的注册信息

注册好后就连接,切换到作业操作就可以操作自己的任务

 

到此,一个定时任务的全部完成,由于网上资料较少,自己写了,有什么问题欢迎指出,

知识无界限,一起学习一起进步。

 

源码地址

链接: https://pan.baidu.com/s/1OJHBlHB2GtTncAIuN0rveg 提取码: fydr

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值