定时任务demo
来源:互联网 发布:数据分析师证 编辑:程序博客网 时间:2024/04/30 12:38
首先你需要定义一个bean类,和需要执行的方法(需要引入quartz-1.8.6.jar和org.springframework.context.support-3.0.2.RELEASE.jar)
public class QuartzTask { public void doSimpleTriggerTask() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("doing simpleTrigger task..." + sdf.format(new Date())); } public void doCronTriggerTask() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("doing cronTrigger task..." + sdf.format(new Date())); }}
然后在spring配置文件中配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 注册一个普通bean --> <bean id="quartzTask" class="cn.itcast.nsfw.complain.QuartzTask"></bean> <!-- 1、制定任务信息信息 --> <bean id="jobDetail1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 设置执行对象 --> <property name="targetObject" ref="quartzTask"></property> <!-- 设置执行对象中对应的执行方法 --> <property name="targetMethod" value="doSimpleTriggerTask"></property> <!-- 是否可以同步执行;不可同步执行 --> <property name="concurrent" value="false"></property> </bean> <bean id="jobDetail2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 设置执行对象 --> <property name="targetObject" ref="quartzTask"></property> <!-- 设置执行对象中对应的执行方法 --> <property name="targetMethod" value="doCronTriggerTask"></property> <!-- 是否可以同步执行;不可同步执行 --> <property name="concurrent" value="false"></property> </bean> <!-- 2、制定任务执行时机(任务执行触发器) --> <bean id="simplerTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <!-- 设置任务详细 --> <property name="jobDetail" ref="jobDetail1"></property> <!-- 设置任务延迟执行时间 ;延迟2秒执行--> <property name="startDelay" value="2000"></property> <!-- 设置任务执行频率;执行频率为每4秒执行一下 --> <property name="repeatInterval" value="2000"></property> </bean> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <!-- 设置任务详细 --> <property name="jobDetail" ref="jobDetail2"></property> <!-- 设置任务执行时机,cron表达式 --> <property name="cronExpression" value="* * * 18c * ?"></property> </bean> <!-- 3、设置调度工厂 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <!-- <ref bean="simplerTrigger"/> --> <ref bean="cronTrigger"/> </list> </property> </bean></beans>
阅读全文
0 0
- 定时任务demo
- ScheduledThreadPoolExecutor 定时任务demo
- mysql定时任务demo
- Spring定时任务的Demo
- spring quartz 定时任务 demo
- oracle的定时任务demo
- Linux定时任务小demo
- 定时任务,quartz的demo
- spring 实现定时任务demo
- spring task 定时任务 注解方式 demo
- asp.net使用Hangfire开发后台定时任务demo
- Quartz定时任务简单demo及与Spring整合
- Quartz定时任务(附demo)(一)
- spring整合quartz定时任务(附demo)(二)
- Spring 整合 Quartz 实现动态定时任务(附demo)
- 定时任务
- 定时任务
- 定时任务
- 垃圾回收机制如何优化程序
- Python3 爬虫
- Intellij idea创建javaWeb以及Servlet简单实现
- VS 动态链接库(.dll)的加载问题
- 【笔记】ViewPager+自定义控件实现的日历控件CalenderView
- 定时任务demo
- Drools 运行时出现 java.lang.RuntimeException: wrong class format 问题解决
- linux-5-压缩打包
- ios 巧用UIButton的UIControlStateSelected属性
- 使用黄金分割比值计算斐波那契数列
- Linux编程:Valgrind在调试中的使用
- windows IO 设备以及打开
- Android SystemProperties设置/取得系统属性
- window安装mysql 5.7解压版