使用定时器:@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);
}
}
}
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);
}
}
}