1. package cn.com.pansky.xmdswz.application.scheduler;
2.
3. import org.apache.commons.logging.Log;
4. import org.apache.commons.logging.LogFactory;
5. import org.quartz.SchedulerException;
6. import org.quartz.impl.StdScheduler;
7. import org.springframework.beans.factory.BeanFactory;
8. import org.springframework.context.support.ClassPathXmlApplicationContext;
9. import cn.com.pansky.xmdswz.system.cache.CachedTableMgr;
10. import cn.com.pansky.xmdswz.system.config.SystemConfig;
11. import cn.com.pansky.xmdswz.utility.DateUtil;
12. import org.quartz.JobDetail;
13. import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
14. import java.net.ServerSocket;
15. import java.io.*;
16.
17. /**
18. * Title: XXXXXXX
19. * Description: XXXXXXXXXXXX
20. * Copyright: Copyright (c) 2006
21. * Company: www.pansky.com.cn
22. *
23. * @author Sheng Youfu
24. * @version 1.0
25. */
26. public class Scheduler {
27. private static Log log = LogFactory.getLog(Scheduler.class);
28.
29. private static ServerSocket srvSocket = null; //服务线程,用以控制服务器只启动一个实例
30.
31. private static final int srvPort = 12345; //控制启动唯一实例的端口号,这个端口如果保存在配置文件中会更灵活
32.
33. /**
34. * 定时任务配置文件
35. */
36. private static String CONFIG_FILE = "cn/com/pansky/xmdswz/application/scheduler/Scheduling-bean.xml";
37.
38.
39. public Scheduler() {
40. //检测系统是否只启动一个实例
41. checkSingleInstance();
42.
43. //下面读取Spring的配置文件
44. SystemConfig cfg = new SystemConfig();
45. String config = cfg.parseParam("SCHEDULER.CONFIG_FILE", false);
46. if(config!=null && !"".equals( config.trim()))
47. CONFIG_FILE = config;
48. log.debug("CONFIG_FILE: "+CONFIG_FILE);
49. }
50.
51. /**
52. * 主函数
53. * @param args String[]
54. * @throws Exception
55. */
56. public static void main(String[] args) throws Exception{
57. Scheduler sch = new Scheduler();
58. sch.execute();
59. }
60.
61. /**
62. * 运行定时任务
63. */
64. public void execute() {
65. ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {CONFIG_FILE});
66. BeanFactory factory = (BeanFactory) appContext;
67.
68. /**
69. * 装载任务调度
70. */
71. StdScheduler scheduler = (StdScheduler) factory.getBean("schedulerFactoryBean");
72. //先暂停所有任务,等待装载缓存代码表
73. try {
74. scheduler.pauseAll();
75. } catch (SchedulerException ex) {
76. log.error("",ex);
77. }
78.
79. /**
80. * 装载缓存代码表
81. */
82. CachedTableMgr cachedtableMgr = (CachedTableMgr) factory.getBean("cachedTableMgr");
83. try {
84. cachedtableMgr.loadCodeTable();
85. } catch (Exception ex) {
86. log.fatal("Load cached table failed. System will exit.", ex);
87. System.exit(0);
88. }
89.
90. //重新恢复所有任务
91. try {
92. scheduler.resumeAll();
93. } catch (SchedulerException ex) {
94. log.error("",ex);
95. }
96. }
97.
98. /**
99. * 检测系统是否只启动了一个实例
100. */
101. protected void checkSingleInstance() {
102. try {
103. srvSocket = new ServerSocket(srvPort); //启动一个ServerSocket,用以控制只启动一个实例
104. } catch (IOException ex) {
105. if(ex.getMessage().indexOf("Address already in use: JVM_Bind")>=0)
106. System.out.println("在一台主机上同时只能启动一个进程(Only one instance allowed)。");
107. log.fatal("", ex);
108. System.exit(0);
109. }
110. }
111. }
2.
3. import org.apache.commons.logging.Log;
4. import org.apache.commons.logging.LogFactory;
5. import org.quartz.SchedulerException;
6. import org.quartz.impl.StdScheduler;
7. import org.springframework.beans.factory.BeanFactory;
8. import org.springframework.context.support.ClassPathXmlApplicationContext;
9. import cn.com.pansky.xmdswz.system.cache.CachedTableMgr;
10. import cn.com.pansky.xmdswz.system.config.SystemConfig;
11. import cn.com.pansky.xmdswz.utility.DateUtil;
12. import org.quartz.JobDetail;
13. import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
14. import java.net.ServerSocket;
15. import java.io.*;
16.
17. /**
18. * Title: XXXXXXX
19. * Description: XXXXXXXXXXXX
20. * Copyright: Copyright (c) 2006
21. * Company: www.pansky.com.cn
22. *
23. * @author Sheng Youfu
24. * @version 1.0
25. */
26. public class Scheduler {
27. private static Log log = LogFactory.getLog(Scheduler.class);
28.
29. private static ServerSocket srvSocket = null; //服务线程,用以控制服务器只启动一个实例
30.
31. private static final int srvPort = 12345; //控制启动唯一实例的端口号,这个端口如果保存在配置文件中会更灵活
32.
33. /**
34. * 定时任务配置文件
35. */
36. private static String CONFIG_FILE = "cn/com/pansky/xmdswz/application/scheduler/Scheduling-bean.xml";
37.
38.
39. public Scheduler() {
40. //检测系统是否只启动一个实例
41. checkSingleInstance();
42.
43. //下面读取Spring的配置文件
44. SystemConfig cfg = new SystemConfig();
45. String config = cfg.parseParam("SCHEDULER.CONFIG_FILE", false);
46. if(config!=null && !"".equals( config.trim()))
47. CONFIG_FILE = config;
48. log.debug("CONFIG_FILE: "+CONFIG_FILE);
49. }
50.
51. /**
52. * 主函数
53. * @param args String[]
54. * @throws Exception
55. */
56. public static void main(String[] args) throws Exception{
57. Scheduler sch = new Scheduler();
58. sch.execute();
59. }
60.
61. /**
62. * 运行定时任务
63. */
64. public void execute() {
65. ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {CONFIG_FILE});
66. BeanFactory factory = (BeanFactory) appContext;
67.
68. /**
69. * 装载任务调度
70. */
71. StdScheduler scheduler = (StdScheduler) factory.getBean("schedulerFactoryBean");
72. //先暂停所有任务,等待装载缓存代码表
73. try {
74. scheduler.pauseAll();
75. } catch (SchedulerException ex) {
76. log.error("",ex);
77. }
78.
79. /**
80. * 装载缓存代码表
81. */
82. CachedTableMgr cachedtableMgr = (CachedTableMgr) factory.getBean("cachedTableMgr");
83. try {
84. cachedtableMgr.loadCodeTable();
85. } catch (Exception ex) {
86. log.fatal("Load cached table failed. System will exit.", ex);
87. System.exit(0);
88. }
89.
90. //重新恢复所有任务
91. try {
92. scheduler.resumeAll();
93. } catch (SchedulerException ex) {
94. log.error("",ex);
95. }
96. }
97.
98. /**
99. * 检测系统是否只启动了一个实例
100. */
101. protected void checkSingleInstance() {
102. try {
103. srvSocket = new ServerSocket(srvPort); //启动一个ServerSocket,用以控制只启动一个实例
104. } catch (IOException ex) {
105. if(ex.getMessage().indexOf("Address already in use: JVM_Bind")>=0)
106. System.out.println("在一台主机上同时只能启动一个进程(Only one instance allowed)。");
107. log.fatal("", ex);
108. System.exit(0);
109. }
110. }
111. }
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货