本文实例为大家分享了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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?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配置如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
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
1
2
3
4
5
6
7
8
9
|
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秒就调用一下,希望你们顺利。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/u013521220/article/details/78794134