在项目中经常会用到执行定时任务,下面的两种是我在做项目的时候用到的两种
(1)spring task
在spring.xml中beans xmlns添加
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
<task:scheduled-tasks> <task:scheduled ref="timeTaskServiceImpl" method="task" cron="0 * 19 * * ?"/> </task:scheduled-tasks>
timeTaskServiceImpl 为你要执行的类,cron来定义您执行时间
(2)java自带的类 ScheduledExecutorService
首先我们先建一个工具类commontimer
package com.wonder.Util; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; /** * 通用定时任务 */ public class CommonTimer { private final static ScheduledExecutorService scheduler; static { scheduler = Executors.newSingleThreadScheduledExecutor(); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { // TODO Auto-generated method stub scheduler.shutdown(); } }); } private static CommonTimer instance = new CommonTimer(); private CommonTimer() { } public static CommonTimer getInstance() { return instance; } /** * 创建并执行在给定延迟后启用的 ScheduledFuture * * @param command * @param delay 秒 * @return */ public ScheduledFuture<?> scheduleWithSeconds(Runnable command, long delay) { return scheduler.schedule(command, delay, TimeUnit.SECONDS); } /** * 创建并执行在给定延迟后启用的 ScheduledFuture * * @param command * @param delay 毫秒 * @return */ public ScheduledFuture<?> scheduleWithMilliSeconds(Runnable command, long delay) { return scheduler.schedule(command, delay, TimeUnit.MILLISECONDS); } /** * 创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在 * initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。 * * @param command * @param delay 秒 * @param period 秒 * @return */ public ScheduledFuture<?> scheduleAtFixedRateWithSeconds(Runnable command, long delay, long period) { return scheduler.scheduleAtFixedRate(command, delay, period, TimeUnit.SECONDS); } /** * 创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在 * initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。 * * @param command * @param delay 毫秒 * @param period 毫秒 * @return */ public ScheduledFuture<?> scheduleAtFixedRateWithMilliSeconds(Runnable command, long delay, long period) { return scheduler.scheduleAtFixedRate(command, delay, period, TimeUnit.MILLISECONDS); } }
里面封装了几个定时执行的方法
然后建一个TimeTaskListener
import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; /** * Created by Guozhijie on 2016/9/20. */ @Component("timeTaskListener") public class TimeTaskListener implements ApplicationListener<ContextRefreshedEvent> { @Autowired TimeTaskService timeTaskService; @Override public void onApplicationEvent(ContextRefreshedEvent e){ CommonTimer.getInstance().scheduleAtFixedRateWithSeconds(timeTaskService,1,60*1); } }
实现了 spring的 applicationListener ,即为每次在启动项目的时候,在各个service类加载完成之后,开始执行
多说一句,applicationListener 这个类,很适合 在项目启动适合将一些缓存的数据加载出来
附:TimeTaskServiceImpl类
package com.wonder.provider; import com.wonder.exception.ServiceException; import com.wonder.service.TimeTaskService; import org.springframework.stereotype.Service; /** * Created by Guozhijie on 2016/9/20. */ @Service public class TimeTaskServiceImpl implements TimeTaskService, Runnable { public void task() throws ServiceException { try{ System.out.println("***********************111111"); }catch (Exception e){ throw e; } } public void run(){ task(); } }
相关推荐
java定时任务,每天定时执行任务,每天到这个时间点都会执行
java定时任务,每天定时执行任务.java定时任务,每天定时执行任务.
JAVA项目服务器启动时自启动指定的Servlet,并定时执行任务。 配置关键在web.xml和servlet文件中的init操作
java 定时执行任务
通过配置数据库连接信息和要执行的sql语句,可实现定时执行多个sql语句。 所要执行的语句只能是写死的,可支持sqlserver mysql oracle。 配置说明: config/sys.properties 中指定数据库类型及连接信息,执行间隔...
只需要在web.xml文件中添加监听器listener,然后加入附件中的文件,并将lstener指向所添加的文件TimerTaskListener,定时任务的研发便可轻松搞定!
spring+quartz整合,动态管理定时任务,可对正在进行的任务操作增删改查,可配合数据库进行操作
介绍了Java定时执行任务,给出了如何使用ServletContextListener实现定时运行java某个方法的例子,简单易用。
使用quartz实现的java 定时任务,通过写cron表达式,让任务在制定时刻触发执行。
NULL 博文链接:https://penghuaiyi.iteye.com/blog/353669
java每天实现定点执行任务java每天实现定点执行任务java每天实现定点执行任务java每天实现定点执行任务java每天实现定点执行任务java每天实现定点执行任务java每天实现定点执行任务
Quartz--JAVA定时任务\Java应用:Java调度任务和Spring Quartz (1)
用java写的任务定时器,设置想设的时间,选择可执行文件或者txt,word,时间一到便会自动运行。现在有源码。
java定时任务是指在java程序中,使用Timer和TimerTask来实现的定时执行任务的机制。java提供了多种方式来实现定时任务,包括使用java.util.Timer和java.util.TimerTask类、使用java.lang.Thread类的sleep方法、使用...
在实际项目应用中经常会用到定时任务,可以通过quartz和spring的简单配置即可完成,但如果要改变任务的执行时间、频率,废弃任务等就需要改变配置甚至代码需要重启服务器,这里介绍一下如何通过quartz与spring的组合...
java定时执行多任务和quartz定时执行多任务
Java语言 适用于定时执行任务场景
使用Java Swing写的一个小程序,用来定时关机、定时重启、定时执行某一个应用程序。执行时调用cmd命令执行的。其他的,就没什么了,最后用exe4j把jar打包成了exe文件,在本地测能用,不知道其他地方是否能用,内部有...
Spring+Quartz 从数据库中获取定时任务和定时时间,动态实现对定时任务的增删改查,部署到tomcat即可看到定时任务执行效果。本人亲测,可用!