本文实例为大家分享了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秒就调用一下,希望你们顺利。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。