java 定时任务问题 一个彩票接口的问题,有N个彩种,读取第N个彩种的当前期次的开奖时间,然后延迟第N个小时开奖,这个时候触发开奖业务,有什么好的解决方案? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用Timer还有些个类继承TimerTask,重写run()方法 可以使用线程,Timer,或者qurtz框架,等等以Timer为例Date d = 获取开奖时间java.util.Timer timer = new java.util.Timer();timer.schedule(new TimerTask(){ public void run() { //调用开奖业务 }}, d.getTime()-System.currentTimeMillis()); //设置延时(开奖时间-当前时间) 用spring 配置 然后继承TimerTask接口 重写run方法 1. <?xml version="1.0" encoding="UTF-8"?> 2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" > 3. <beans> 4. <!—要调度的对象--> 5. <bean id="testQuarz" class="springtimer.TestQuarz" /> 6. 7. <!--定义定时执行testQuarz 这个bean中的sayHello()方法--> 8. <bean id="helloworldTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 9. <property name="targetObject"> 10. <ref bean="testQuarz" /> 11. </property> 12. <property name="targetMethod"> 13. <value>sayHello</value> 14. </property> 15. </bean> 16. 17. <!--触发器的bean的设置,在这里我们设置了我们要触发的jobDetail是哪个。这里我们定义了要触发的jobDetail是helloworldTask,即触发器去触发哪个bean..并且我们还定义了触发的时间:每天5:17pm--> 18. <bean id="cronTrigger" 19. class="org.springframework.scheduling.quartz.CronTriggerBean"> 20. <property name="jobDetail"> 21. <ref bean=" helloworldTask " /> 22. </property> 23. <property name="cronExpression"> 24. <!-- 关键在配置此表达式 --> 25. <value>0 17 17 * * ?</value> 26. </property> 27. </bean> 28. 29. <!--管理触发器的总设置,管理我们的触发器列表,可以在bean的list中放置多个触发器。 30. --> 31. <bean autowire="no" 32. class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 33. <property name="triggers"> 34. <list> 35. <ref local="cronTrigger" /> 36. </list> 37. </property> 38. </bean> 39. </beans> 1. package springtimer; 2. 3. ublic class TestQuarz { 4. 5. public void sayHello() { 6. System.out.println("开奖! "); 7. } 1. public class TestHello { 2. 3. public static void main(String[] args) { 4. // 只要加载配置文件就可以了, 5. ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); 6. System.out.println("*****完毕******"); 7. } Ok自已可以设置sping-config文件中<value>0 17 17 * * ?</value>的调度表达式,可以测试啦! 6, 下面简单的介绍一下cron expressions(调度表达式) 格式列表: Java代码 1. Field Name | Mandatory? | Allowed Values | Allowed Special Characters 2. Seconds | YES | 0-59 | , - * / 3. Minutes | YES | 0-59 | , - * / 4. Hours | YES | 0-23 | , - * / 5. Day of month | YES | 1-31 | , - * ? / L W C 6. Month | YES | 1-12 or JAN-DEC | , - * / 7. Day of week | YES | 1-7 or SUN-SAT | , - * ? / L C # 8. Year | NO | empty, 1970-2099 | , - * / 至于详细的解释你一看例子就会明白 例子列表如下: 表达式 解释 0 0 12 * * ? 在每天中午12:00触发 0 15 10 ? * * 每天上午10:15 触发 0 15 10 * * ? 每天上午10:15 触发 0 15 10 * * ? * 每天上午10:15 触发 0 15 10 * * ? 2005 在2005年中的每天上午10:15 触发 0 * 14 * * ? 每天在下午2:00至2:59之间每分钟触发一次 0 0/5 14 * * ? 每天在下午2:00至2:59之间每5分钟触发一次 0 0/5 14,18 * * ? 每天在下午2:00至2:59和6:00至6:59之间的每5分钟触发一次 0 0-5 14 * * ? 每天在下午2:00至2:05之间每分钟触发一次 0 10,44 14 ? 3 WED 每三月份的星期三在下午2:00和2:44时触发 0 15 10 ? * MON-FRI 从星期一至星期五的每天上午10:15触发 0 15 10 15 * ? 在每个月的每15天的上午10:15触发 0 15 10 L * ? 在每个月的最后一天的上午10:15触发 0 15 10 ? * 6L 在每个月的最后一个星期五的上午10:15触发 0 15 10 ? * 6L 2002-2005 在2002, 2003, 2004 and 2005年的每个月的最后一个星期五的上午10:15触发 0 15 10 ? * 6#3 在每个月的第三个星期五的上午10:15触发 0 0 12 1/5 * ? 从每月的第一天起每过5天的中午12:00时触发 0 11 11 11 11 ? 在每个11月11日的上午11:11时触发. 过滤器问题 多线程同步问题 struts2【短路验证】 visitor验证的short-circuit 新手提问,请帮忙 Spring的依赖注入的问题 hibernate save和update方法不起作用 一个朋友翻译的文章,希望大家可以看看。About Spring Web Flow 工作流的问题??? EJB入门级问题,求解。--早答早结单。 java 类似tostring 的效果 求教FTP下载问题 JBOSS怎么修改端口号
还有些个类继承TimerTask,重写run()方法
以Timer为例
Date d = 获取开奖时间
java.util.Timer timer = new java.util.Timer();
timer.schedule(new TimerTask(){
public void run() {
//调用开奖业务
}
}, d.getTime()-System.currentTimeMillis()); //设置延时(开奖时间-当前时间)
2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" >
3. <beans>
4. <!—要调度的对象-->
5. <bean id="testQuarz" class="springtimer.TestQuarz" />
6.
7. <!--定义定时执行testQuarz 这个bean中的sayHello()方法-->
8. <bean id="helloworldTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
9. <property name="targetObject">
10. <ref bean="testQuarz" />
11. </property>
12. <property name="targetMethod">
13. <value>sayHello</value>
14. </property>
15. </bean>
16.
17. <!--触发器的bean的设置,在这里我们设置了我们要触发的jobDetail是哪个。这里我们定义了要触发的jobDetail是helloworldTask,即触发器去触发哪个bean..并且我们还定义了触发的时间:每天5:17pm-->
18. <bean id="cronTrigger"
19. class="org.springframework.scheduling.quartz.CronTriggerBean">
20. <property name="jobDetail">
21. <ref bean=" helloworldTask " />
22. </property>
23. <property name="cronExpression">
24. <!-- 关键在配置此表达式 -->
25. <value>0 17 17 * * ?</value>
26. </property>
27. </bean>
28.
29. <!--管理触发器的总设置,管理我们的触发器列表,可以在bean的list中放置多个触发器。
30. -->
31. <bean autowire="no"
32. class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
33. <property name="triggers">
34. <list>
35. <ref local="cronTrigger" />
36. </list>
37. </property>
38. </bean>
39. </beans> 1. package springtimer;
2.
3. ublic class TestQuarz {
4.
5. public void sayHello() {
6. System.out.println("开奖! ");
7. }
1. public class TestHello {
2.
3. public static void main(String[] args) {
4. // 只要加载配置文件就可以了,
5. ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
6. System.out.println("*****完毕******");
7. }
Ok自已可以设置sping-config文件中<value>0 17 17 * * ?</value>的调度表达式,可以测试啦! 6, 下面简单的介绍一下cron expressions(调度表达式) 格式列表:
Java代码
1. Field Name | Mandatory? | Allowed Values | Allowed Special Characters
2. Seconds | YES | 0-59 | , - * /
3. Minutes | YES | 0-59 | , - * /
4. Hours | YES | 0-23 | , - * /
5. Day of month | YES | 1-31 | , - * ? / L W C
6. Month | YES | 1-12 or JAN-DEC | , - * /
7. Day of week | YES | 1-7 or SUN-SAT | , - * ? / L C #
8. Year | NO | empty, 1970-2099 | , - * /
至于详细的解释你一看例子就会明白 例子列表如下:
表达式 解释
0 0 12 * * ? 在每天中午12:00触发
0 15 10 ? * * 每天上午10:15 触发
0 15 10 * * ? 每天上午10:15 触发
0 15 10 * * ? * 每天上午10:15 触发
0 15 10 * * ? 2005 在2005年中的每天上午10:15 触发
0 * 14 * * ? 每天在下午2:00至2:59之间每分钟触发一次
0 0/5 14 * * ? 每天在下午2:00至2:59之间每5分钟触发一次
0 0/5 14,18 * * ? 每天在下午2:00至2:59和6:00至6:59之间的每5分钟触发一次
0 0-5 14 * * ? 每天在下午2:00至2:05之间每分钟触发一次
0 10,44 14 ? 3 WED 每三月份的星期三在下午2:00和2:44时触发
0 15 10 ? * MON-FRI 从星期一至星期五的每天上午10:15触发
0 15 10 15 * ? 在每个月的每15天的上午10:15触发
0 15 10 L * ? 在每个月的最后一天的上午10:15触发
0 15 10 ? * 6L 在每个月的最后一个星期五的上午10:15触发
0 15 10 ? * 6L 2002-2005 在2002, 2003, 2004 and 2005年的每个月的最后一个星期五的上午10:15触发
0 15 10 ? * 6#3 在每个月的第三个星期五的上午10:15触发
0 0 12 1/5 * ? 从每月的第一天起每过5天的中午12:00时触发
0 11 11 11 11 ? 在每个11月11日的上午11:11时触发.