JCrontab简单入门实例详解
程序员文章站
2023-12-10 14:42:16
本文实例为大家分享了jcrontab简单入门,供大家参考,具体内容如下
创建一个javaweb项目
1、首先要下载jcrontab的相关jar包,jcrontab-...
本文实例为大家分享了jcrontab简单入门,供大家参考,具体内容如下
创建一个javaweb项目
1、首先要下载jcrontab的相关jar包,jcrontab-2.0-rc0.jar。放到lib文件夹下。
2、在src下新建文件jcrontab.properties如下:
#crontab.xml
文件的目录,这个是作业调度规则
org.jcrontab.data.file =e:/eclipseworkspace/ademo/webcontent/web-inf/crontab.xml
#sax解析驱动类型
org.xml.sax.driver=org.apache.xerces.parsers.saxparser
#datasource文件类型
org.jcrontab.data.datasource=org.jcrontab.data.xmlsource
3、在web-inf下新建文件crontab.xml
<?xml version="1.0" encoding="utf-8"?> <crontab> <crontabentry id="2014"> <seconds>0,5,10,15,20,25,30,35,40,45,50,55</seconds> <minutes>*</minutes> <hours>*</hours> <daysofmonth>*</daysofmonth> <months>*</months> <daysofweek>*</daysofweek> <years>*</years> <bussinesdays>true</bussinesdays> <startdate></startdate> <enddate></enddate> <class>xu.crontab.crontab1</class> <method>run</method> <parameters></parameters> <description></description> </crontabentry> </crontab>
以上属性自己百度一下,<seconds>0,5,10,15,20,25,30,35,40,45,50,55</seconds>这个是秒数是5的倍数就调用作业
4、web.xml配置如下
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="webapp_id" version="3.1"> <display-name>ademo</display-name> <servlet> <servlet-name>loadonstartupservlet</servlet-name> <servlet-class>xu.crontab.loadcrontabservlet</servlet-class> <init-param> <param-name>properties_file</param-name> <!--此处路径是绝对路径 --> <param-value>e:\eclipseworkspace\ademo\src\jcrontab.properties</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>loadonstartupservlet</servlet-name> <url-pattern>/startup</url-pattern> </servlet-mapping> </web-app>
5、在xu.crontab包下新建两个java文件(文件位置见最上的截图)【其他java文件不用理会】
loadcrontabservlet.java
package xu.crontab; import java.io.fileinputstream; import java.io.ioexception; import java.io.inputstream; import java.util.enumeration; import java.util.properties; import javax.servlet.servletconfig; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import org.jcrontab.crontab; import org.jcrontab.log.log; public class loadcrontabservlet extends httpservlet { /** * */ private static final long serialversionuid = 1l; private crontab cron = null; public void init(servletconfig config) throws servletexception { super.init(config); try { system.out.print("working?..."); process(); system.out.println("ok"); } catch (exception e) { throw new servletexception(e); } } protected inputstream createpropertiesstream(string name) throws ioexception { return new fileinputstream(name); } public void process() { string propz = "jcrontab.properties"; string props = getservletconfig().getinitparameter("properties_file"); if (props == null) { props = propz; } properties propobj = new properties(); try { inputstream input = createpropertiesstream(props); propobj.load(input); } catch (ioexception ioe) { ioe.printstacktrace(); } servletconfig c = getservletconfig(); enumeration keys = c.getinitparameternames(); while (keys.hasmoreelements()) { string key = (string) keys.nextelement(); propobj.setproperty(key, c.getinitparameter(key)); } cron = crontab.getinstance(); try { shutdownhook(); cron.init(propobj); } catch (exception e) { log.error(e.tostring(), e); } } public void shutdownhook() throws exception { runtime.getruntime().addshutdownhook(new thread() { public void run() { dostop(); } }); } public void destroy() { dostop(); } public void dostop() { log.info("shutting down..."); cron.uninit(100); log.info("stoped"); } }
crontab1.java
package xu.crontab; import java.util.date; public class crontab1 { public static void run(string[] args) { system.out.println(new date()+"-----> hello world!!!"); } }
启动tomcat后即可看到作业每隔5秒就调用一下,希望你们顺利。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。