• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Where place code in Servlet on start up using Tomcat.

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have created a servlet and each time it is started up, it is required to run several threads
of code to completion prior to accepting any incoming requests.
The threads could take up to several minutes to complete and require to interface significantly with a
database and the file system.

I am new to writing servlets and would like to know the best place to insert this code. i.e. is the
servlet init() method the best (or an appropriate) place for this code?

Thank you,
Simon
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way of achieving this would be a ServletContextListener. Check out http://docs.oracle.com/cd/B15904_01/web.1012/b14017/filters.htm
However I am not sure this will be the optimal or correct way, considering

Simon Reeves wrote:The threads could take up to several minutes to complete and require to interface significantly with a
database and the file system.

 
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each time it is started or each time it is called?
 
Simon Reeves
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Each time the servlet within the webapp is started after having been shutdown. The ServletContextListener is a possible solution though there
are other servlets in the webapp that require ideally to still be running if this servlet is shutdown.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you plan to shut down one particular servlet?
 
Simon Reeves
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am investigating this - the web app consists of a number of spring mvc controllers and a couple of servlets.
I was hoping I could use tomcat manager or similar to shutdown a single servlet within the webapp. However
I'm not sure if this is possible? Would you know this?
The servlet in question handles browser requests and updates a database amongst other things.
The spring controllers respond to user requests from client guis (javascript, extjs etc.) and ideally these would
still be able to run if the servlet was shutdown.
I may have to create a separate webapp for the servlet?

 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you cannot shut down one particular servlet.

Why do you need to? It's a really odd thing to be doing.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:No, you cannot shut down one particular servlet.


It wouldn't be hard to do, though, and particularly in scenarios where servlets can be thought of as web services it often makes sense. I have implemented something like this via JMX - the servlet registers an MBean that has a boolean on/off switch. If it's turned off, the servlet returns with a 503 immediately before doing any work. The switch can be accessed by all the means you can use JMX - via JConcole or Visualvm if you have a direct IP connection or a VPN, or jmxsh if you only have shell access.
 
Simon Reeves
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The reason for shutting down a particular servlet is because configuration data
is changed fairly frequently and read upon servlet startup in servlet.init() -
which is not needed by other servlets.

I guess (not sure) I could update config data dynamically by passing a separate
request to the servlet?
or should a separate web app be the solution?

The reason for the long-running jobs is in order to carry out mainly transactions
that did not complete when the web app was last shutdown or stopped.

Spring declarative transactions are being used. Am not sure if this could also be
done on shutdown?

kind regrds,
Simon
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd just cache the config data in memory and update it as needed. No need to stop and start things.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can make use of ServletConfig reference to gain the information as the data is just bound to one particular Servlet. Also consider the option of <init-param> in your code that will help you out with it
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is not very helpful. For configuration information that needs to be update at run-time, the servlet configuration is not suitable. It's much easier, and customary to use a properties file that can be reloaded whenever desired (and perhaps cached in memory).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic