Dieser Artikel hat eine einfache Einführung in JCrontab als Referenz. Der spezifische Inhalt ist wie folgt
Erstellen Sie ein Javaweb -Projekt
1. Erstens müssen Sie das entsprechende JAR-Paket von JCrontab, JCrontab-2.0-Rc0.jar, herunterladen. Legen Sie es in den Lib -Ordner.
2. Erstellen Sie eine neue Datei jCrontab.properties unter SRC wie folgt:
#crontab.xml
Das Verzeichnis der Datei ist die Regelplanungsregel
org.jcrontab.data.file = e: /clipseworkspace/ademo/webcontent/web-inf/crontab.xml
#SAX Parsing -Treibertyp
org.xml.sax.driver = org.apache.xerces.parsers.saxparser
#DataSource -Dateityp
org.jcrontab.data.datasource = org.jcrontab.data.xmlSource
3. Erstellen Sie eine neue Datei crontab.xml unter web-inf
<? id = "2014"> <Sekunden> 0,5,10,15,20,25,30,35,40,45,50,55 </Sekunden> <Minuten>*</minutes>*</stunden> <daysofmonth>*</daysofmonth> <monate>*</monates> </daysosingsdosings>*</daysofwofWeek> </daysofwofWeek> </touth> <jahrelang> AYS> TRUE </bussinesdays> <startdate> </startdate> <enddate> </enddate> <class> xu.crontab.crontab1 </class> <Methode> run </methode> <parameter> </parameters> <beschreibung
Nehmen Sie die obigen Attribute auf Baidu selbst. <Sekunden> 0,5,10,15,20,25,30,35,40,45,50,55 </Sekunden> Dies ist ein Vielfaches der zweiten Anzahl von 5 und nennt den Job.
4. Die Konfiguration von web.xml ist wie folgt
<xml Version = "1.0" coding = "utf-8"?> <web-App xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http: //xmlns.j cp.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 "> <diagnern-name> ademo </display-name> <servlet> <Servlet-name> loadonStartUpServlet </servlet-name> <Servlet-Klasse> xu.crontab.loadcrontabServlet </Servlet-Class> <init-param> <param-name> Eigenschaften_file </param-name> <!-Der Pfad ist der absolute Path. src/jcrontab.Properties </param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet> <Servlet-Mapping> <Servlet-name> loadonStartupserservlet </servlet-name> <url-puffer>/startup </url-puffer>
5. Erstellen Sie zwei neue Java -Dateien unter dem Xu.Crontab -Paket (siehe Top Screenshot des Dateispeichers) [Egal andere Java -Dateien]
LoadcrontabServlet.java
Paket xu.crontab; importieren java.io.fileinputstream; import java.io.ioxception; import Java.io.inputStream; import Java.util.enumeration; import Java.util.Properties; importieren javax.servlet.servletconfig; import. org.jcrontab.crontab; import org.jcrontab.log.log; öffentliche Klasse loadCrontabServlet erweitert HttpServlet { /** * * /Private statische endgültige lange Serialversionuid = 1L; Private Crontab Cron = null; public void init (servletConfig config) löst ServletException {Super.init (config) aus; try {system.out.print ("Arbeiten? "jcrontab.properties"; String props = getServletConfig (). GetInitParameter ("Properties_file"); if (props == null) {props = propz;} Eigenschaften propobj = neue Eigenschaften (); try {InputStream input = createProperTieStream (Props); PropOBJ.load (Eingabe);} catch (ioException ioe) {ioe.printStacktrace (); C.GetinitParameternames (); while (keys.hasmoreElements ()) {String key = (String) keys.nextElement (); PropOBJ.SetProperty (Schlüssel, C.GetInitParameter (Schlüssel));} cron = crontab.getInstance (); try {Shortdownhook (); cron.init (PropOBJ);} catch (Ausnahme E) {log.Error (e.tostring (), e);}} public void schadendownhook () löst die Ausnahme aus. {dostop ();} public void dostop () {log.info ("Abschalten ..."); Cron.uninit (100); log.info ("Stoped");}} Crontab1.java
Paket xu.crontab; import Java.util.date; public class crontab1 {public static void run (String [] args) {System.out.println (neues Datum ()+"------> Hallo Welt !!!"); }}Nach dem Start von Tomcat können Sie sehen, dass der Job alle 5 Sekunden aufgerufen wird. Ich hoffe, Sie werden erfolgreich sein.
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.