使用timer从servletContext中删除会话

时间:2017-06-21 10:10:49

标签: session servlets timer java-ee-6 servlet-3.0

我需要从我的所有模块中删除servletContext的会话,因此我创建了一个计时器并将getServlet上下文作为参数传递给TimerConnector的对象 我从会话列表中删除会话但我不知道如何将新列表设置为getSessionContext或如何从getServletContext类中的TimerConnector删除属性

public class TimerConnector {
static TimerConnector instance;
public static synchronized TimerConnector getInstance() throws Exception {

    if (instance == null) {
        instance = new TimerConnector();
    }

    return instance;

}
protected TimerConnector(){
    getSessioContextForDestroy();
}
public TimerConnector() {

}
private TimerScheduler scheduler;
public void getSessioContextForDestroy() {
    // if scheduler is not init, try first.
    if (scheduler == null) {
        scheduler = TimerScheduler.getInstance();
    }   
}
}

我的classScheuler:

public class TimerScheduler {
private final static Logger LOGGER = Logger.getLogger(DctmConnectorScheduler.class) ;

/** the sole instance */
private static TimerScheduler instance;

/**
 * The sole function to get an instance of the class
 */

public static synchronized TimerScheduler getInstance() {

    if (instance == null) {
        instance = new TimerScheduler();
    }

    return instance;

}

/** the flag */
private boolean isSchedulerStarted;

/** the timer for the connection tester scheduler */
private Timer timer;
protected TimerScheduler() {
    startScheduler();
}
private void startScheduler() {

    if (isSchedulerStarted) {
        return;
    }

    try {
        // default values
        int delay = 600000;
        int period = 600000;
        timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                //Do sameting.............

            } // end run()

        }, delay, period);

    } catch (final Exception ex) {
        LOGGER.error(ex.getMessage(), ex);
    }

    // trace in PROD
    LOGGER.error("*** Scheduler started");

    refreshDctmHashtables();

    isSchedulerStarted = true;

}
}

0 个答案:

没有答案