您好,登錄后才能下訂單哦!
本文實例為大家分享了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秒就調用一下,希望你們順利。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。