两篇博客:
http://blog.csdn.net/jijijiujiu123/article/details/9086847 网站同事写的(chenrui)
原理比较简单,基于Jcrontab,使用jar包是:Jcrontab-2.0-RC0.jar,详见附件。基于web.xml配置servlet,然后配置.properties文件,包含log4j、数据源等。
Jcrontab的工作原理是配置一系列数据源,由Jcrontab按照定时规则去处理的类和程序。数据源的配置程序可以是写到文件中、数据库中或者XML文件中。(详见附件CSDN某博客)
标准配置:
web.xml配置:
<servlet>
<servlet-name>LoadOnStartupServlet</servlet-name>
<servlet-class>org.jcrontab.web.loadCrontabServlet</servlet-class>
<init-param>
<param-name>PROPERTIES_FILE</param-name>
<!--此处路径是绝对路径 -->
<param-value>C:UserspcWorkspacesMyEclipse 8.5jcrontabsrcjcrontab.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>
jcrontab.properties配置:
#
# Those are the parameters to the DAO
#
# FileSource:
# org.jcrontab.data.file
# org.jcrontab.data.datasource
#
# SQLSource:
# org.jcrontab.data.GenericSQLSource.driver
# org.jcrontab.data.GenericSQLSource.url
# org.jcrontab.data.GenericSQLSource.password
# org.jcrontab.data.datasource
#
org.jcrontab.data.file = {$HOME}.jcrontab/crontab
org.jcrontab.data.datasource = org.jcrontab.data.FileSource
org.jcrontab.Crontab.refreshFrequency = 3
# Those lines are necessaty to parse the XML file
#org.xml.sax.driver=org.apache.xerces.parsers.SAXParser
# Those Lines are necessary to connect to postgresql and use it as DataSource
#org.jcrontab.data.GenericSQLSource.driver = org.postgresql.Driver
#org.jcrontab.data.GenericSQLSource.url = jdbc:postgresql://yourmachine.jcrontab.org:5432/jcrontab
# Those Lines are necessary to connect to mysql and use it as DataSource
#org.jcrontab.data.GenericSQLSource.driver = org.gjt.mm.mysql.Driver
#org.jcrontab.data.GenericSQLSource.url = jdbc:mysql://yourmachine.jcrontab.org:3306/jcrontab
#org.jcrontab.data.GenericSQLSource.username = iolalla
#org.jcrontab.data.GenericSQLSource.password = yourpassword
#org.jcrontab.data.datasource = org.jcrontab.data.GenericSQLSource
#org.jcrontab.data.GenericSQLSource.dbDataSource=yourDS
# Those lines are necesary to send mail
#org.jcrontab.sendMail.to=iolalla@yahoo.com
#org.jcrontab.sendMail.from=jcrontab@yoursystem.com
#org.jcrontab.sendMail.smtp.host=smtp.yahoo.com
#org.jcrontab.sendMail.smtp.user= yourSMTPusername
#org.jcrontab.sendMail.smtp.password=yourSMTPpassword
#Those lines defines the default Logger for the system
#org.jcrontab.log.Logger=jcrontabplugin.jEditLogger
org.jcrontab.log.Logger=org.jcrontab.log.Log4JLogger
org.jcrontab.log.log4J.Properties={$HOME}.jcrontab/log4j.properties
#org.jcrontab.data.FileOpener=file
#Those parameters are to get the Holidays from the system
#org.jcrontab.data.holidaysource=org.jcrontab.data.HoliDayFileSource
#org.jcrontab.data.holidaysfilesource={$HOME}.jcrontab/holidays
#To change this plz refer to java.text.SimpleDateFormat
#org.jcrontab.data.dateFormat=dd/MM/yyyy
通过阅读源码,我们可以看到loadcrontabServlet的主要功能就是从一个名为jcrontab.properties的文件中读取各种属性并初始化。
项目配置:
web.xml配置:
<servlet>
<servlet-name>servletjcrontab</servlet-name>
<servlet-class>公司自定义包.jcrontab.ServletJcrontab</servlet-class>
<init-param>
<param-name>org.jcrontab.data.datasource</param-name>
<param-value>公司自定义包.jcrontab.JcrontabSQLSource</param-value>
</init-param>
<load-on-startup>200</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>servletjcrontab</servlet-name>
<url-pattern>/servletjcrontab</url-pattern>
</servlet-mapping>
我买网在这一步的处理方式略有不同,原理大体相同。自己封装了一下。
刚才我们说过数据源的处理有三种方式,我买网采用数据库存储的方式。
数据库的方式标准配置是在jcrontab.properties配置文件中配置各种url、driver....(配置数据源那一套),但是现有项目早已经有数据源无需重新配置。
我买网自己实现而不读取jcrontab.properties中的数据源配置。
jcrontab.properties:
org.jcrontab.log.log4J.Properties=WEB-INF/classes/log4j.properties
org.jcrontab.version=2.0.RC1
org.jcrontab.log.Logger=org.jcrontab.log.Log4JLogger
org.jcrontab.data.dateFormat=dd/MM/yyyy
org.jcrontab.data.nodetype=58
org.jcrontab.data.datasource = net.xinshi.jemall.jcrontab.JcrontabSQLSource
org.jcrontab.Crontab.refreshFrequency =3
jcrontab.properties的详细配置详见源码jar包中的文件。