在之前的一篇文章(点击打开链接)介绍过借助quartz.jar实现定时器。但是这种方式太重了,配置繁琐,还需要引入新的包。其实Spring自己有对定时器的支持,借助注解的威力,开发十分简单!效率很高。
首先看java代码如何使用自带timer。
package com.**.chat.core.common;
import javax.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.**.chat.cache.core.impl.CommonCacheServiceImpl;
import com.**.chat.cache.domain.CoreKey;
import com.**.chat.cache.key.KeyHelper;
/**
* 从缓存同步配置
*
* @author Administrator
*
*/
@Component("configSyncTimer")
public class ConfigSyncTimer {
// 默认开启
public static boolean jimiSwitch = true;
@Resource
private CommonCacheServiceImpl dataCommonCacheService;
@Scheduled(fixedRate = 30 * 1000)
private void jimi() {
CoreKey key = KeyHelper.Data.Chat.getJimiShuntSwitch();
boolean open = !dataCommonCacheService.exists(key);
jimiSwitch = open;
}
}
此外还需要配置下xml,配完了,就可以跑了。
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!-- Enables the Spring Task @Scheduled programming model -->
<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor" scheduler="scheduler" />
</beans>
借助@Scheduled注解,轻松实现timer。这个注解还有另外两种使用方式,我们看下它的定义。
public @interface Scheduled {
/**
* A cron-like expression, extending the usual UN*X definition to include
* triggers on the second as well as minute, hour, day of month, month
* and day of week. e.g. <code>"0 * * * * MON-FRI"</code> means once
* per minute on weekdays (at the top of the minute - the 0th second).
* @return an expression that can be parsed to a cron schedule
*/
String cron() default "";
/**
* Execute the annotated method with a fixed period between the end
* of the last invocation and the start of the next.
* @return the delay in milliseconds
*/
long fixedDelay() default -1;
/**
* Execute the annotated method with a fixed period between invocations.
* @return the period in milliseconds
*/
long fixedRate() default -1;
}
cron支持cron表达式类型的timer,支持从配置文件读取cron配置。但是fixedRate和fixedDelay支持硬编码配置。建议大家使用3.2.2版本以上的Spring,这个版本fixedRateString和fixedDelayString支持从配置文件读取配置。
/**
* Execute the annotated method with a fixed period between the end
* of the last invocation and the start of the next.
* @return the delay in milliseconds as a String value, e.g. a placeholder
* @since 3.2.2
*/
String fixedDelayString() default "";
/**
* Execute the annotated method with a fixed period between invocations.
* @return the period in milliseconds as a String value, e.g. a placeholder
* @since 3.2.2
*/
String fixedRateString() default "";
个人十分喜欢Spring自带的timer。尤其是将它应用在从缓存中轮询配置或开关的操作。如果每个操作都去读取redis缓存中的配置,大量的网络调用redis很可能把redis打爆。但是如果每个应用实例仅仅是30秒或十分钟拉取一次缓存配置,那对访问redis的压力的改善将会从很高的数量级直接降低到常量级别。