This article shares a simple introduction to JCrontab for your reference. The specific content is as follows
Create a JavaWeb project
1. First, you need to download the relevant jar package of JCrontab, Jcrontab-2.0-RC0.jar. Put it in the lib folder.
2. Create a new file jcrontab.properties under src as follows:
#crontab.xml
The directory of the file, this is the job scheduling rule
org.jcrontab.data.file =E:/EclipseWorkspace/ADemo/WebContent/WEB-INF/crontab.xml
#sax parsing driver type
org.xml.sax.driver=org.apache.xerces.parsers.SAXParser
#datasource file type
org.jcrontab.data.datasource=org.jcrontab.data.XMLSource
3. Create a new file crontab.xml under WEB-INF
<?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><bussinesd ays>true</bussinesdays><startDate></startDate><endDate></endDate><class>xu.crontab.Crontab1</class><method>run</method><parameters></parameters><description></description></crontabentry></crontab>
Take the above attributes on Baidu by yourself. <seconds>0,5,10,15,20,25,30,35,40,45,50,55</seconds> This is a multiple of the second number of 5 and calls the job.
4. The web.xml configuration is as follows
<?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 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><!--The path here is the absolute path--><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. Create two new java files under the xu.crontab package (see the top screenshot of the file location) [Never mind other java files]
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!!!"); }}After starting tomcat, you can see that the job is called every 5 seconds. I hope you will be successful.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.