Appropriate use of a ServletContextListener in Java / Apache Tomcat web application

StackOverflow https://stackoverflow.com/questions/1404654

  •  05-07-2019
  •  | 
  •  

Question

Can anyone tell me if it is appropriate to use a ServletContextListener for a particular requirement?

What I have is a web application which runs a particular job every hour to poll a number of RSS feeds and at any time a user can define a new RSS feed for polling.

What I want to avoid is a user adding a feed(s) during a current hourly polling cycle and having this feed(s) included in that cycle.

So what I'm thinking about is using a ServletContextListener with a static boolean variable which records whether a polling cycle is running or not. If an hourly polling cycle is running, a new feed defined by a user will not be included in this cycle; if polling is not running, then the feed can be included in the next hourly run.

Can anyone advise? The only alternatives I see are to use a flag on a database table or to update a value in a properties file (if this can be done).

Thanks

Martin O'Shea.

Was it helpful?

Solution

That sounds like a bad idea. Why not simply use the ServletContext for your web-app to hold a property for the web-app ("polling-cycle-running")? The component in your web-app that kicks off the job sets the property in the context; all interested components (such as your AddRSS action would check that property in the context as a matter of course.

servletCtx.setAttribute(POLLING_CYCLE_FLAG_ATTR, true);

and

if(!servletCtx.getAttribute((POLLING_CYCLE_FLAG_ATTR)) {
   // let the user add the feed ...
}

ServletContextListener is all about the web-app's life-cycle and the changes to the web-app's ServletContext. Typical use is for initializing and cleaning up resources for modules for the application. But basically, if you need to keep tabs on what is happening in the ServletContext, ServletContextListener is your friend.

OTHER TIPS

You keep the feed data in a database? Don't know how you access the database, but with plain jdbc you would select the feeds to poll and iterate over the resultset. Any newly added feed (after the select) would not appear in this resultset and therefore not get included.

If that for some reason does not apply to your problem, you could read the feats into a collection and work with that.

Or did i miss your question completely?

Agree with the last post that ServletContext attributes are the way to create sort of 'global variables' in the Servlet API.

This would not work across servlet container instances, or even web apps in one container. Each would have its own ServletContext. If you have just one instance, it works. Otherwise I'd guess you need a database flag.

Servlets get access to the ServletContext object via the init() method. ServletContextListeners also get access through the contextInitialized(ServletContextEvent sce) lifecycle event. I think it's reasonable for your poll class to exist as a listener that starts and stops with the ServletContext lifecycle, yes. And it will have access to the ServletContext when initialized, yes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top