Este artigo compartilha uma introdução simples ao JCrontab para sua referência. O conteúdo específico é o seguinte
Crie um projeto Javaweb
1. Primeiro, você precisa baixar o pacote JAR relevante do jcrontab, jcrontab-2.0-rc0.jar. Coloque -o na pasta Lib.
2. Crie um novo arquivo jcrontab.properties sob SRC da seguinte forma:
#Crontab.xml
O diretório do arquivo, esta é a regra de agendamento de empregos
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 Tipo de arquivo
org.jcrontab.data.dataSource = org.jcrontab.data.xmlsource
3. Crie um novo arquivo Crontab.xml em Web-Inf
<? xml versão = "1.0" coding = "utf-8"?> <rcrontab> <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 </BUSSINESSDAYS> <CARTDATE> </startDate> <dddate> </nddate> <class> xu.crontab.crontab1 </class> <odhods> run </métodos </parâmetros> </parameters> <crection> </chection> </rontabentry> </crontab>
Pegue os atributos acima no Baidu sozinho. <Cends> 0,5,10,15,20,25,30,35,40,45,50,55 </conds> Este é um múltiplo do segundo número de 5 e chama o trabalho.
4. A configuração web.xml é a seguinte
<? xml versão = "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 "=" 3 "3llay> <Verlet> <Verlet-name> loadOnStartUpServlet </servlet-name> <Servlet-class> xu.crontab.loadcrontabservlet </servlet-class> <iit-param> <amam-name> Properties_file </param-name> <! src/jcrontab.properties </param-value> </irit-param> <adarot-on-startup> 1 </olload-on-startup> </servlet> <Verlet-mapping> <Vertlet-name> loadOnstartupServlet </servlet-name> <url-stattern>/startup-papp--Pattern>
5. Crie dois novos arquivos java no pacote Xu.Crontab (consulte a captura de tela superior do local do arquivo) [Não importa outros arquivos java]
LoadCrontabServlet.java
pacote xu.crontab; importar java.io.fileInputStream; importar java.io.ioException; importar java.io.inputStream; importar java.util.enumeration; importar java.util.properties; importexception.servlet.servletLonfig; import Javax.ServLetTexceptException; importexcept.Mervlet.ServletLonfig; org.jcrontab.crontab; importar org.jcrontab.log.log; classe pública LoadCrontabServlet estende httpServlet { /** * * /private estático final serialversionuid = 1L; privado crontab cron = null; public void init (servletConfig config) lança servletexception {super.init (config); tente {System.out.print ("Working? ..."); Process (); System.out.println ("OK");} Catch (Exceção e) {Throw New ServleTexception (e);}} protegida InputStream CreatePropertReRTream (Nome String) Throws IoException {Return FileInputStream (Name); {names (names) (names) (names); "jcrontab.properties"; String props = getServletConfig (). GetinitParameter ("Properties_file"); if (props == null) {props = propz;} Propriedades PropoBJ = new Properties (); Try {inputStream input = CreatePropertiessTream (Props); Propobj.load (input);} Catch (IoException Ioe) {Ioe.PrintStAlTrace ();}; 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 (Exceção e) {Log.error (e.ToString (), E);}} public void ShutdownHook () lança excepção {RunTime.getRuntime (). AddShutdook (new) {public; {Dostop ();} public void Dostop () {Log.info ("Desligando ..."); Cron.uninit (100); log.info ("Stoped");}}} Crontab1.java
pacote xu.crontab; importar java.util.date; classe pública Crontab1 {public static void run (string [] args) {System.out.println (new Date ()+"-----> Hello World !!!"); }}Depois de iniciar o Tomcat, você pode ver que o trabalho é chamado a cada 5 segundos. Espero que você seja bem -sucedido.
O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.