Spring ScheduledTimerTask 定时任务执行

1、写好JAVA类TimerTaskTest 必须继承TimerTask

package spring;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Date;

import java.util.TimerTask;

import org.springframework.beans.BeansException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TimerTaskTest extends TimerTask {

public static void main(String[] args) throws IOException {

try {

try {

new ClassPathXmlApplicationContext(new String[] {"beans-config.xml"});

} catch (BeansException e) {

e.printStackTrace();

}

} catch (Exception e) {

e.printStackTrace();

}

System.out.println("启动 Task..");

System.out.println("请输入 exit 关闭 Task: ");

BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));

while(true) {

if(reader.readLine().equals("exit")) { System.exit(0);}

}

}

@Override

public void run() {

System.out.println(new Date().toLocaleString()+">>>>>>>>>>正在执行定时任务");

}

}

2、配置Spring 的 xml文件

<?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:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd ">

<bean id="demoTask" class="spring.TimerTaskTest"/>

<bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">

<property name="timerTask">

<ref bean="demoTask"/>

</property>

<property name="period">

<value>60000</value>

</property>

<property name="delay">

<value>10000</value>

</property>

</bean>

<bean id="timerFactoryBean" class="org.springframework.scheduling.timer.TimerFactoryBean">

<property name="scheduledTimerTasks">

<list>

<ref bean="scheduledTimerTask"/>

</list>

</property>

</bean>

</beans>

3、在上面写的JAVA类中的main方法里面,运行即可执行定时任务

备注:项目需要一些jar包,欢迎联系我的扣扣(1051479609)问我要。

时间: 2024-08-08 09:33:39

Spring ScheduledTimerTask 定时任务执行的相关文章

Spring Quartz定时任务不准时执行

1. 前言 也是前段时间工作上遇到过这样的问题:quartz定时任务没有在预期的时间执行.后来研究了下quartz的机制,查明了原因,这里做个记录和分享. 2. 原因解释 先看一下spring quartz的大致机制或者说原理.quartz任务由一个主线程和线程池中的多个具体的工作线程构成. 主线程是QuartzSchedulerThread, 主要负责获取具体的定时任务和该任务执行的时间(比如可以通过cron expression 得到时间),并分发任务给线程池. 具体的任务由线程池中的工作线

Spring 的@Scheduled注解实现定时任务执行和调度

首先要配置我们的spring.xml   ---  即spring的主配置文件(有的项目中叫做applicationContext.xml或context.xml) xmlns 多加下面的内容. [html] view plaincopy xmlns:task="http://www.springframework.org/schema/task" 然后xsi:schemaLocation多加下面的内容. [html] view plaincopy http://www.springf

spring定时任务执行两次的原因与解决方法

spring定时任务,本地执行一次,放到服务器上后,每次执行时会执行两次,原因及解决办法. http://blog.csdn.net/yaobengen/article/details/70312663 spring定时任务执行两次的原因与解决方法

Spring Task定时任务

Spring Task定时任务 1.基于Spring Task的任务调度方法: Spring框架自带的异步执行(TaskExecutor)和任务调度(TaskScheduler)接口. Spring Task官方地址: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html 以下是task任务调度配置:spring-tasks.xml <?xml version="1

spring schedule定时任务(一):注解的方式

我所知道的java定时任务的几种常用方式: 1.spring schedule注解的方式: 2.spring schedule配置文件的方式: 3.java类继承TimerTask: 第一种方式的实现: 1.使用maven创建spring项目,schedule在spring-context.jar的包下边,因此需要导入与之相关的包:同时,我配的是spring web项目,也同时导入了spring-web和spring-webmvc的包,如下: <dependency> <groupId&

Spring Task定时任务的配置和使用详解

spring中使用定时任务 1.基于xml配置文件使用定时任务 首先配置spring开启定时任务 <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:task

Java spring quartz 定时任务

首先,需要导入quartz 的jar包 ① applicationContext.xml <!-- 轮询任务 --> <import resource="classpath:/conf/quartz/ctmanage-schedule.xml" /> ② ctmanage-schedule.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q

Spring - spring task 定时任务

spring task 实现定时任务 由于Spring3.0以后自带spring task定时任务工具,使用方法也非常简单,在Spring的架构下不需要额外引入其他jar包,同时还支持注解和配置文件两种形式. 一.注解方式配置(@Scheduled) 1.编写定时任务实体类 @Component public class SpringTaskJob { /* * cron : 秒,分,时,日,月,星期,年(可选) */ @Scheduled(cron = "0/5 * * * * ?"

spring @Scheduled定时任务使用说明及基本工作原理介绍

使用说明及工作原理: package com.example.spring.async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import com.example.spring.MyLog; /** * 定时任务使用示例 * 1.启动类增加注解 @EnableScheduling * 2.相应类声明为服务 @Servic