使用定时器:@Scheduled(cron="0 0/59 * * * ? ")的步骤:
1、在spring-context.xml中加入:
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
再加入定时的:
<task:annotation-driven />
2、在代码的前面加入@Scheduled(cron="0 0/59 * * * ? ")的注解;
package com.yitianjian.ops.task;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.yitianjian.common.domain.ReleaseProductStatistics;
import com.yitianjian.common.domain.User;
import com.yitianjian.common.service.ReleaseProductService;
import com.yitianjian.common.service.ReleaseProductStatisticsService;
import com.yitianjian.common.service.UserService;
@Component
public class UpdateProductReleaseTask {
@Autowired
private UserService userService;
@Autowired
private ReleaseProductService releaseProductService;
@Autowired
private ReleaseProductStatisticsService releaseProductStatisticsService;
/**每隔1小时执行一次*/
@Scheduled(cron="0 0/59 * * * ? ")
public void doTask(){
List<User> userList = userService.queryAllUser();
for(User user: userList){
ReleaseProductStatistics rps = new ReleaseProductStatistics();
rps.setId(UUID.randomUUID().toString());
rps.setUserName(user.getUserName());
rps.setSuccessCount(releaseProductService.queryPublishedProductsCount(user));
rps.setFailureCount(releaseProductService.queryFailureProductsCount(user));
rps.setCreatedOn(new Date());
releaseProductStatisticsService.insert(rps);
}
}
}
相关文章
- Ruby使用gets的错误:gets得到的有'\n',需要使用chomp去掉
- python 爬虫1 Urllib库的基本使用
- 16.1 用auth0服务 实现用登录和管理 使用auth版本的2个大坑。
- 关于ajax在微信智能客服管理端的使用
- GS使用HTTPS登录的设置过程
- 使用kindeditor获取不到富文本框中的值
- js中setTimeout和clearTimeout的使用
- 使用vue的mixins混入实现对正在编辑的页面离开时提示
- 0045 Spring中使用DataSourceTransactionManager进行事务管理的xml配置
- Spring中使用DataSourceTransactionManager进行事务管理的xml配置