javascript
Spring注解方式实现定时器
一、springmvc.xml中添加以下配置
1、beans添加xmlnx:task
xmlns:task="http://www.springframework.org/schema/task"2、xsi:schemaLocation中添加
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd?
3、核心配置
(1)方法一:
<context:annotation-config /> <!-- spring掃描注解的配置(要掃描的包,即定時器所在包) --><context:component-scan base-package="com.qiyuan.listener" /><!-- 開啟這個配置,spring才能識別@Scheduled注解 --> <task:annotation-driven scheduler="qbScheduler" mode="proxy"/> <task:scheduler id="qbScheduler" pool-size="10"/>
說明:理論上只需要加上<task:annotation-driven />這句配置就可以了,這些參數都不是必須的(見方法二)。
(2)方法二
<!-- task任務注解掃描包(定時器開關) --> <task:annotation-driven/> <!-- 用定時器注解 --><!-- 掃描位置是 --> <context:annotation-config/> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <context:component-scan base-package="com.qiyuan.listener"/>?
二、定時任務Java代碼
(1)接口和實現類
package com.qiyuan.listener;public interface Test {public void myTest1(); public void myTest2(); public void myTest3();public void myTest4();}?
package com.qiyuan.listener;import java.text.SimpleDateFormat; import java.util.Date;import javax.annotation.Resource;import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;import com.qiyuan.bean.TRate; import com.qiyuan.service.AnalysticService; import com.qiyuan.service.CenterPurseService; import com.qiyuan.service.OrderService; import com.qiyuan.service.RateService; import com.qiyuan.util.RateUtil;@Component public class TestImp implements Test {@Resourceprivate RateService rateServiceImp;@Resourceprivate CenterPurseService centerPurseServiceImp;@Resourceprivate OrderService orderServiceImp;@Resourceprivate AnalysticService analysticServiceImp;int count1 = 0;int count2 = 0;int count3 = 0;int count4 = 0;@Scheduled(cron="0 */5 * * * ?") //每5分鐘執行一次 @Overridepublic void myTest1() {System.out.println("進入測試定時器1"); ++count1;System.out.println("定時器1:時間=" + new Date() + " 執行了" + count1 + "次"); // 1次//查詢各種幣的匯率TRate tRate=new TRate();tRate.setRate(1);tRate.setYkcrate(RateUtil.YKC());tRate.setBtcrate(RateUtil.BTC());tRate.setLtcrate(RateUtil.LTC());tRate.setEthrate(RateUtil.ETH());tRate.setLastmodify(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));System.out.println("==========匯率"+tRate);//更新匯率表 rateServiceImp.updateRate(tRate);//查詢錢包充值記錄,更新用戶錢包 centerPurseServiceImp.searchPurseChargeRecord();}@Scheduled(cron="0 0 2 * * ?") //每天凌晨一點執行一次 @Overridepublic void myTest2() {System.out.println("進入測試定時器2"); ++count2;System.out.println("定時器2:時間=" + new Date() + " 執行了" + count2 + "次"); // 1次//每天凌晨1點:超過7天已發貨但未收貨的讓系統自動收貨 orderServiceImp.autoAcceptOrder();}@Scheduled(cron="0 0 2 * * ?") //每天凌晨兩點執行一次 @Overridepublic void myTest3() {System.out.println("進入測試定時器3"); ++count3;System.out.println("定時器3:時間=" + new Date() + " 執行了" + count3 + "次"); // 1次//每天凌晨1點統計前一天的數據 analysticServiceImp.insertAnalystic();}@Scheduled(cron = "0 0 3 1 * ?") //每個月1號凌晨三點執行一次public void myTest4(){System.out.println("進入測試定時器4"); ++count4;System.out.println("定時器3:時間=" + new Date() + " 執行了" + count4 + "次"); // 1次//每個月1號凌晨三點清空臨時訂單表的數據 orderServiceImp.deleteAllTempOrder();}}?
三、擴展資料
關于@Scheduled注解,查看源文件中該注解的定義
@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Scheduled { public abstract String cron(); public abstract long fixedDelay(); public abstract long fixedRate(); }參數具體介紹見:
(1)理解 Spring 定時任務的 fixedRate 和 fixedDelay 的區別
(2)
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:task="http://www.springframework.org/schema/task"?xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsd?http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvc?http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/task ?http://www.springframework.org/schema/task/spring-task-4.1.xsd ?"?><!-- 掃描:通知spring容器采用自動掃描機制查找注解的bean --><context:component-scan base-package="com.qiyuan.controller"/>
<!-- 默認加載方法,啟動時加載 (定時器的另外一種方法)--><!-- <bean id="templateAnnotationInit1" class="com.qiyuan.listener.TestTimer1" init-method="showTimer1" /><bean id="templateAnnotationInit2" class="com.qiyuan.listener.TestTimer2" init-method="showTimer2" /><bean id="templateAnnotationInit3" class="com.qiyuan.listener.TestTimer3" init-method="showTimer3" /> --><!-- task任務注解掃描包(定時器開關) --><task:annotation-driven/> <!-- 用定時器注解 -->??<!-- 掃描位置是 --><context:annotation-config/> ?? ? <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>?? ? <context:component-scan base-package="com.qiyuan.listener"/> ?<!--開啟springmvc 注解 --><mvc:annotation-driven> ?<mvc:message-converters>? ? ? ? ? ?<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>? ? ? ?</mvc:message-converters></mvc:annotation-driven><!-- 視圖解析器 --><bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> ? <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> ? <property name="prefix" value="/WEB-INF/jsp/"></property> ? <property name="suffix" value=".jsp"></property></bean><!-- 前臺攔截器 --><mvc:interceptors> ? ? <mvc:interceptor> ? ? ?<!-- 過濾全部請求 --> ? ? ? ? ?<mvc:mapping path="/**"/> ? ? ? ? ?<!-- ?除了此請求 --> ? ? ? ? ?<mvc:exclude-mapping path="/login/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/item/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/analystic/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/members/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/itemMage/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/back_order/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/back_order1/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/logistics/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/product/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/admin/*"/> ? ? ? ? ?<bean class="com.qiyuan.interceptor.MyHandlerInterceptor"></bean> ? ? </mvc:interceptor></mvc:interceptors><!-- 后臺(管理員)攔截器 --><mvc:interceptors> ? ? <mvc:interceptor> ? ? ?<!-- 過濾全部請求 --> ? ? ? ? ?<mvc:mapping path="/**"/> ? ? ? ? ?<!-- ?除了此請求 --> ? ? ? ? ?<mvc:exclude-mapping path="/login/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/centerAddress/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/centerOrder/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/centerPurse/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/centerScore/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/centerTeam/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/item/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/order/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/shopCar/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/userCenter/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/back_order1/*"/> ? ? ? ? ?<bean class="com.qiyuan.interceptor.MyHandlerInterceptor_back"></bean> ? ? </mvc:interceptor></mvc:interceptors><!-- 后臺(供應商)攔截器 --><mvc:interceptors> ? ? <mvc:interceptor> ? ? ?<!-- 過濾全部請求 --> ? ? ? ? ?<mvc:mapping path="/**"/> ? ? ? ? ?<!-- ?除了此請求 --> ? ? ? ? ?<mvc:exclude-mapping path="/login/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/centerAddress/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/analystic/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/centerOrder/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/centerPurse/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/centerScore/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/centerTeam/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/item/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/order/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/shopCar/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/userCenter/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/admin/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/itemMage/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/logistics/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/members/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/back_order/*"/> ? ? ? ? ?<mvc:exclude-mapping path="/product/*"/> ? ? ? ? ?<bean class="com.qiyuan.interceptor.MyHandlerInterceptor_back1"></bean> ? ? </mvc:interceptor></mvc:interceptors><!-- 文件上傳 --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> ? <property name="defaultEncoding" value="UTF-8"></property><property name="maxUploadSize" value="99999999999"></property><property name="resolveLazily" value="true"></property></bean>
</beans>
轉載于:https://www.cnblogs.com/javahr/p/8318573.html
總結
以上是生活随笔為你收集整理的Spring注解方式实现定时器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php查询
- 下一篇: T extends Comparable