【niubi-job——一个分布式的任务调度框架】----如何开发一个niubi-job的定时任务

引言

  

  上篇文章LZ主要讲解了niubi-job如何安装,如果看过上一篇文章的话,大家应该知道,niubi-job执行的任务是需要用户自己上传jar包的。

  那么问题来了,这个jar包如何产生?有没有要求?

  本文就是来解决这个问题的,虽然LZ的github上面有例子,但是终究还是LZ自己解释一下会让大家更清晰一些。废话不多说,接下来咱们就来看看如何开发一个定时任务,并且可以运行在niubi-job的容器中。

  

概述

  

  首先,LZ在设计的时候,主要将任务分成两大类:一类是运行在spring容器当中的任务,一类则是不运行在spring容器当中的任务。

  什么叫运行在spring容器当中?

  很简单,就是你的任务类引用了spring提供的bean,比如XXXService,或者是XXXXMapper,亦或是XXXXDao,又或者是其它。那么相反的,如果你的类可以独立运行,而不需要spring容器的运行环境,则被LZ统一看作是普通的任务。

  PS:本文所有代码都取自niubi-job-samples,在阅读本文的时候,大家可以参考一下。

  

非spring环境的任务

  

  第一步:使用maven建立一个普通的项目,你的pom.xml如下。

<?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">
    <parent>
        <artifactId>niubi-job-parent</artifactId>
        <groupId>com.zuoxiaolong</groupId>
        <version>0.9.6</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>niubi-job-sample-common</artifactId>
    <name>${project.groupId}:${project.artifactId}</name>

</project>

  第二步:创建你的任务java类。

  以下这就是一个典型的非spring环境的niubi-job任务,取自niubi-job-sample-common。(niubi-job依靠@Schedule识别任务,因此如果你想让一个方法在niubi-job当中可以发布,则必须给该方法加上@Schedule注解。)

/*
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zuoxiaolong.niubi.job.sample.common.job;

import com.zuoxiaolong.niubi.job.core.helper.LoggerHelper;
import com.zuoxiaolong.niubi.job.scanner.annotation.Schedule;

/**
 * @author Xiaolong Zuo
 * @since 16/1/18 22:25
 */
public class Job1 {

    @Schedule(cron = "0/15 * * * * ?")
    public void job1Test() {
        LoggerHelper.info("[job1] is running.......");
    }

}

  第三步:写一个测试类,来测试你的任务是否能正常运行。

  运行以下这个简单的类,就可以在本地测试你的定时任务了。(本地测试时,cron和misfirePolicy会取自你Schedule注解的属性值。在任务在提交到niubi-job集群以后,会取自你在console控制台填写的值,Schedule注解的属性值将会被忽略。)

/*
 * Copyright 2002-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.zuoxiaolong.niubi.job.sample.common;

import com.zuoxiaolong.niubi.job.scheduler.node.Node;
import com.zuoxiaolong.niubi.job.scheduler.node.SimpleLocalJobNode;

/**
 * @author Xiaolong Zuo
 * @since 1/22/2016 14:13
 */
public class Test {

    public static void main(String[] args) {
//com.zuoxiaolong为任务所在的包,这个参数指定了niubi-job需要扫描哪些包找到任务。 Node node
= new SimpleLocalJobNode("com.zuoxiaolong"); node.join(); } }

  第四步:打包你的任务,上传到niubi-job的console控制台即可。

  使用以下命令即可将你的任务打包成符合niubi-job规范的jar包。(打包后,target文件下会有一个niubi-job-sample-common.jar和一个original-niubi-job-sample-common.jar,使用niubi-job-sample-common.jar即可)

mvn clean package

  

spring环境的任务

  

  第一步:使用maven建立一个普通的项目,你的pom.xml如下。

<?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">
    <parent>
        <artifactId>niubi-job-parent</artifactId>
        <groupId>com.zuoxiaolong</groupId>
        <version>0.9.6</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>niubi-job-sample-spring</artifactId>
    <name>${project.groupId}:${project.artifactId}</name>

    <dependencies>
        <!-- 这是spring的jar包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
    </dependencies>

</project>  

  第二步:模拟一个spring容器中的bean。

  以下这个类是一个非常普通的spring的bean。在实际开发中,它可能是任何一个在spring容器中初始化出来的bean,代码取自niubi-job-sample-spring。

/*
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zuoxiaolong.niubi.job.sample.spring.bean;

import com.zuoxiaolong.niubi.job.core.helper.LoggerHelper;
import org.springframework.stereotype.Service;

/**
 * @author Xiaolong Zuo
 * @since 16/1/18 22:33
 */
@Service
public class OneService {

    public void someServiceMethod1() {
        LoggerHelper.info("[job1] invoke [serviceMethod1] successfully......");
    }

    public void someServiceMethod2() {
        LoggerHelper.info("[job2] invoke [serviceMethod2] successfully......");
    }

}

  第三步:创建你的任务java类。

  以下就是一个需要在spring容器中运行的任务,因为它引用了上面的spring容器中的bean。(实际当中这个bean最有可能是一个XXXXService)

 

/*
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zuoxiaolong.niubi.job.sample.spring.job;

import com.zuoxiaolong.niubi.job.sample.spring.bean.OneService;
import com.zuoxiaolong.niubi.job.scanner.annotation.Schedule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Xiaolong Zuo
 * @since 16/1/16 15:30
 */
@Component
public class Job1 {

    @Autowired
    private OneService oneService;

    @Schedule(cron = "0/15 * * * * ?")
    public void test() {
        oneService.someServiceMethod1();
    }

}

  第四步:在classpath下创建一个applicationContext.xml。

  以下就是一个applicationContext.xml的简单示例。(如果你原本的spring配置文件不叫applicationContext.xml,而你又不想改原本spring配置的名字,那么可以在classpath建立一个applicationContext.xml文件,并且将你原本的spring配置文件用import标签导入。)

<?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"
       xmlns:job="http://www.zuoxiaolong.com/schema/niubi-job"
       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
       http://www.zuoxiaolong.com/schema/niubi-job
       http://www.zuoxiaolong.com/schema/niubi-job/niubi-job-1.0.xsd">

    <!-- 你自己的一些spring配置 -->
    <context:annotation-config/>

    <context:component-scan base-package="com.zuoxiaolong.niubi.job.sample.spring"/>

    <!-- 以下这一行用于开启niubi-job的驱动,可以用于本地测试任务 -->
    <!-- packagesToScan属性指定了需要扫描那些包寻找任务 -->
    <job:job-driven packagesToScan="com.zuoxiaolong.niubi.job.sample.spring"/>

</beans>

  第五步:写一个测试类,来测试你的任务是否能正常运行。

  运行以下这个类,就可以测试你的任务。

/*
 * Copyright 2002-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.zuoxiaolong.niubi.job.sample.spring;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * use to test jobs.
 *
 * @author Xiaolong Zuo
 * @since 1/22/2016 14:19
 */
public class Test {

    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("applicationContext.xml");
    }

}

  第六步:打包你的任务,上传到niubi-job的console控制台即可。

  使用以下命令即可将你的任务打包成符合niubi-job规范的jar包。(打包后,target文件下会有一个niubi-job-sample-spring.jar和一个original-niubi-job-sample-spring.jar,使用niubi-job-sample-spring.jar即可)

mvn clean package

  

总结

  

  接下来,总结一下niubi-job对上传的任务jar包的要求。

  1、请使用maven构建项目,并继承com.zuoxiaolong:niubi-job-parent:0.9.6。

  2、如果需要spring的运行环境,请确保您的classpath下有一个包含了spring配置的applicationContext.xml文件。    

  

编写任务时如何打印日志

  

  当你按照以上的方式去编写你的任务时,你可以找到一个叫做LoggerHelper的类,它里面包含了一些打印日志的方法。

  强烈建议,如果要在任务中打印日志的话,请使用该类。

  使用该类打印的日志,都将出现在niubi-job-cluster的logs文件夹的日志文件里,可以非常方便的查看,也便于后期与elasticsearch集成。

  有关和elasticsearch集成的内容,后期LZ会补充上来。集成以后,你可以非常方便的查看任务运行日志。如果你的公司本身就有一套基于elasticsearch的日志查看系统,那就更加完美了。

  

结束语

  

  niubi-job是LZ倾心打造的一个项目,LZ会出一系列文章来介绍它,包括如何使用以及它的一些设计思想和原理,有兴趣的同学可以关注一下。

  如果你的项目刚好缺一个定时任务的调度框架,那么niubi-job应该是你不二的选择!

  当然,如果你有兴趣参与进来,也可以在Github上面给LZ提交PR,LZ一定尽职尽责的进行review。

posted @ 2016-01-25 03:34  左潇龙  阅读(4415)  评论(2编辑  收藏  举报