【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。
【niubi-job——一个分布式的任务调度框架】----如何开发一个niubi-job的定时任务的更多相关文章
- 【niubi-job——一个分布式的任务调度框架】----niubi-job这下更牛逼了!
niubi-job迎来第一次重大优化 niubi-job是一款专门针对定时任务所设计的分布式任务调度框架,它可以进行动态发布任务,并且有超高的可用性保证. 有多少人半夜被叫起来查BUG,结果差到最后发 ...
- 【niubi-job——一个分布式的任务调度框架】----安装教程
niubi-job是什么 niubi-job是LZ耗时三个星期,费尽心血打造的一个具备高可靠性以及水平扩展能力的分布式任务调度框架,采用quartz作为底层的任务调度管理器,zookeeper做集群的 ...
- 【niubi-job——一个分布式的任务调度框架】----框架设计原理以及实现
引言 niubi-job的框架设计是非常简单实用的一套设计,去掉了很多其它调度框架中,锦上添花但并非必须的组件,例如MQ消息通讯组件(kafka等).它的框架设计核心思想是,让每一个jar包可以相对之 ...
- niubi-job:一个分布式的任务调度框架设计原理以及实现
niubi-job的框架设计是非常简单实用的一套设计,去掉了很多其它调度框架中,锦上添花但并非必须的组件,例如MQ消息通讯组件(kafka等).它的框架设计核心思想是,让每一个jar包可以相对之间独立 ...
- 【niubi-job——一个分布式的任务调度框架】----FAQ文档
引言 本文为niubi-job的FAQ文档,该文档会无限更新.如果您在这里没有找到您想要的答案,请把问题提交到这里. FAQ 1.为什么我的所有任务总是运行在同一个节点上,而没有平均分配到所有节点上? ...
- 企业级任务调度框架Quartz(3) 一个简单的Quartz 例子
1. 一个简单的Quartz 工程 本示例应用比起众所周知的 System.out.println("Hello world from Quartz") 来还是要有趣些.当 ...
- 应用Struts2框架,开发一个加法器,采用两个页面,一个页面输入数据,另一个界面输出结果。
软件152谭智馗 一.新建maven项目 1.选择菜单file—new—maven project,勾选“Create a &simple project (skip archetype se ...
- 手写web框架之开发一个类加载器
ackage io.renren.common; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUti ...
- Cola:一个分布式爬虫框架 - 系统架构 - Python4cn(news, jobs)
Cola:一个分布式爬虫框架 - 系统架构 - Python4cn(news, jobs) Cola:一个分布式爬虫框架 发布时间:2013-06-17 14:58:27, 关注:+2034, 赞美: ...
随机推荐
- 读书笔记——OpenGL超级宝典
对于某些函数的理解 glClear和glClearColor glClearColor指定glClear清除特定缓冲区时使用的值. glFlush 让所有已发送的命令尽快的由实际的绘制引擎执行. gl ...
- php 修改上传文件大小 (max_execution_time post_max_size)
有些朋友要通过自己的网站后台,包括论坛,来上传一些文件,php一般为2m,或8m(以下我们按默认为2m),接下来就是来讲怎么修改上传文件大小的. 1.首先修改执行上传文件限制 一般的文件上传,除非文件 ...
- Tomcat 服务器版本的区别以及下载与安装
Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun 和其他一些公司及个人共同开发而成.由于有了 ...
- How to use python remove the '^M' when copy words from Windows to Linux
今天帮同事用Python写了一个小工具,实现了在linux下批量文件名和去掉windows 文件到linux过程中产生^M的脚本,代码如下: !/opt/exptools/bin/python imp ...
- [转载]ExtJs4 笔记(11) Ext.ListView、Ext.view.View 数据视图
本篇介绍两个用来展示数据的容器控件,分别是Ext.ListView和Ext.view.View.Ext.ListView就是大名鼎鼎的 Ext GridPanel的前身,不过现在的Ext4已经将它整合 ...
- POJ 3304 Segments --枚举,几何
题意: 给n条线段,问有没有一条直线,是每条线段到这条直线上的投影有一个公共点. 解法: 有公共点说明有一条这条直线的垂线过所有线段,要找一条直线过所有线段,等价于从所有线段中任选两端点形成的直线存在 ...
- HDU 4419 Colourful Rectangle --离散化+线段树扫描线
题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何 ...
- 第8课 goto 和 void 分析
1. 遭人遗弃的goto (1)高手潜规则:禁用goto (2)项目经验:程序质量与goto出现的次数成反比 (3)最后的判决:将goto打入冷宫(1)循环语句的基本工作方式 [实例分析]goto副作 ...
- centos下安装xampp,Zend Guard,memcached
这里说的生产环境是php5.4x,要高版本的其实也一样 第一步:安装xampp xampp它是跨平台的,且自带很多拓展,安装之后会为我们省去很多事,使用起来很方便. i>http://sourc ...
- css3新属性的总结
今天继续总结css3的一些css3新样式,先列一个简单的提纲,重要的还是圆角.阴影.渐变.文字缩略,最最重要的是过度transition,变换transform和animation圆角阴影渐变 圆形渐 ...