【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, 赞美: ...
随机推荐
- PHP模拟发送POST请求之二、用PHP和JS处理URL信息
明白了HTTP请求的头信息后,我们还需要对请求地址有所了解.再者,HTTP GET请求是靠URL实现的,所以了解URL的构造,处理URL的重要性不言而喻. 在PHP中我们用parse_url()函数来 ...
- 常用HTML正则
<?php //HTML a连接正则 $str = ''; $isMatched = preg_match('/<a.*?[^<]>.*?<\/a>/', $str ...
- webstorm 注册码
User Name: EMBRACE License Key: ===== LICENSE BEGIN ===== 24718-12042010 00001h6wzKLpfo3gmjJ8xoTPw5m ...
- 最近学习linux常用命令。
一.文件系统的管理tips:输入命令的时候要常用tab键来补全 ls 查看目录信息 ( ls / ) ls -l 等价于 llpwd 查看当前所处的路径 cd 切换目录 (cd /) ,如果不带参数则 ...
- android去掉顶部标题栏
在清单文件(manifest.xml)里面实现 <application> <activity android:name="cn.ui.activity.UserRegAc ...
- nginx看端口使用情况
[root@iZ94j7ahvuvZ sbin]# netstat -apn Active Internet connections (servers and established) Proto R ...
- Openstack-Ceilometer-SNMP的使用
1. 物理服务器配置 1.1安装 #yum install -y net-snmp net-snmp-utils 1.2 配置 复制[附件]中snmpd.conf文件到/etc/snmp/目 ...
- python中str()和repr()的区别
>>> s = 'Hello, world.' >>> str(s) 'Hello, world.' >>> repr(s) "'Hel ...
- 用SqlParameter 给SQL传递参数
1.数据访问层 using的用法: 01.可以using System;导命名控空间 02.using 的语法结构 using(变量类型 变量名 =new 变量类型()) { } 案例: 03.us ...
- Hibernate入门注解笔记
@Entity 代表实体 映射一张表 @Table 定义表的属性 @Embeddable 定义类级别可以被嵌入 @Id 指定主键 @GeneratedValue 指定主键生成策略 @Column指定列 ...