作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个 Spring-MVC 应用程序,在该应用程序中我会定期要求让 onlineUsers 将它们保存在数据库中。我在网上查了一下,我可以使用Spring中的@Scheduled来完成这个任务。我写了一个方法,但它似乎不会自动触发。我没有错误日志,因为该方法不会自动触发,但程序可以正常编译。我只是在控制台中看不到输出。你能帮忙的话,我会很高兴。非常感谢。
这是我的代码:
@Scheduled(fixedRate = 5000)
public void listUsers(){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
List<Object> principals = sessionRegistry.getAllPrincipals();
List<Person> usersInfoList = new ArrayList<>();
for (Object principal: principals) {
if (principal instanceof Person) {
for(SessionInformation sess :sessionRegistry.getAllSessions(principal, false)){
if(!sess.isExpired()){
usersInfoList.add((Person) sess.getPrincipal());
System.out.println("Logged in users are "+((Person) sess.getPrincipal()).getUsername());
}
}
}
}
}
我还在 security-application-context 中的 session 管理中进行了更改,以获得如下未过期的 session :
<security:session-management>
<security:concurrency-control session-registry-ref="sessionRegistry" max-sessions="1" expired-url="/login"/>
</security:session-management>
<bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl" />
任何帮助都会很好。非常感谢...:-)
编辑
sessionRegistry.getAllPrincipals() 返回:
Session registry all sessions is [org.springframework.security.core.userdetails.User@dde5c0af: Username: user@email.de; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_USER]
它没有输入我希望 session 不过期的代码。
@Scheduled(fixedRate = 5000)
// it does not enter in below for loop.
for(SessionInformation sess :sessionRegistry.getAllSessions(principal, false)){
System.out.println("Logged in users are "+((Person) sess.getPrincipal()).getUsername());
}
知道可能出了什么问题吗?
最佳答案
您需要启用Schedluing,因为默认情况下spring不启用调度支持。您可以使用 @EnableScheduling
来实现,它启用 Spring 的计划任务执行功能,类似于 Spring 的 XML 命名空间中的功能
关于java - Spring MVC : Scheduled method not autofiring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29342692/
我正在开发一个 Spring-MVC 应用程序,在该应用程序中我会定期要求让 onlineUsers 将它们保存在数据库中。我在网上查了一下,我可以使用Spring中的@Scheduled来完成这个任
我是一名优秀的程序员,十分优秀!