applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="mesBean" class="cn.xg.spring.Message" abstract="false"
lazy-init="default" autowire="default" dependency-check="default">
<property name="title">
<value>标题</value>
</property>
</bean>
<!-- spring定时器 -->
<!-- 方法一 -->
<!-- 第一步 声明一个定时任务,该类extends java.util.TimerTask -->
<bean id="clock" class="cn.xg.spring.Clock"></bean>
<!-- 第二步 调度定时任务,把声明的定时任务注入进来,并设置定时参数 -->
<bean id="scheduledClock" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask">
<ref bean="clock"></ref>
</property>
<property name="period">
<value>5000</value>
<!--这里是每隔多长时间就进行一次计时任务,单位ms-->
</property>
<property name="delay">
<value>5000</value>
<!--这里是服务启动后延时多少时间,开始计时任务,单位ms-->
</property>
</bean>
<!-- 启动定时任务,如果有多个定时任务,则重复步骤一,二,然后把第二步设置的beany放在下面的list列表中.此方法不能精确几点运行定时任务 -->
<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="scheduledClock"></ref>
</list>
</property>
</bean>
<!-- 方法二 -->
<!-- 第一步 声明一个定时任务,注意不是直接声明,而是声明一个JobDetailBean,通过jobClass属性设置一个定时对象 -->
<bean id="quartzClock" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>cn.xg.spring.QuartzClock</value>
</property>
</bean>
<!-- 第二步 调度定时任务 -->
<!--这种配置与第一种方法效果一样
<bean id="quartzClockTask" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="quartzClock"/>
</property>
<property name="startDelay">
<value>6000</value>
这里是服务启动后延时多少时间,开始计时任务,单位ms
</property>
<property name="repeatInterval">
<value>6000</value>
这里是每隔多长时间就进行一次计时任务,单位ms
</property>
</bean>
-->
<!-- 这种配置可以精确几点执行定时任务 -->
<bean id="cronQuartzClock" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="quartzClock"></ref>
</property>
<property name="cronExpression">
<value>0 52 22 * * ?</value><!--定时在任何月份任何日期(不管星期几)的22点52分0秒 -->
<!-- 一个cron表达式有到少6个(也可能是7个)由空格分隔的时间元素.从左到右,这些元素的定义如下:
1.秒(0-59)
2.分钟(0-59)
3.小时(0-23)
4.月份中的是期(1-31)
5.月份(1-12或SUN-DEC)
6.星期中的日期(1-7或SUN-SAT)
7.年份(1970-2099)
例子:
0 0 10,14,16 * * ? 每天上午10点,下午2点和下午4点
0 0,15,30,45 * 1-10 * ? 每月前10天每隔15分钟
30 0 0 1 1 ? 2012 在2012年1月1日午夜过30秒时
0 0 8-5 ? * MON-FRI 每个工作日的工作时间
- 区间
* 通配符
? 你不想设置那个字段
-->
</property>
</bean>
<!--第三步 启动定时任务,注意这里的ref bean -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronQuartzClock"></ref>
</list>
</property>
</bean>
</beans>
clock.java
import java.util.TimerTask;
public class Clock extends TimerTask{
@Override
public void run() {
System.out.println("clock..!clock....!.......");
}
}
QuartzClock .java
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class QuartzClock extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
System.out.println("QuartzClock..!QuartzClock....!.......");
}
}
SpringTest .java
package cn.xg.spring;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
/**
* @param args
*/
public static Logger log = Logger.getLogger(SpringTest.class);
public static void main(String[] args) {
//第一种写法(加载配置文件)
ApplicationContext ctx = new
ClassPathXmlApplicationContext("applicationContext.xml");
//第二种写法
//ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
//ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//加载多个配置文件
// ApplicationContext ctx = new ClassPathXmlApplicationContext(
//new String[]{"applicationContext.xml","applicationContext2.xml"} );
}
}
所需要的jar包:
spring.jar;quartz-all-1.6.0.jar;commons-collections.jar;可能还要commons-*.jar
分享到:
相关推荐
spring定时器spring定时器spring定时器spring定时器spring定时器
springAop与spring定时器
spring定时器spring定时器spring定时器spring定时器
spring定时器spring定时器spring定时器spring定时器spring定时器spring定时器spring定时器spring定时器spring定时器spring定时器spring定时器spring定时器
java定时器 spring定时器:包括spring定时器的详细配置以及说明 包含所需的jar包
java spring 定时器(精确启动时间) 完整实例 cronExpression 表达式
spring定时器时间配置.时间的设置规则,并有代码予以演示。
spring定时器所需jar包spring定时器所需jar包spring定时器所需jar包
一个spring定时器的一个小文档,详细,
spring 定时器的实力使用方法,使用实例可以参考
使用spring 实现的两种定时任务,使用eclipse直接导入就可使用
JAVA获取当前时间以及JAVA_Spring定时器
spring 定时器 挺好用的哦,希望对大家有所帮助。
spring 定时器 内含TimerTask实现和Quartz实现两种方式
spring定时器。这里有需要的包和applicationContext.xml配置文件
spring定时器Spring定时器的两种实现方式Java的Timer类和OpenSymphony的Quartz。
举个例子吧 可对仓库库存状况定时检查,并执行删除等其他操作
spring定时器实例
并非应用系统中发生的所有事情都是由用户的动作引起的。...在Spring中有两种流行配置:Java的Timer类和OpenSymphony的Quartz来执行调度任务。下面以给商丘做的接口集抄900到中间库的日冻结数据传输为例:
使用方法很简单,部署上去,打开服务器就可以了.不需要任何的操作. 主要看applicationContext.xml的配置就可以