当前位置:   article > 正文

微服务定时执行任务

微服务定时执行任务

需要的注解

@Configuration      //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling   // 2.开启定时任务

@Scheduled(fixedRate = 3000000) 定时 30分钟

  1. package com.czxy.conllonter;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.czxy.Administrator;
  4. import com.czxy.config.BaseResult;
  5. import com.czxy.config.JwtProperTies;
  6. import com.czxy.mapper.UserMapper;
  7. import com.czxy.servise.Userservise;
  8. import com.czxy.untils.CodeUtil;
  9. import com.czxy.untils.JwtUtils;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.scheduling.annotation.EnableScheduling;
  12. import org.springframework.scheduling.annotation.Scheduled;
  13. import org.springframework.stereotype.Component;
  14. import org.springframework.web.bind.annotation.*;
  15. import javax.annotation.Resource;
  16. import javax.servlet.http.HttpServletRequest;
  17. import java.sql.Time;
  18. import java.util.List;
  19. import java.util.Timer;
  20. import java.util.TimerTask;
  21. @RestController
  22. @RequestMapping("/administrator")
  23. @Configuration //1.主要用于标记配置类,兼备Component的效果。
  24. @EnableScheduling // 2.开启定时任务
  25. //@Component
  26. public class UserConlleter {
  27. @Resource
  28. private Userservise userservise;
  29. @Resource
  30. private UserMapper userMapper;
  31. @Resource
  32. private JwtProperTies jwtProperTies;
  33. @Resource
  34. private HttpServletRequest httpServletRequest;
  35. //生成随机校验码用于注册
  36. @PostMapping("Invitationcode/{id}")
  37. private BaseResult<String> getCode(@PathVariable Integer id){
  38. String code = CodeUtil.toSerialCode(id);
  39. //设置权限码
  40. Administrator administrator = userMapper.selectById(id);
  41. administrator.setCode(code);
  42. userservise.updateById(administrator);
  43. return BaseResult.ok("请求成功",code);
  44. }
  45. //30分钟刷新校验码
  46. @Scheduled(fixedRate = 3000000)
  47. @GetMapping("updateCode")
  48. private void updateCode(){
  49. List<Administrator> administratorList = userservise.list();
  50. for (Administrator administrator:administratorList){
  51. //获取code
  52. BaseResult<String> code = getCode(administrator.getAid());
  53. //设置校验码
  54. administrator.setCode(code.getData());
  55. //更新
  56. userservise.updateById(administrator);
  57. }
  58. }
  59. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/190525
推荐阅读
相关标签
  

闽ICP备14008679号