users@glassfish.java.net

Stumped by ServletContext and ServletContextListener behavior

From: <glassfish_at_javadesktop.org>
Date: Wed, 11 Mar 2009 13:02:54 PDT

Hi guys,

I am trying to create a service that our other servlets will use. Its supposed to set itself up on ContextInitialized and clean up on ContextDestroyed.

My problem is that a key attribute I store in the ServletContext and need toa ccess for celanup is VANISHING from the attributes before ContextDestroyed get called :(

I am *positive* I am not resetting it myself. Where is it going and why???

This has been immensely frustrating to try to debug.

The code is below if anyone wants to look at it

jk

=====================================

[code]
public class TimerManager implements ServletContextListener{
    static final private Logger logger = Logger.getLogger(TimerManager.class.getName());
    static final private String TIMERLIST="com.rebelmonkey.TIMERLIST";

    public void contextInitialized(ServletContextEvent sce) {
        logger.info("Initializing Timer Manager");
        setTimerList(sce.getServletContext(),
                new LinkedList<Timer>());
    }

    public void contextDestroyed(ServletContextEvent sce) {
        logger.info("Closing Timer Manager");
        List<Timer> timerList = getTimerList(sce.getServletContext());
        for(Timer t : timerList){
            logger.info("Canceling timer "+t);
            t.cancel();
        }
        timerList.clear();
    }

    static Timer getTimer(ServletContext ctxt){
        Timer timer = new Timer();
        getTimerList(ctxt).add(timer);
        return timer;
    }

    static void disposeTimer(ServletContext ctxt, Timer timer){
        getTimerList(ctxt).remove(timer);
        timer.cancel();
    }

    private static List<Timer> getTimerList(ServletContext servletContext) {
        List<Timer> timerList = (List<Timer>) servletContext.getAttribute(TIMERLIST);
        logger.info("Retreived attribute "+TIMERLIST+" of value "+timerList);
        return timerList;
    }

    private static void setTimerList(ServletContext servletContext, List<Timer> list) {
        logger.info("Setting attribute "+TIMERLIST+" to "+list.toString());
        servletContext.setAttribute(TIMERLIST, list);
    }

}
[/code]
[Message sent by forum member 'jeffpk' (jeffpk)]

http://forums.java.net/jive/thread.jspa?messageID=336468