【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, 赞美: ...
随机推荐
- redis客户端--jedis
一.jedis jedis 是 redis推荐的java客户端.通过Jedis我们可以很方便地使用java代码的方式,对redis进行操作.jedis使用起来比较简单,它的操作方法与redis命令相类 ...
- Android开发之 Android应用程序目录结构解析
建立的HelloWorld的应用项目,其代码是由ADT插件自动生成的,形成Android项目特有的结构框架. 接下来让我带领大家解析一个Android程序的各个组成部分,这次我们拿一个Hello,Wo ...
- Vmware中Ubuntu的各种问题
Question1: Ubuntu正在运行的时候手残强行关闭了Vmware软件,重启软件发现如下图所示的状态: ''' 完全黑屏怎么解决,提示信息是recovering journal就是说正在恢复日 ...
- 探索 OpenStack 之(9):深入块存储服务Cinder (功能篇)
继研究了Neutron之后,继续Nova的外围研究之旅.本站是研究块存储服务Cinder. 0.验证环境 环境包括: 1.一个controller节点,运行nova-api, nova-schedul ...
- 什么办法可以替代distinct
今天在论坛上看到一个面试题,是说有什么办法可以替代distinct,得到同样的结果.答案都被大家说的差不多了,发现挺有意思的,就记录一下: SQL> select num from t1; ...
- 我的第一篇博客--SQL小语句
开通了博客,拥有了属于自己的小小天地.先写一篇今儿刚学到的 1 remove mirroring relationship alter database datab_name set partner ...
- Appium路线图及1.0正式版发布
Appium更新的速度极快,从我试用时候的0.12到1.0(0.18版本后就是1.0),完全符合移动互联网的节奏. 更新可能会慢,可以多试几次 整理了testerhome上思寒发表的帖子,让我们来看下 ...
- jquery 实现邮箱输入自动提示功能:(一)
记得去年做某个项目的时候,用到了邮箱输入自动提示功能,于是网上搜了一下,发现了这个写得不错,现在回想起来,转载一下,方便查阅. 邮箱的广泛使用得益于它的免费,因此很多网站在注册的时候都会直接使用邮箱作 ...
- python如何控制数据库?
http://www.w3cschool.cc/python/python-mysql.html 通过利用MySQLdb可以操作数据库 实例: 以下实例链接Mysql的TESTDB数据库: # enc ...
- 关于JAVA应用中文字体显示小方框的问题解决
最近碰到linux下jboss应用中中文字体显示为小方框: “在JRE 5以上的java环境中,java会自动加载$JAVA_HOME/jre/lib/fonts目录下的字体.链接或复制宋体或微软雅黑 ...