Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Where place code in Servlet on start up using Tomcat.  RSS feed

 
Simon Reeves
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each time it is started or each time it is called?
 
Simon Reeves
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you plan to shut down one particular servlet?
 
Simon Reeves
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • 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: 12
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Neelesh Srinivasan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!