Java的几种定时任务

墨蓝 2022-08-05 15:05 251阅读 0赞

1、XML文件配置的job

  1. web.xml
  2. <servlet>
  3. <servlet-name>QuartzInitializer</servlet-name>
  4. <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
  5. <init-param>
  6. <param-name>shutdown-on-unload</param-name>
  7. <param-value>true</param-value>
  8. </init-param>
  9. <init-param>
  10. <param-name>config-file</param-name>
  11. <param-value>quartz.properties</param-value>
  12. </init-param>
  13. <load-on-startup>2</load-on-startup>
  14. </servlet>

quartz.properties

#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = org.quartz.scheduler.instanceId = AUTO
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 3
org.quartz.threadPool.threadPriority = 5
#============================================================================
# Configure Plugins
#============================================================================
#org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
#org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.class = com.coship.dhm.portalMS.common.util.DHMJobInitializer
#org.quartz.plugin.jobInitializer.fileName = quartz.xml
#\u5fc5\u987b\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u914d\u7f6e\u6210quartz_nanjing-classify.xml\uff08\u5357\u4eac\u5206\u7ea7\uff09\u6216\u8005quartz_tianwei.xml \u6216 quartz_nanjing-no-classify.xml\uff08\u4e0d\u5206\u7ea7\uff09—>
org.quartz.plugin.jobInitializer.fileNames = quartz_tianwei.xml
org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval = 10

quartz_tianwei.xml

<?xml version=”1.0” encoding=”UTF-8”?>




AutoSentPublishTaskJob
AutoSentPublishTaskJobGroup
定时发布门户任务(Release scheduled portal task)
com.coship.dhm.portalMS.publishMs.task.AutoSentPublishTask



AutoSentPublishTaskJobCron
AutoSentPublishTaskJobGroup
AutoSentPublishTaskJob
AutoSentPublishTaskJobGroup
0 * * * * ?







2、spring框架








doRun







10 0/5 * * * ?

appContextServiceSynchronize.xml
<?xml version=”1.0” encoding=”UTF-8”?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN 2.0//EN” “http://www.springframework.org/dtd/spring-beans-2.0.dtd">










doRun







10 0/5 * * * ?


…….

appContextService.xml
<?xml version=”1.0” encoding=”UTF-8”?>
































PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED,readOnly
















com.coship.dhm.nspAdapter.*\.execute





web.xml

<?xml version=”1.0” encoding=”UTF-8”?>


nspAdapter

contextConfigLocation

classpath:spring/appContextService.xml,
classpath:spring/applicationContext-operation.xml




org.mule.config
/WEB-INF/mule-transformer-config.xml,/WEB-INF/mule-server-config.xml


dhm-system-name
nspAdapter


system.config-path-name
conf


checkConfigFilePath
conf/para_check_config.xml

  1. <context-param>
  2. <param-name>log.config-file</param-name>
  3. <param-value>log4j.properties</param-value>
  4. </context-param>
  5. <context-param>
  6. <param-name>log.config-file-refresh-interval</param-name>
  7. <param-value>6000</param-value>
  8. </context-param>
  9. <context-param>
  10. <param-name>system.log-path-name</param-name>
  11. <param-value>log</param-value>
  12. </context-param>
  13. <context-param>
  14. <param-name>webAppRootKey</param-name>
  15. <param-value> app.root </param-value>
  16. </context-param>
  17. <listener>
  18. <listener-class>
  19. org.springframework.web.util.Log4jConfigListener
  20. </listener-class>
  21. </listener>
  22. <listener>
  23. <listener-class>
  24. com.coship.dhm.common.uif.system.StartupListener
  25. </listener-class>
  26. </listener>
  27. <!--
  28. <listener>
  29. <listener-class>
  30. com.coship.dhm.common.init.InitSystemListener
  31. </listener-class>
  32. </listener>
  33. -->
  34. <listener>
  35. <listener-class>
  36. com.coship.dhm.nspAdapter.common.NSPInitUifListener
  37. </listener-class>
  38. </listener>
  39. <listener>
  40. <listener-class>
  41. com.coship.dhm.nspAdapter.common.NSPInitSerListener
  42. </listener-class>
  43. </listener>
  44. <!--
  45. <listener>
  46. <listener-class>
  47. com.coship.dhm.common.uif.system.ACFStartupListener
  48. </listener-class>
  49. </listener>
  50. <listener>
  51. <listener-class>
  52. com.coship.dhm.aaa.daf.DAFStartListener
  53. </listener-class>
  54. </listener>
  55. -->
  56. <filter>
  57. <filter-name>encoding</filter-name>
  58. <filter-class>
  59. org.springframework.web.filter.CharacterEncodingFilter
  60. </filter-class>
  61. <init-param>
  62. <param-name>encoding</param-name>
  63. <param-value>UTF-8</param-value>
  64. </init-param>
  65. </filter>
  66. <filter-mapping>
  67. <filter-name>encoding</filter-name>
  68. <url-pattern>/\*</url-pattern>
  69. </filter-mapping>
  70. <filter>
  71. <filter-name>ipAuthFilter</filter-name>
  72. <filter-class>com.coship.dhm.common.uif.system.IpAuthFilter</filter-class>
  73. </filter>
  74. <filter-mapping>
  75. <filter-name>ipAuthFilter</filter-name>
  76. <url-pattern>/services/\*</url-pattern>
  77. </filter-mapping>
  78. <!--<servlet>
  79. <servlet-name>MonitorServlet</servlet-name>
  80. <servlet-class>
  81. com.coship.aaa.monitor.servlet.MonitorServlet
  82. </servlet-class>
  83. <load-on-startup>1</load-on-startup>
  84. </servlet>
  85. -->
  86. <listener>
  87. <listener-class>com.coship.backupmode.listener.JBackupListener</listener-class>

  1. <listener>
  2. <listener-class>
  3. com.coship.dhm.nspAdapter.common.util.AdapterDBInitListener
  4. </listener-class>

  1. <servlet>
  2. <servlet-name>muleServlet</servlet-name>
  3. <servlet-class>
  4. org.mule.transport.servlet.MuleReceiverServlet
  5. </servlet-class>
  6. <load-on-startup>1</load-on-startup>
  7. </servlet>
  8. <servlet-mapping>
  9. <servlet-name>muleServlet</servlet-name>
  10. <url-pattern>/services/\*</url-pattern>
  11. </servlet-mapping>
  12. <welcome-file-list>
  13. <welcome-file>index.jsp</welcome-file>
  14. </welcome-file-list>

3、Timer
public class SystemXmlFileTask extends TimerTask
public void run()
{

  1. try
  2. \{
  3. init();
  4. \}
  5. catch (Exception e)
  6. \{
  7. logger.error("SystemXmlFileTask Exception" + e.getMessage());
  8. \}
  9. \}

//1.初始化系统配置文件刷新时间
int systemRefreshInterval = 60000;
try
{
systemRefreshInterval = Integer.parseInt(ctx.getInitParameter(SYSTEM_REFRESH_INTERVAL));
}

SystemXmlFileTask systemXmlFileTask = new SystemXmlFileTask(
configPath + FS + SYSTEMCONFIG_FILE_NAME);
systemXmlFileTask.init();
Timer timer = new Timer();
timer.schedule(systemXmlFileTask,
systemRefreshInterval,
systemRefreshInterval);

发表评论

表情:
评论列表 (有 0 条评论,251人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring定时任务实现

    近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整理了一下定时任务的几种实现方式,由于项目采用spring框架,所以我都将结合

    相关 JAVA定时任务方法

    \[b\]\[size=large\]在应用里经常都有用到在后台跑定时任务的需求。举个例子,比如需要在服务后台跑一个定时任务来进行垃圾回收(译者注:个人觉得用定时任务来跑垃圾回

    相关 主流定时分布式任务

    单机定式任务调度的问题 在很多应用系统中我们常常要定时执行一些任务。比如,订单系统的超时状态判断、缓存数据的定时更新、定式给用户发邮件,甚至是一些定期计算的报表等等。常见