spring 定时任务

1、定时任务 服务启动5秒后启动定时任务


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

<task:annotation-driven />

<bean id="loadPicJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="reloadPicService" />
<property name="targetMethod" value="startUpLoad" />
</bean>

<bean id="loadPicTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="loadPicJob" />
<!-- 这里表示只运行一次 -->
<property name="repeatCount" value="0" />
</bean>

<!-- 配置在系统启动5秒后,通过PHP接口加载图片信息到缓存中 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="startupDelay" value="5"/>
<property name="triggers">
<list>
<ref local="loadPicTrigger"/>
</list>
</property>
</bean>

</beans>



2、调用的相关service

@Component
public class ReloadPicService {

public static final String CACHE_LOCK_KEY = "pinge:reloadpic:lock:key";

// 从PHP接口load数据每页取回的条数
public static final int LOAD_PAGE_SIZE = 1000;

// 放入缓存的图片信息过期时间(秒)
public static final int PIC_INFO_CACHE_EXPIRE = 29 * 24 * 60 * 60;

// 获取多长时间以前的图片更新信息(毫秒),默认1小时10分钟
public static final long PIC_TIME_UPDATE = (1 * 3600 + 10 * 60) * 1000;

private static Log log = LogFactory.getLog(ReloadPicService.class);

@Autowired
private PicInfoServiceImplNoCache serviceNoCache;

@Autowired
private MemcachedClient cacheClient;

public void startUpLoad() {
reloadPic();
}

/**
* 通过PHP接口获取更新的图片信息,更新缓存<br/>
* 每小时整点运行
*/
@Scheduled(cron = "0 0 * * * ?")
public void updatePicInfo() {

Date date = new Date(System.currentTimeMillis() - PIC_TIME_UPDATE);
List<Picture> ret = serviceNoCache.getUpdatePicInfo(date);
int size = 0;
if (null != ret && ret.size() > 0) {
size = ret.size();
}
log.info("从[" + date + "]到目前,共有[" + size + "]条图片信息更新");
try {
addToCache(ret);
} catch (Exception e) {
log.error("", e);
}
}

/**
* 通过PHP接口加载全部图片信息到缓存<br/>
* 定时运行,或者通过暴露的url主动加载<br/>
* 每天凌晨4点运行一次
*/
@Scheduled(cron = "0 0 4 * * ?")
public void reloadPic() {

try {
/**
* 首先获取分布锁, 避免多个实例同时加载信息到缓存的问题
*/
boolean getLock = cacheClient.add(CACHE_LOCK_KEY, 30, "ok");
if (getLock) {
log.info("========开始从PHP接口加载数据==========");
PagedInfo<Picture> pg = serviceNoCache.getPicInfoByCategoryId(0, 1, LOAD_PAGE_SIZE);
int pageTotal = pg.getPageTotal();
addToCache(pg.getRsList());

for (int i = 2; i <= pageTotal; i++) {
pg = serviceNoCache.getPicInfoByCategoryId(0, i, LOAD_PAGE_SIZE);
addToCache(pg.getRsList());
log.info("从PHP接口加载数据" + (pg.getRsList() == null ? 0 : pg.getRsList().size()) + "条");
}

log.info("========从PHP接口加载数据完毕==========");
}
} catch (Exception e) {
log.error("", e);
} finally {
try {
cacheClient.delete(CACHE_LOCK_KEY);
} catch (Exception e) {
log.error("", e);
}
}
}

private void addToCache(List<Picture> list) throws Exception {
if (null != list) {
for (Picture p : list) {
cacheClient.setWithNoReply(CACHE_KEY_PINGE_PIC + p.getPicId(), PIC_INFO_CACHE_EXPIRE, p.getPicUrl());
}
}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值