欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【实用】SSM定时器使用详解步骤

程序员文章站 2022-03-13 17:31:36
...
做项目中经常会需要使用定时到任务,以下是我项目中运用到的,仅供参考
//server层 定时器清除累计抽奖次数
	public void updateLuckDrawNum()  throws Exception;
//impl层  定时器清除累计抽奖次数
	@Override
	public void updateLuckDrawNum() throws Exception {
		Date date = new Date();
    	SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
    	String dateTime = format.format(date);
    	System.out.println("我是定时器,正在运行中="+dateTime);
		
	}

//application-mvc.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:websocket="http://www.springframework.org/schema/websocket"  
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/mvc 
	http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd	
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/websocket 
	http://www.springframework.org/schema/websocket/spring-websocket.xsd">


 <!-- 定时任务  清除累计抽奖次数 -->
	<bean id="SpringUpdataQuestion" class="com.douples.facade.guessFacade.impl.GuessFacadeImpl" /> 
	<bean id="SpringUpdataQuestionMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
			<ref bean="SpringUpdataQuestion" />
		</property>
		<property name="targetMethod"> 
			<value>updateLuckDrawNum</value>
		</property>
	</bean>

	<!-- ======================== 调度触发器 ======================== -->
	
	<bean id="UpdataQuestionBean"
		class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="SpringUpdataQuestionMethod"></property>
		<property name="cronExpression" value="0/10 * * * * ?"></property>
	</bean>

	<!-- ======================== 调度工厂 ======================== -->
	<bean id="SpringJobSchedulerFactoryBean"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="UpdataQuestionBean"/>
			</list>
		</property>
	</bean> </beans>

效果如下:

【实用】SSM定时器使用详解步骤