下面是簡單的定時任務,其中:
(一)和(二)都是關于查詢id與name的關系的map集合;
(三)是關于查詢id與user對象的關系的map集合;
<<有關屬性介紹均轉載自Pagegle部落客的文章特此說明>>
@Scheduled()中的屬性介紹說明
① 注解@Scheduled 可以作為一個觸發源添加到一個方法中,例如,以下的方法将以一個固定延遲(fixedDelay)時間5秒鐘調用一次執行,這個周期是以上一個調用任務的完成時間為基準,在上一個任務完成之後,5s後再次執行:
@Scheduled(fixedDelay=5000)
public void doSomething() {
// 定時的去執行的方法
}
② 如果需要以固定速率執行,隻要将注解中指定的屬性名稱改成(fixedRate)即可,以下方法将以一個固定速率5s來調用一次執行,這個周期是以上一個任務開始時間為基準,從上一任務開始執行後5s再次調用:
@Scheduled(fixedRate=5000)
public void doSomething() {
// 定時的去執行的方法
}
③ 對于固定延遲和固定速率的任務,可以指定一個"初始延遲"表示該方法在第一被調用執行之前等待的毫秒數:
@Scheduled(initialDelay=5000, fixedRate=5000)
public void doSomething() {
// 初始延遲5s,執行完後每隔5s執行一次
}
④ 如果簡單的定期排程不能滿足,那麼cron表達式提供了可能。例如,下面的方法将隻會在工作日執行:
@Scheduled(cron="*/5 * * * * MON-FRI")
public void doSomething() {
// 隻會在工作日執行的方法
}
以下小案例,是定時掃描查詢一些資料并傳回相應的Map集合:
@Service
public class TimingTask {
public final static String MY_RULE_OF_CANSHU = "thesomestrings";
public final static long TEN_MINUTES = 10 * 60 * 1000;
public final static long ONE_MINUTES = 60 * 1000;
public static final ConcurrentMap<String, String> ID_NAME_MAPPING = new ConcurrentHashMap<>();
public static final ConcurrentMap<Long, UserInfo> ID_USER_MAPPING = new ConcurrentHashMap<>();
@Autowired
private UserMapper userMapper;
/**
* 定時任務,掃描資料庫表中的id與name的對應關系,同步到ID_NAME_MAPPING供查詢。
* fixedDelay = TEN_MINUTES 延遲十分鐘 == public final static long TEN_MINUTES = 10 * 60 * 1000;
*/
@Scheduled(fixedDelay = TEN_MINUTES)
public void scheduleUserInfo() {
//根據規則參數'MY_RULE_OF_CANSHU'查詢相應的user使用者
UserInfo user = userDao.selectUserIfonByRule(MY_RULE_OF_CANSHU);
//根據使用者id查詢使用者資訊集合
List<UserInfo> userInfoList = userMapper.selectUserInfoListById(user.getId());
//對userInfoList進行判空
if (CollectionUtils.isNotEmpty(userInfoList)) {
//userInfoList非空,通過for循環依次将list集合資訊放入到map集合'ID_NAME_MAPPING'中
for (UserInfo user : userInfoList) {
ID_NAME_MAPPING.put(user.getId(), user.getName());
}
}
}
/**
* 定時任務,掃描user表中的id與name的對應關系,同步到ID__NAME_MAPPING供查詢。
* fixedDelay = ONE_MINUTES 延遲一分鐘 == public final static long ONE_MINUTES = 60 * 1000;
*/
@Scheduled(fixedDelay = ONE_MINUTES)
public void scheduleUserInfo() {
//new一個example對象
UserInfoExample example = new UserInfoExample();
//添加條件 example對象的enabled屬性為true
example.createCriteria().andEnabledEqualTo(true);
List<UserInfo> userInfoList = userMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(userInfoList)) {
//list集合非空,通過stream流将集合put進Map集合'ID_NAME_MAPPING'==(id,name)
ID_NAME_MAPPING.putAll(userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, UserInfo::getName)));
}
}
/**
* 定時任務,掃描資料庫表中的id與user對象的對應關系,同步到ID_USER_MAPPING供查詢。
* fixedDelay = ONE_MINUTES 延遲一分鐘 == public final static long ONE_MINUTES = 60 * 1000;
*/
@Scheduled(fixedDelay = ONE_MINUTES)
public void scheduleTemplateSceneMapping() {
//new一個example對象
UserInfoExample example = new UserInfoExample();
List<UserInfo> userInfoList = userMapper.selectByExample(example);
//判斷list集合是否"非空"
if (CollectionUtils.isNotEmpty(userInfoList)) {
//list集合非空,通過stream流将集合put進Map集合'ID_USER_MAPPING'==(id,user對象)
ID_USER_MAPPING.putAll(userInfoList .stream().collect(Collectors.toMap(UserInfo::getId, x -> x)));
}
}
}