引言
上篇文章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。