定时任务简单实例
2015-11-11 19:06
465 查看
1、java util包中自带的Timer及TimerTask作定时任务 public class TimerTaskTestXml { private int delay=0; private long period=0; public void setDelay(int delay) { this.delay = delay; } public void setPeriod(long period) { this.period = period; } public TimerTaskTestXml(int dealy,long period){ this.delay=dealy; this.period=period; this.timeTask(); } public void timeTask(){ Timer timer=new Timer(); timer.schedule(new TimerTask(){ @Override public void run() { queryAlipays(); try{ testfunction(); }catch(Exception ex){ System.out.println(ex.getMessage()); } System.out.println("dssssssss"); } }, delay, period); } public void queryAlipays(){ List<AlipayInfoEntity> alipays=AlipayInfoEntity.findAllAlipayInfoEntitys(); if(alipays!=null){ for(AlipayInfoEntity aie:alipays) System.out.println(aie.getOutTradeNo()+"----------"+aie.getTradeStatus()); } } public void testfunction(){ try{ int i=0; i=i/0; }catch(Exception ex){ ex.printStackTrace(); System.out.println("function aaa is exception"); } } } spring配置文件如下: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <bean id="timerTaskTestXml" class="tv.bgsc.paymentplatform.routine.test.TimerTaskTestXml"> <constructor-arg index="0" value="2000"/> <constructor-arg index="1" value="3000"/> </bean> </beans> 2、spring3.0及以上版本自带的@scheduler注释 @Component public class SpringSchedulerTaskTest { /****** * 每隔1分钟执行 */ @Scheduled(cron="* */1 * * * ?") public void schedulerTask(){ this.queryTransactioins(); } public void queryTransactioins(){ List<TransactionEntity> trans=TransactionEntity.findAllTransactionEntitys(); if(trans!=null){ for(TransactionEntity te:trans) System.out.println(te.getOrderNo()+"----------"+te.getUserNo()); } } } spring配置文件如下: 省略了xml文件:将1中的<bean></bean>部分去掉,换为下面的代码即可。更详细的配置请参考spring参考文档。</span> <task:annotation-driven scheduler="myScheduler" mode="proxy"/> //相当于是启动定时任务 <task:scheduler id="myScheduler" pool-size="10"/> 注意:使用注解时,该类要被扫描到,不然注解无用,就不能执行了。 3、spring与quartz搭配使用 public class SpringQuartzTaskTtest{ /******* * 业务逻辑处理 */ public void doTask(){ this.queryAlipays(); System.out.println("---------------------------"); } public void queryAlipays(){ List<AlipayInfoEntity> alipays=AlipayInfoEntity.findAllAlipayInfoEntitys(); if(alipays!=null){ for(AlipayInfoEntity aie:alipays) System.out.println(aie.getOutTradeNo()+"----------"+aie.getTradeStatus()); } } } spring配置文件如下: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <!-- 线程执行器配置,用于任务注册 --> <bean id="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="10"/> <property name="maxPoolSize" value="100"/> <property name="queueCapacity" value="500"/> </bean> <!-- 定义业务逻辑处理类 --> <bean id="springQuartzTaskTtest" class="tv.bgsc.paymentplatform.routine.test.SpringQuartzTaskTtest"></bean> <!-- 高度业务逻辑 --> <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="springQuartzTaskTtest" /> <property name="targetMethod" value="doTask" /> </bean> <!-- 增加高度触发器 --> <bean id="taskTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="jobDetail"/> <property name="startDelay" value="10000"/> <property name="repeatInterval" value="60000"/> </bean> <!-- <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="jobDetail" /> <property name="cronExpression" value="10 */2 * * * ?" /> </bean> --> <!-- 配置调度工厂 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <!-- <ref bean="cronTrigger"/> --> <ref bean="taskTrigger"/> </list> </property> <property name="taskExecutor" ref="executor"/> </bean> </beans> 注:在这里使用Quartz的Maven依赖如下: <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>1.8.4</version> </dependency> 而我首次使用的是quratz版本为2.1.1时cronExpression及repeatInterval在对应类或超类中没有找到对应的set方法,因此不能像上面那样使用。感兴趣的朋友可以去研究一下,弄出来了,可以贴出来大家分享!
相关文章推荐
- 优化UITableViewCell高度计算的那些事
- UML——活动图,状态图
- 类型对指针运算的影响
- 巴伦电路基础
- [LeetCode] Delete Node in a Linked List
- 51nod 1270 数组的最大代价 (DP_好题)
- 在按一次退出App
- 平行二叉堆和优先队列
- 如何把一个字符串格式化位4位一个空格
- 使用Ant构建项目
- Python + OpenCV边沿检测(Edge Detection)
- google git resource
- iOS 生成规范注释的工具
- iOS 图片比例压缩
- Java语言解决约瑟夫环问题(ArrayList实现)
- Ueditor绝对路径问题
- 文件结尾判断feof , EOF
- CSS 属性的默认值
- 用java进行简单的万年历编写
- 二叉树