栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Shiro基础组件 SessionValidationScheduler

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Shiro基础组件 SessionValidationScheduler

简介

支持定期校验Session,由DefaultSessionManager调用实现Session校验;

核心方法
boolean isEnabled();


void enableSessionValidation();


void disableSessionValidation();
实现子类
public interface SessionValidationScheduler
    public class ExecutorServiceSessionValidationScheduler implements SessionValidationScheduler, Runnable
ExecutorServiceSessionValidationScheduler 简介

借助ScheduledExecutorService实现定期校验Session;
###核心方法

// Session管理器
ValidatingSessionManager sessionManager;
// 策略执行服务
private ScheduledExecutorService service;
// 间隔时长
private long interval = DefaultSessionManager.DEFAULT_SESSION_VALIDATION_INTERVAL;
// 启用标识
private boolean enabled = false;
// 线程名称前缀
private String threadNamePrefix = "SessionValidationThread-";


public boolean isEnabled() {
    return this.enabled;
}


public void enableSessionValidation() {
    if (this.interval > 0l) {
        // 创建单线程池
        this.service = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {  
            private final AtomicInteger count = new AtomicInteger(1);

            public Thread newThread(Runnable r) {  
                Thread thread = new Thread(r);
                // 设置后台运行
                thread.setDaemon(true);
                // 设置线程名称
                thread.setName(threadNamePrefix + count.getAndIncrement());
                return thread;
            }
        });
        // 启动线程池
        this.service.scheduleAtFixedRate(this, interval, interval, TimeUnit.MILLISECONDS);
    }
    // 设置启用标识
    this.enabled = true;
}


public void run() {
    if (log.isDebugEnabled()) {
        log.debug("Executing session validation...");
    }
    Thread.currentThread().setUncaughtExceptionHandler((t, e) -> {
        log.error("Error while validating the session, the thread will be stopped and session validation disabled", e);
        // 禁用校验
        this.disableSessionValidation();
    });
    long startTime = System.currentTimeMillis();
    try {
        // 校验Session
        this.sessionManager.validateSessions();
    } catch (RuntimeException e) {
        log.error("Error while validating the session", e);
        //we don't stop the thread
    }
    long stopTime = System.currentTimeMillis();
    if (log.isDebugEnabled()) {
        log.debug("Session validation completed successfully in " + (stopTime - startTime) + " milliseconds.");
    }
}


public void disableSessionValidation() {
    if (this.service != null) {
        this.service.shutdownNow();
    }
    this.enabled = false;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/727379.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号