zoukankan      html  css  js  c++  java
  • Jcrontab定时任务

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





    附件列表

  • 相关阅读:
    数据库设计三大范式
    常用正则表达式
    全国省市县无刷新多级关联菜单
    可选择Email和用户名登录的代码
    注册与登录界面的美化
    只能输入汉字js脚本
    js确认删除对话框
    同步文本框内容的JS代码
    网站变灰代码
    悬浮右侧可展开搜索的客服代码
  • 原文地址:https://www.cnblogs.com/douJiangYouTiao888/p/6474023.html
Copyright © 2011-2022 走看看