I made a blog project, I want to realize that every time users click on the blog, the page views will be automatically updated, I use redis to store data, and every once in a while, the data in redis will be updated to the database, but when I want to update the current login user id to the database, an exception occurs. I was hoping you could help me figure out what the problem is, thank you.
This is my implementation class:
@Componentpublic class UpdateViewCountJob { @Autowired private RedisCache redisCache; @Autowired private ArticleService articleService; @Scheduled(cron = "0/5 * * * * ?") public void updateViewCount(){ //获取redis中的浏览量 Map<String, Integer> viewCountMap = redisCache.getCacheMap("article:viewCount"); List<Article> articles = viewCountMap.entrySet().stream() .map(entry -> new Article(Long.valueOf(entry.getKey()), entry.getValue().longValue())) .collect(Collectors.toList()); Long userId = SecurityUtils.getUserId(); for (Article article : articles) { article.setUpdateBy(userId); } //更新到数据库中 articleService.updateBatchById(articles); }}
This is the class for the auto-fill function I implemented with mybatisplus:
@Componentpublic class MyMetaObjectHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { Long userId = null; userId = SecurityUtils.getUserId(); this.setFieldValByName("createTime", new Date(), metaObject); this.setFieldValByName("createBy",userId , metaObject); this.setFieldValByName("updateTime", new Date(), metaObject); this.setFieldValByName("updateBy", userId, metaObject); } @Override public void updateFill(MetaObject metaObject) { this.setFieldValByName("updateTime", new Date(), metaObject); this.setFieldValByName("updateBy", SecurityUtils.getUserId(), metaObject); }}
The error message is as follows:
2023-08-05 13:42:05.005 ERROR 20388 --- [ scheduling-1] o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled taskjava.lang.NullPointerException: null at com.blog.framework.utils.SecurityUtils.getLoginUser(SecurityUtils.java:13) ~[classes/:na] at com.blog.framework.utils.SecurityUtils.getUserId(SecurityUtils.java:29) ~[classes/:na] at com.blog.job.UpdateViewCountJob.updateViewCount(UpdateViewCountJob.java:32) ~[classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131] at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.3.7.jar:5.3.7] at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.3.7.jar:5.3.7] at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:95) [spring-context-5.3.7.jar:5.3.7] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_131] at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_131] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_131] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_131] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_131] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_131] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]
SecurityUtils class as follow:
public class SecurityUtils { /** * 获取用户 **/ public static LoginUser getLoginUser() { return (LoginUser) getAuthentication().getPrincipal(); } /** * 获取Authentication */ public static Authentication getAuthentication() { return SecurityContextHolder.getContext().getAuthentication(); } public static Boolean isAdmin(){ Long id = getLoginUser().getUser().getId(); return id != null && id.equals(1L); } public static Long getUserId() { return getLoginUser().getUser().getId(); }}
What confused me was that when I commented out the code in MyMetaObjectHandler class that automatically populated the user id there was no error, which is like this:
@Override public void updateFill(MetaObject metaObject) { this.setFieldValByName("updateTime", new Date(), metaObject);// this.setFieldValByName("updateBy", SecurityUtils.getUserId(), metaObject); }
If you can help me solve this problem, I will be very grateful to you!